Browse Source

Update README.md

master
Lloyd Brookes 8 years ago
committed by GitHub
parent
commit
b2e72d86af
  1. 50
      README.md

50
README.md

@ -54,33 +54,37 @@ const users = [
{ "id": 3, "name": "Francesco", "age": 24 }
]
/* response mocks for /users */
module.exports = [
{
route: '/users',
responses: [
/* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
{
/* for GET requests return the collection */
request: { method: 'GET' },
response: { type: 'application/json', body: users }
},
module.exports = MockBase => class MockUsers extends MockBase {
mocks () {
/* response mocks for /users */
return [
{
/* for POST requests, create a new user and return its location */
request: { method: 'POST' },
response: function (ctx) {
const newUser = ctx.request.body
users.push(newUser)
newUser.id = users.length
ctx.status = 201
ctx.response.set('Location', `/users/${newUser.id}`)
}
route: '/users',
responses: [
/* Respond with 400 Bad Request for PUT and DELETE requests (inappropriate on a collection) */
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
{
/* for GET requests return the collection */
request: { method: 'GET' },
response: { type: 'application/json', body: users }
},
{
/* for POST requests, create a new user and return its location */
request: { method: 'POST' },
response: function (ctx) {
const newUser = ctx.request.body
users.push(newUser)
newUser.id = users.length
ctx.status = 201
ctx.response.set('Location', `/users/${newUser.id}`)
}
}
]
}
]
}
]
}
```
Next, launch `ws` passing in your mock response file:

Loading…
Cancel
Save