- Notifications
You must be signed in to change notification settings - Fork90
How to create a mock response
DEPRECATED. The --mocks option and lws-mock-response module were removed in v3.
If you need a server-side resource or service which doesn't yet exist or is out of reach, the goal of mocks is to enable you to quickly create one.
If you havelws-mock-response present in your stack (included by default) you will have the option to pass one or more mock module paths to--mocks.
$ ws --mocks my-mocks-module.jsThis is the minimum code required for a mock module, you can use it as a boilerplate template. ReplaceMyMockModule with a more useful name. Theoptions argument will contain the currently activews config.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[]}}
Themocks method must return one or more objects, each with two properties -route andresponses. The route value can use any Express-style routing syntax, describedhere in the "Route paths" and "Route parameters" sections.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[]}]}}
Theresponses property must contain one or more objects, each with aresponse property. Theresponse property value will be written directly to the Koactx object, meaning on this object you can set any of the standardctx response values. The properties you will set must commonly arebody,status andtype.
This example will respond with200 (the default statusCode) and the bodyMargerita to any request for/pizzas/:id, e.g./pizzas/1.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[{response:{body:'Margerita'}}]}]}}
We can convert our response to return JSON.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[{response:{type:'json',body:'{ "name": "Margerita" }'}}]}]}}
Each response object can optionally include arequest property. Therequest property limits which type of request should receive this mock response. The value should be an object with three possible properties:method,is andaccepts (all correlating to properties of the same name on theKoa Response object).
With this mock example...
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[{request:{method:'GET',accepts:'json'},response:{type:'json',body:'{ "name": "Margerita" }'}}]}]}}
...this request would receive a404 Not Found (asaccept is notjson):
$ curl http://127.0.0.1:8000/pizzas/1 --header 'accept: application/xml'However, if weaccept JSON will we receive a200:
$ curl http://127.0.0.1:8000/pizzas/1 --header 'accept: application/json'With this example...
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[{request:{method:'PUT',is:'json'},response:{type:'json',body:'{ "status": "updated" }'}}]}]}}
...this request would receive a404 Not Found (ascontent-type is notjson):
$ curl http://127.0.0.1:8000/pizzas/1 --header 'content-type: application/xml'However, if we setcontent-type to JSON will we receive a200:
$ curl http://127.0.0.1:8000/pizzas/1 --header 'content-type: application/json'If you need to craft a more complex response and know what you're doing with theKoa ctx object, you can defineresponse as a function and handle setting thectx values yourself. Note that theresponse function also receives any route parameter values. Therefore, if theroute is/pizzas/:id and you received a request for/pizzas/5 thenid will equal5.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){return[{route:'/pizzas/:id',responses:[{request:{method:'GET',is:'json'},response:function(ctx,id){ctx.json='json'ctx.body=`{ "name": "Margerita", "id":${id} }`}}]}]}}
Finally, if you want to send any debug information to the--verbose output, you can do so by emitting theverbose event anywhere within themocks method.
module.exports=MockBase=>classMyMockModuleextendsMockBase{mocks(options){this.emit('verbose','mock.MyMockModule','some debug information')return[]}}