mock responses

This commit is contained in:
Lloyd Brookes
2015-11-19 20:40:41 +00:00
parent 509cea9de2
commit f96c9382cb
3 changed files with 23 additions and 28 deletions

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

View File

@ -1,28 +1,21 @@
const data = [
{ 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 }
]
const data = require('./trees')
module.exports = [
/* CREATE */
{
request: { method: 'POST' },
response: {
status: 201,
location: '/tree/1'
response: function (ctx) {
data.push(ctx.request.body)
this.status = 201
this.location = '/tree/1'
}
},
/* READ */
{
request: { method: 'GET' },
response: {
status: 200,
body: function (ctx) {
return data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan))
}
response: function (ctx) {
this.status = 200
this.body = data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan || 0))
}
},
/* UPDATE (forbidden on collection)*/
@ -36,3 +29,6 @@ module.exports = [
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