mock responses
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"rewrite": [
|
"rewrite": [
|
||||||
{ "from": "/tree", "to": "/mocks/trees.mock.js" },
|
{ "from": "/trees", "to": "/mocks/trees.mock.js" },
|
||||||
{ "from": "/tree/:id", "to": "/mocks/tree.mock.js" }
|
{ "from": "/trees/:id", "to": "/mocks/tree.mock.js" },
|
||||||
|
{ "from": "/search", "to": "/mocks/search.mock.js" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
20
example/mock/mocks/search.mock.js
Normal file
20
example/mock/mocks/search.mock.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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,8 +1,25 @@
|
|||||||
module.exports = [
|
module.exports = [
|
||||||
|
/* CREATE (409 CONFLICT - should be called on the collection) */
|
||||||
{
|
{
|
||||||
|
request: { method: 'POST' },
|
||||||
|
response: { status: 409 }
|
||||||
|
},
|
||||||
|
/* READ */
|
||||||
|
{
|
||||||
|
request: { method: 'GET' },
|
||||||
response: {
|
response: {
|
||||||
status: 200,
|
status: 200,
|
||||||
body: { id: 2, name: 'eucalyptus', maxHeight: 210 }
|
body: { id: 2, name: 'eucalyptus', maxHeight: 210 }
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
/* UPDATE */
|
||||||
|
{
|
||||||
|
request: { method: 'PUT' },
|
||||||
|
response: { status: 204 }
|
||||||
|
},
|
||||||
|
/* DELETE */
|
||||||
|
{
|
||||||
|
request: { method: 'DELETE' },
|
||||||
|
response: { status: 204 }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -1,23 +1,38 @@
|
|||||||
|
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 }
|
||||||
|
]
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
|
/* CREATE */
|
||||||
{
|
{
|
||||||
request: {
|
request: { method: 'POST' },
|
||||||
method: 'GET'
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
status: 200,
|
|
||||||
body: [
|
|
||||||
{ id: 1, name: 'conifer', maxHeight: 115 },
|
|
||||||
{ id: 2, name: 'eucalyptus', maxHeight: 210 }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
request: {
|
|
||||||
method: 'POST'
|
|
||||||
},
|
|
||||||
response: {
|
response: {
|
||||||
status: 201,
|
status: 201,
|
||||||
location: '/tree/1'
|
location: '/tree/1'
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
/* READ */
|
||||||
|
{
|
||||||
|
request: { method: 'GET' },
|
||||||
|
response: {
|
||||||
|
status: 200,
|
||||||
|
body: function (ctx) {
|
||||||
|
return data.filter(tree => tree.maxHeight > Number(ctx.query.tallerThan))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/* UPDATE (forbidden on collection)*/
|
||||||
|
{
|
||||||
|
request: { method: 'PUT' },
|
||||||
|
response: { status: 404 }
|
||||||
|
},
|
||||||
|
/* DELETE (forbidden on collection) */
|
||||||
|
{
|
||||||
|
request: { method: 'DELETE' },
|
||||||
|
response: { status: 404 }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -3,7 +3,7 @@ const path = require('path')
|
|||||||
const http = require('http')
|
const http = require('http')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
const arrayify = require('array-back')
|
const arrayify = require('array-back')
|
||||||
let debug
|
let debug, pathToRegexp
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @module local-web-server
|
* @module local-web-server
|
||||||
@ -57,7 +57,7 @@ function localWebServer (options) {
|
|||||||
const convert = require('koa-convert')
|
const convert = require('koa-convert')
|
||||||
const cors = require('kcors')
|
const cors = require('kcors')
|
||||||
const _ = require('koa-route')
|
const _ = require('koa-route')
|
||||||
const pathToRegexp = require('path-to-regexp')
|
pathToRegexp = require('path-to-regexp')
|
||||||
debug = require('debug')('local-web-server')
|
debug = require('debug')('local-web-server')
|
||||||
|
|
||||||
const log = options.log
|
const log = options.log
|
||||||
@ -70,9 +70,6 @@ function localWebServer (options) {
|
|||||||
function verbose (category, message) {
|
function verbose (category, message) {
|
||||||
if (options.verbose) {
|
if (options.verbose) {
|
||||||
debug(category, message)
|
debug(category, message)
|
||||||
// process.nextTick(() => {
|
|
||||||
// app.emit('verbose', category, message)
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
app._verbose = verbose
|
app._verbose = verbose
|
||||||
@ -93,8 +90,9 @@ function localWebServer (options) {
|
|||||||
app.use(_.all(route.from, proxyRequest(route, app)))
|
app.use(_.all(route.from, proxyRequest(route, app)))
|
||||||
} else {
|
} else {
|
||||||
const rewrite = require('koa-rewrite')
|
const rewrite = require('koa-rewrite')
|
||||||
verbose('local rewrite', `${route.from} -> ${route.to}`)
|
const mw = rewrite(route.from, route.to)
|
||||||
app.use(rewrite(route.from, route.to))
|
mw._name = 'rewrite'
|
||||||
|
app.use(mw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -110,7 +108,6 @@ function localWebServer (options) {
|
|||||||
if (!options['no-cache']) {
|
if (!options['no-cache']) {
|
||||||
const conditional = require('koa-conditional-get')
|
const conditional = require('koa-conditional-get')
|
||||||
const etag = require('koa-etag')
|
const etag = require('koa-etag')
|
||||||
// verbose('etag caching', 'enabled')
|
|
||||||
app.use(conditional())
|
app.use(conditional())
|
||||||
app.use(etag())
|
app.use(etag())
|
||||||
}
|
}
|
||||||
@ -239,11 +236,39 @@ function mockResponses (options) {
|
|||||||
return function mockResponses (ctx, next) {
|
return function mockResponses (ctx, next) {
|
||||||
if (/\.mock.js$/.test(ctx.path)) {
|
if (/\.mock.js$/.test(ctx.path)) {
|
||||||
const mocks = arrayify(require(path.join(options.root, ctx.path)))
|
const mocks = arrayify(require(path.join(options.root, ctx.path)))
|
||||||
const mock = mocks.find(mock => {
|
const testValue = require('test-value')
|
||||||
return !mock.request || mock.request.method === ctx.method
|
const t = require('typical')
|
||||||
|
|
||||||
|
/* find a mock with compatible method and accepts */
|
||||||
|
let mock = mocks.find(mock => {
|
||||||
|
return testValue(mock, {
|
||||||
|
request: {
|
||||||
|
method: [ ctx.method, undefined ],
|
||||||
|
accepts: type => ctx.accepts(type)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Object.assign(ctx.response, mock.response)
|
|
||||||
options.verbose('mock response', JSON.stringify(mock.response), JSON.stringify(ctx.response))
|
/* else take the first mock without a request (no request means 'all requests') */
|
||||||
|
if (!mock) {
|
||||||
|
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) {
|
||||||
|
Object.assign(ctx.response, mockedReponse)
|
||||||
|
options.verbose('mocked response', JSON.stringify(mockedReponse))
|
||||||
|
options.verbose('actual response', JSON.stringify(ctx.response))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,9 @@
|
|||||||
"koa-static": "^1.5.2",
|
"koa-static": "^1.5.2",
|
||||||
"path-to-regexp": "^1.2.1",
|
"path-to-regexp": "^1.2.1",
|
||||||
"stream-log-stats": "^v1.1.0-0",
|
"stream-log-stats": "^v1.1.0-0",
|
||||||
"string-tools": "^1.0.0"
|
"string-tools": "^1.0.0",
|
||||||
|
"test-value": "^1.0.1",
|
||||||
|
"typical": "^2.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"req-then": "^0.2.2",
|
"req-then": "^0.2.2",
|
||||||
|
Reference in New Issue
Block a user