Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a mock response

Yannick Drolet edited this pageJul 22, 2020 ·14 revisions

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.

Consuming mock modules

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.js

Creating a mock module

This 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[]}}

route andresponses

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:[]}]}}

response

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'}}]}]}}

responsetype

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" }'}}]}]}}

request

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).

accepts

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'

is

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'

response function

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} }`}}]}]}}

verbose events

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[]}}

See also

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp