From 9f5f9692051b0b5e301701a04a2411938c3954c8 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Wed, 18 Nov 2015 16:37:16 +0000 Subject: [PATCH] mock responses.. example --- .gitignore | 1 + example/mock/.local-web-server.json | 6 +++++ example/mock/css/style.css | 7 ++++++ example/mock/index.html | 8 ++++++ example/mock/index.js | 9 +++++++ example/mock/mocks/tree.mock.js | 8 ++++++ example/mock/mocks/trees.mock.js | 23 +++++++++++++++++ jsdoc2md/README.hbs | 2 +- lib/local-web-server.js | 49 ++++++++++++++++++++++++++++++------- package.json | 1 + 10 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 example/mock/.local-web-server.json create mode 100644 example/mock/css/style.css create mode 100644 example/mock/index.html create mode 100644 example/mock/index.js create mode 100644 example/mock/mocks/tree.mock.js create mode 100644 example/mock/mocks/trees.mock.js diff --git a/.gitignore b/.gitignore index 6cb772b..70a2466 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules tmp +*bundle.js diff --git a/example/mock/.local-web-server.json b/example/mock/.local-web-server.json new file mode 100644 index 0000000..a16f358 --- /dev/null +++ b/example/mock/.local-web-server.json @@ -0,0 +1,6 @@ +{ + "rewrite": [ + { "from": "/tree", "to": "/mocks/trees.mock.js" }, + { "from": "/tree/:id", "to": "/mocks/tree.mock.js" } + ] +} diff --git a/example/mock/css/style.css b/example/mock/css/style.css new file mode 100644 index 0000000..7fb71e5 --- /dev/null +++ b/example/mock/css/style.css @@ -0,0 +1,7 @@ +body { + background-color: #AA3939; + color: #FFE2E2 +} +svg { + fill: #000 +} diff --git a/example/mock/index.html b/example/mock/index.html new file mode 100644 index 0000000..eec74b2 --- /dev/null +++ b/example/mock/index.html @@ -0,0 +1,8 @@ + + + +

Mock responses

+ + diff --git a/example/mock/index.js b/example/mock/index.js new file mode 100644 index 0000000..ce602ed --- /dev/null +++ b/example/mock/index.js @@ -0,0 +1,9 @@ +'use strict' +const request = require('req-then') +const $ = document.querySelector.bind(document) + +request('http://localhost:8000/tree').then(response => { + $('ul').innerHTML = JSON.parse(response.data).map(tree => { + return `
  • ${tree.name}
  • ` + }).join('') +}) diff --git a/example/mock/mocks/tree.mock.js b/example/mock/mocks/tree.mock.js new file mode 100644 index 0000000..753f707 --- /dev/null +++ b/example/mock/mocks/tree.mock.js @@ -0,0 +1,8 @@ +module.exports = [ + { + response: { + status: 200, + body: { id: 2, name: 'eucalyptus', maxHeight: 210 } + } + } +] diff --git a/example/mock/mocks/trees.mock.js b/example/mock/mocks/trees.mock.js new file mode 100644 index 0000000..2d3e30e --- /dev/null +++ b/example/mock/mocks/trees.mock.js @@ -0,0 +1,23 @@ +module.exports = [ + { + request: { + method: 'GET' + }, + response: { + status: 200, + body: [ + { id: 1, name: 'conifer', maxHeight: 115 }, + { id: 2, name: 'eucalyptus', maxHeight: 210 } + ] + } + }, + { + request: { + method: 'POST' + }, + response: { + status: 201, + location: '/tree/1' + } + } +] diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs index cb4b35a..1262a26 100644 --- a/jsdoc2md/README.hbs +++ b/jsdoc2md/README.hbs @@ -192,7 +192,7 @@ $ npm install local-web-server --save-dev "version": "1.0.0", "local-web-server": { "port": 8100, - "forbid": "\\.json$" + "forbid": "*.json" }, "scripts": { "start": "ws" diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 6f79222..fc450c8 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -2,11 +2,8 @@ const path = require('path') const http = require('http') const url = require('url') -const Koa = require('koa') -const convert = require('koa-convert') -const cors = require('kcors') -const _ = require('koa-route') -const pathToRegexp = require('path-to-regexp') +const arrayify = require('array-back') +let debug /** * @module local-web-server @@ -43,15 +40,30 @@ function localWebServer (options) { verbose: false }, options) + if (options.verbose) { + process.env.DEBUG = '*' + } + + const Koa = require('koa') + const convert = require('koa-convert') + const cors = require('kcors') + const _ = require('koa-route') + const pathToRegexp = require('path-to-regexp') + debug = require('debug')('local-web-server') + const log = options.log log.options = log.options || {} const app = new Koa() const _use = app.use app.use = x => _use.call(app, convert(x)) + function verbose (category, message) { if (options.verbose) { - process.nextTick(() => app.emit('verbose', category, message)) + debug(category, message) + // process.nextTick(() => { + // app.emit('verbose', category, message) + // }) } } app._verbose = verbose @@ -89,7 +101,7 @@ function localWebServer (options) { if (!options['no-cache']) { const conditional = require('koa-conditional-get') const etag = require('koa-etag') - verbose('etag caching', 'enabled') + // verbose('etag caching', 'enabled') app.use(conditional()) app.use(etag()) } @@ -131,17 +143,20 @@ function localWebServer (options) { } } + /* Mock Responses */ + app.use(mockResponses({ root: options.static.root, verbose: verbose })) + /* serve static files */ if (options.static.root) { const serve = require('koa-static') - verbose('static', `root: ${options.static.root} options: ${JSON.stringify(options.static.options)}` ) + // verbose('static', 'enabled') app.use(serve(options.static.root, options.static.options)) } /* serve directory index */ if (options.serveIndex.path) { const serveIndex = require('koa-serve-index') - verbose('serve-index', `root: ${options.serveIndex.path} options: ${JSON.stringify(options.serveIndex.options)}` ) + // verbose('serve-index', 'enabled') app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) } @@ -210,6 +225,22 @@ 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 mock = mocks.find(mock => { + return !mock.request || mock.request.method === ctx.method + }) + Object.assign(ctx.response, mock.response) + options.verbose('mock response', JSON.stringify(mock.response), JSON.stringify(ctx.response)) + } else { + return next() + } + } +} + process.on('unhandledRejection', (reason, p) => { throw reason }) diff --git a/package.json b/package.json index 114e966..bac763d 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "repository": "https://github.com/75lb/local-web-server", "author": "Lloyd Brookes <75pound@gmail.com>", "dependencies": { + "array-back": "^1.0.2", "command-line-args": "^2.0.2", "config-master": "^2", "http-proxy": "^1.12.0",