Testing

app.inject( options ) runs one request through the app — routing, middleware, handlers, error handling — on the calling thread, without opening a port, and returns what the app produced:

bxsvar res = app.inject( { method: "POST", url: "/users", body: { name: "ada" } } )
expect( res.statusCode ).toBe( 201 )
expect( res.json().name ).toBe( "ada" )

A bare string is a GET: app.inject( "/health/ready" ).

Options

OptionEffect
methodHTTP method (default "GET")
urlPath plus optional query string. Encoded characters in the path are decoded, as for a real request
queryA struct merged into the query string, URL-encoded. An array value repeats the key
headersRequest headers
bodyA struct or array is sent as JSON with the matching Content-Type; a string or binary value is sent as-is
formA struct, sent urlencoded
cookiesA struct, sent as the Cookie header
ipWhat req.ip sees (default 127.0.0.1)
logPrint the usual request log line (off by default)

The result

MemberDescription
statusCodeThe response status
bodyThe response body, as a string
headersA struct of response headers with lower-cased names
json()The body parsed as JSON
header( name )One response header, case-insensitive — "" if absent
cookiesThe cookies the response set, name to value
setCookiesEach raw Set-Cookie line

A response's cookies is shaped to be passed straight back in as the next call's cookies, which carries a session across calls:

bxsvar login = app.inject( { method: "POST", url: "/login", form: { user: "ada" } } )
var me    = app.inject( { url: "/me", cookies: login.cookies } )

What behaves like a real request

The request goes through the same Request/Response objects, router and error handling as one arriving over a socket. A 404, a thrown handler becoming a 500, an oversized body becoming a 413, and a response header refused for containing a newline (see Request & Response) all behave the same.

What it can't do

  • req.rawExchange() is null — there is no Undertow behind an injected request.
  • app.ws() routes can't be reached this way. Those need a real server and a WebSocket client.