|
@ -3,6 +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') |
|
|
|
|
|
const t = require('typical') |
|
|
const pathToRegexp = require('path-to-regexp') |
|
|
const pathToRegexp = require('path-to-regexp') |
|
|
const debug = require('debug')('local-web-server') |
|
|
const debug = require('debug')('local-web-server') |
|
|
|
|
|
|
|
@ -63,17 +64,30 @@ 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))) |
|
|
|
|
|
|
|
|
function mime (mimeTypes) { |
|
|
|
|
|
return function mime (ctx, next) { |
|
|
|
|
|
return next().then(() => { |
|
|
|
|
|
const reqPathExtension = path.extname(ctx.path).slice(1) |
|
|
|
|
|
Object.keys(mimeTypes).forEach(mimeType => { |
|
|
|
|
|
const extsToOverride = mimeTypes[mimeType] |
|
|
|
|
|
if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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') |
|
|
const testValue = require('test-value') |
|
|
const t = require('typical') |
|
|
|
|
|
|
|
|
|
|
|
/* find a mock with compatible method and accepts */ |
|
|
/* find a mock with compatible method and accepts */ |
|
|
let mock = mocks.find(mock => { |
|
|
|
|
|
return testValue(mock, { |
|
|
|
|
|
|
|
|
let target = targets.find(target => { |
|
|
|
|
|
return testValue(target, { |
|
|
request: { |
|
|
request: { |
|
|
method: [ ctx.method, undefined ], |
|
|
method: [ ctx.method, undefined ], |
|
|
accepts: type => ctx.accepts(type) |
|
|
accepts: type => ctx.accepts(type) |
|
@ -81,33 +95,22 @@ function mockResponses (options) { |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
/* else take the first mock without a request (no request means 'all requests') */ |
|
|
|
|
|
if (!mock) { |
|
|
|
|
|
mock = mocks.find(mock => !mock.request) |
|
|
|
|
|
|
|
|
/* else take the first target without a request (no request means 'all requests') */ |
|
|
|
|
|
if (!target) { |
|
|
|
|
|
target = targets.find(target => !target.request) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (mock) { |
|
|
|
|
|
let mockedResponse = mock.response |
|
|
|
|
|
if (t.isFunction(mock.response)) { |
|
|
|
|
|
mockedResponse = new mock.response(ctx) |
|
|
|
|
|
|
|
|
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('mock response: %j', mockedResponse) |
|
|
|
|
|
|
|
|
debug('target response: %j', mockedResponse) |
|
|
Object.assign(ctx.response, mockedResponse) |
|
|
Object.assign(ctx.response, mockedResponse) |
|
|
} |
|
|
} |
|
|
} else { |
|
|
|
|
|
return next() |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function mime (mimeTypes) { |
|
|
|
|
|
return function mime (ctx, next) { |
|
|
|
|
|
return next().then(() => { |
|
|
|
|
|
const reqPathExtension = path.extname(ctx.path).slice(1) |
|
|
|
|
|
Object.keys(mimeTypes).forEach(mimeType => { |
|
|
|
|
|
const extsToOverride = mimeTypes[mimeType] |
|
|
|
|
|
if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
return next() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |