This commit is contained in:
Lloyd Brookes
2015-11-24 11:46:47 +00:00
parent d963222d19
commit ccaae1614c
10 changed files with 121 additions and 151 deletions

View File

@ -44,7 +44,8 @@ function localWebServer (options) {
mime: {},
forbid: [],
rewrite: [],
verbose: false
verbose: false,
mocks: []
}, options)
if (options.verbose) {
@ -106,7 +107,7 @@ function localWebServer (options) {
app.use(mw.blacklist(options.forbid))
}
/* Cache */
/* cache */
if (!options['no-cache']) {
const conditional = require('koa-conditional-get')
const etag = require('koa-etag')
@ -144,7 +145,21 @@ function localWebServer (options) {
}
/* Mock Responses */
app.use(mw.mockResponses({ root: options.static.root }))
options.mocks.forEach(mock => {
if (mock.module) {
mock.targets = require(path.join(options.static.root, mock.module))
}
if (mock.targets) {
app.use(mw.mockResponses(mock.route, mock.targets))
} else if (mock.response) {
mock.target = {
request: mock.request,
response: mock.response
}
app.use(mw.mockResponses(mock.route, mock.target))
}
})
/* serve static files */
if (options.static.root) {

View File

@ -3,6 +3,7 @@ const path = require('path')
const http = require('http')
const url = require('url')
const arrayify = require('array-back')
const t = require('typical')
const pathToRegexp = require('path-to-regexp')
const debug = require('debug')('local-web-server')
@ -63,43 +64,6 @@ function blacklist (forbid) {
}
}
function mockResponses (options) {
options = options || { root: process.cwd() }
return function mockResponses (ctx, next) {
if (/\.mock.js$/.test(ctx.path)) {
const mocks = arrayify(require(path.join(options.root, ctx.path)))
const testValue = require('test-value')
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)
}
})
})
/* else take the first mock without a request (no request means 'all requests') */
if (!mock) {
mock = mocks.find(mock => !mock.request)
}
if (mock) {
let mockedResponse = mock.response
if (t.isFunction(mock.response)) {
mockedResponse = new mock.response(ctx)
}
debug('mock response: %j', mockedResponse)
Object.assign(ctx.response, mockedResponse)
}
} else {
return next()
}
}
}
function mime (mimeTypes) {
return function mime (ctx, next) {
return next().then(() => {
@ -111,3 +75,42 @@ function mime (mimeTypes) {
})
}
}
function mockResponses (route, targets) {
targets = arrayify(targets)
debug('mock route: %s, targets: %j', route, targets);
const pathRe = pathToRegexp(route)
return function mockResponse (ctx, next) {
if (pathRe.test(ctx.url)) {
const testValue = require('test-value')
/* find a mock with compatible method and accepts */
let target = targets.find(target => {
return testValue(target, {
request: {
method: [ ctx.method, undefined ],
accepts: type => ctx.accepts(type)
}
})
})
/* else take the first target without a request (no request means 'all requests') */
if (!target) {
target = targets.find(target => !target.request)
}
if (target) {
let mockedResponse = target.response
if (t.isFunction(target.response)) {
const pathMatches = ctx.url.match(pathRe).slice(1)
const ctor = target.response.bind(null, ...[ctx].concat(pathMatches))
mockedResponse = new ctor()
}
debug('target response: %j', mockedResponse)
Object.assign(ctx.response, mockedResponse)
}
}
return next()
}
}