Testing requests

A lot of the functionality of your plugin is exposed to the user through your plugin’s request handlers. The testing framework provides an easy way to call them and check the responses. Information about the request and response is made available to your test scripts for checking with assertions.

The testing framework does not use the full HTTP handling stack to call request handlers under test, but you should not notice any difference between the test and live environments.

function t.login(as)

Set up the request context for the given user.

The as argument specifies the user, which can be any value accepted by O.user(), or a SecurityPrincipal object.

Alternatively, you can pass the string "ANONYMOUS" to ‘log in’ as the anonymous user, which is the state before a real user has logged in. Usually your plugin will not handle requests for the anonymous user, but this can be overridden using allowAnonymousRequests in plugin.json.

function t.loginAnonymous()

Shortcut function which calls t.login("ANONYMOUS").

function t.logout()

Set the test state to be outside a request context.

function t.request(method, path, parameters, options)

Call the request handler for the given method and path.

parameters is an optional JavaScript object containing the parameters passed to the request handler. Note that even for GET requests, parameters must be specified in this manner. ?key=value notation on the end of the path is not decoded.

The optional options argument specifies any options for the request. See “Request options” below for details.

Information about the request and the response are returned from the function, and made available in the t.last property. See “Request response information” below for details.

function t.get(path, parameters, options)

Shortcut function which calls t.request() with the "GET" method.

function t.post(path, parameters, options)

Shortcut function which calls t.request() with the "POST" method.

property t.last

The t.last property contains the last return value from the t.request() function. You can use this to avoid needing to declare variables to store the return values, and make your tests read a bit better.

Request options

The t.request() function takes an optional options argument. This has properties:

property headers

E.request.headers is set to the value of this property.

property plugin

The name of the plugin which should respond to this request. This defaults to the plugin under test, and is only necessary if you want to call request handlers in other plugins.

Request response information

The t.request() function returns a JavaScript object with the following properties, and makes it available as the t.last property.

property view

The view argument passed to the E.render() function by the request handler.

property templateName

The templateName argument passed to the E.render() function by the request handler, or if one wasn’t specified, the name of the automatically chosen template.

property templateOptions

The templateOptions argument passed to the E.render() function by the request handler, or {} if not specified.

property body

The body of the response, for example, the output of the rendered template or a value or object set with E.response.body.

property method

The method specified in the request.

property path

The full path specified in the request.

property extraPathElements

The value of extraPathElements in the request object.