koa-mock-response
Mocks give you full control over the response headers and body returned to the client. They can be used to return anything from a simple html string to a resourceful REST API. Typically, they're used to mock services but can be used for anything.
In the config, define an array called mocks
. Each mock definition maps a route
to a response
. A simple home page:
Under the hood, the property values from the response
object are written onto the underlying koa response object. You can set any valid koa response properies, for example type:
Conditional Response
To define a conditional response, set a request
object on the mock definition. The request
value acts as a query - the response defined will only be returned if each property of the request
query matches. For example, return an XML response only if the request headers include accept: application/xml
, else return 404 Not Found.
Multiple Potential Responses
To specify multiple potential responses, set an array of mock definitions to the responses
property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
Dynamic Response
The examples above all returned static data. To define a dynamic response, create a mock module. Specify its path in the module
property:
Here's what the stream-self
module looks like. The module should export a mock definition (an object, or array of objects, each with a response
and optional request
). In this example, the module simply streams itself to the response but you could set body
to any valid value.
const fs = moduleexports = response: body: fs
Response function
For more power, define the response as a function. It will receive the koa context as its first argument. Now you have full programmatic control over the response returned.
moduleexports = { ctxbody = '<h1>I can do anything i want.</h1>' }
If the route contains tokens, their values are passed to the response. For example, with this mock...
...the id
value is passed to the response
function. For example, a path of /players/10?name=Lionel
would pass 10
to the response function. Additional, the value Lionel
would be available on ctx.query.name
:
moduleexports = { ctxbody = `<h1>id: , name: </h1>` }
RESTful Resource example
Here's an example of a REST collection (users). We'll create two routes, one for actions on the resource collection, one for individual resource actions.
Define a module (users.json
) defining seed data:
The collection module:
const users = /* responses for /users */const mockResponses = /* Respond with 400 Bad Request for PUT and DELETE - inappropriate on a collection */ request: method: 'PUT' response: status: 400 request: method: 'DELETE' response: status: 400 /* for GET requests return a subset of data, optionally filtered on 'minAge' and 'nationality' */ request: method: 'GET' { ctxbody = users } /* for POST requests, create a new user and return the path to the new resource */ request: method: 'POST' { const newUser = ctxrequestbody users newUserid = userslength ctxstatus = 201 ctxresponse } moduleexports = mockResponses
The individual resource module:
const users = /* responses for /users/:id */const mockResponses = /* don't support POST here */ request: method: 'POST' response: status: 400 /* for GET requests, return a particular user */ request: method: 'GET' { ctxbody = users } /* for PUT requests, update the record */ request: method: 'PUT' { const updatedUser = ctxrequestbody const existingUserIndex = users users ctxstatus = 200 } /* DELETE request: remove the record */ request: method: 'DELETE' { const existingUserIndex = users users ctxstatus = 200 } moduleexports = mockResponses
© 2017-20 Lloyd Brookes 75pound@gmail.com.