mock responses
This commit is contained in:
7
example/mock/mocks/trees.json
Normal file
7
example/mock/mocks/trees.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[
|
||||||
|
{ "id": 1, "name": "Conifer", "maxHeight": 115 },
|
||||||
|
{ "id": 2, "name": "Eucalyptus", "maxHeight": 210 },
|
||||||
|
{ "id": 3, "name": "Ash", "maxHeight": 40 },
|
||||||
|
{ "id": 4, "name": "Elder", "maxHeight": 5 },
|
||||||
|
{ "id": 5, "name": "Holly", "maxHeight": 10 }
|
||||||
|
]
|
@ -1,28 +1,21 @@
|
|||||||
const data = [
|
const data = require('./trees')
|
||||||
{ id: 1, name: 'Conifer', maxHeight: 115 },
|
|
||||||
{ id: 2, name: 'Eucalyptus', maxHeight: 210 },
|
|
||||||
{ id: 3, name: 'Ash', maxHeight: 40 },
|
|
||||||
{ id: 4, name: 'Elder', maxHeight: 5 },
|
|
||||||
{ id: 5, name: 'Holly', maxHeight: 10 }
|
|
||||||
]
|
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
/* CREATE */
|
/* CREATE */
|
||||||
{
|
{
|
||||||
request: { method: 'POST' },
|
request: { method: 'POST' },
|
||||||
response: {
|
response: function (ctx) {
|
||||||
status: 201,
|
data.push(ctx.request.body)
|
||||||
location: '/tree/1'
|
this.status = 201
|
||||||
|
this.location = '/tree/1'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/* READ */
|
/* READ */
|
||||||
{
|
{
|
||||||
request: { method: 'GET' },
|
request: { method: 'GET' },
|
||||||
response: {
|
response: function (ctx) {
|
||||||
status: 200,
|
this.status = 200
|
||||||
body: function (ctx) {
|
this.body = data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan || 0))
|
||||||
return data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/* UPDATE (forbidden on collection)*/
|
/* UPDATE (forbidden on collection)*/
|
||||||
@ -36,3 +29,6 @@ module.exports = [
|
|||||||
response: { status: 404 }
|
response: { status: 404 }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// curl -i http://localhost:8000/trees -H 'content-type: application/json' -d '{"id":6, "name":"Oak", "maxHeight": 100 }'
|
||||||
|
// curl -i http://localhost:8000/trees
|
||||||
|
@ -86,20 +86,12 @@ function mockResponses (options) {
|
|||||||
mock = mocks.find(mock => !mock.request)
|
mock = mocks.find(mock => !mock.request)
|
||||||
}
|
}
|
||||||
|
|
||||||
const mockedReponse = {}
|
|
||||||
/* resolve any functions on the mock */
|
|
||||||
Object.keys(mock.response).forEach(key => {
|
|
||||||
if (t.isFunction(mock.response[key])) {
|
|
||||||
mockedReponse[key] = mock.response[key](ctx)
|
|
||||||
} else {
|
|
||||||
mockedReponse[key] = mock.response[key]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (mock) {
|
if (mock) {
|
||||||
Object.assign(ctx.response, mockedReponse)
|
let mockedResponse = mock.response
|
||||||
// debug('mocked response', JSON.stringify(mockedReponse))
|
if (t.isFunction(mock.response)) {
|
||||||
// debug('actual response', JSON.stringify(ctx.response))
|
mockedResponse = new mock.response(ctx)
|
||||||
|
}
|
||||||
|
Object.assign(ctx.response, mockedResponse)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return next()
|
return next()
|
||||||
|
Reference in New Issue
Block a user