Lloyd Brookes
9 years ago
10 changed files with 114 additions and 144 deletions
-
31bin/cli.js
-
44example/mock/.local-web-server.json
-
8example/mock/mocks/five.js
-
5example/mock/mocks/four.js
-
20example/mock/mocks/search.mock.js
-
25example/mock/mocks/tree.mock.js
-
7example/mock/mocks/trees.json
-
34example/mock/mocks/trees.mock.js
-
21lib/local-web-server.js
-
63lib/middleware.js
@ -1,7 +1,43 @@ |
|||||
{ |
{ |
||||
"rewrite": [ |
|
||||
{ "from": "/trees", "to": "/mocks/trees.mock.js" }, |
|
||||
{ "from": "/trees/:id", "to": "/mocks/tree.mock.js" }, |
|
||||
{ "from": "/search", "to": "/mocks/search.mock.js" } |
|
||||
|
"mocks": [ |
||||
|
{ |
||||
|
"route": "/one", |
||||
|
"response": { |
||||
|
"body": { "id": 1, "name": "whatever" } |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"route": "/two", |
||||
|
"request": { "accepts": "xml" }, |
||||
|
"response": { |
||||
|
"body": "<result id='2' name='whatever' />" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"route": "/three", |
||||
|
"targets": [ |
||||
|
{ |
||||
|
"request": { "method": "GET" }, |
||||
|
"response": { |
||||
|
"body": { "id": 1, "name": "whatever" } |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"request": { "method": "POST" }, |
||||
|
"response": { |
||||
|
"status": 400, |
||||
|
"body": { "message": "That method is not allowed." } |
||||
|
} |
||||
|
} |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"route": "/four", |
||||
|
"module": "/mocks/four.js" |
||||
|
}, |
||||
|
{ |
||||
|
"route": "/five/:id\\?name=:name", |
||||
|
"module": "/mocks/five.js" |
||||
|
} |
||||
] |
] |
||||
} |
} |
@ -0,0 +1,8 @@ |
|||||
|
module.exports = { |
||||
|
response: function (ctx, id, name) { |
||||
|
this.body = { |
||||
|
id: id, |
||||
|
name: name |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
module.exports = { |
||||
|
response: { |
||||
|
body: { id: 2, name: 'eucalyptus', maxHeight: 210 } |
||||
|
} |
||||
|
} |
@ -1,20 +0,0 @@ |
|||||
module.exports = [ |
|
||||
{ |
|
||||
request: { |
|
||||
accepts: 'json' |
|
||||
}, |
|
||||
response: { |
|
||||
status: 200, |
|
||||
body: { id: 2, name: 'eucalyptus', maxHeight: 210 } |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
request: { |
|
||||
accepts: 'xml' |
|
||||
}, |
|
||||
response: { |
|
||||
status: 200, |
|
||||
body: '<tree id="2" name="eucalyptus" maxHeight="210"/>' |
|
||||
} |
|
||||
} |
|
||||
] |
|
@ -1,25 +0,0 @@ |
|||||
module.exports = [ |
|
||||
/* CREATE (409 CONFLICT - should be called on the collection) */ |
|
||||
{ |
|
||||
request: { method: 'POST' }, |
|
||||
response: { status: 409 } |
|
||||
}, |
|
||||
/* READ */ |
|
||||
{ |
|
||||
request: { method: 'GET' }, |
|
||||
response: { |
|
||||
status: 200, |
|
||||
body: { id: 2, name: 'eucalyptus', maxHeight: 210 } |
|
||||
} |
|
||||
}, |
|
||||
/* UPDATE */ |
|
||||
{ |
|
||||
request: { method: 'PUT' }, |
|
||||
response: { status: 204 } |
|
||||
}, |
|
||||
/* DELETE */ |
|
||||
{ |
|
||||
request: { method: 'DELETE' }, |
|
||||
response: { status: 204 } |
|
||||
} |
|
||||
] |
|
@ -1,7 +0,0 @@ |
|||||
[ |
|
||||
{ "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,34 +0,0 @@ |
|||||
const data = require('./trees') |
|
||||
|
|
||||
module.exports = [ |
|
||||
/* CREATE */ |
|
||||
{ |
|
||||
request: { method: 'POST' }, |
|
||||
response: function (ctx) { |
|
||||
data.push(ctx.request.body) |
|
||||
this.status = 201 |
|
||||
this.location = '/tree/1' |
|
||||
} |
|
||||
}, |
|
||||
/* READ */ |
|
||||
{ |
|
||||
request: { method: 'GET' }, |
|
||||
response: function (ctx) { |
|
||||
this.status = 200 |
|
||||
this.body = data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan || 0)) |
|
||||
} |
|
||||
}, |
|
||||
/* UPDATE (forbidden on collection)*/ |
|
||||
{ |
|
||||
request: { method: 'PUT' }, |
|
||||
response: { status: 404 } |
|
||||
}, |
|
||||
/* DELETE (forbidden on collection) */ |
|
||||
{ |
|
||||
request: { method: 'DELETE' }, |
|
||||
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
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue