Browse Source

mock responses.. example

master
Lloyd Brookes 9 years ago
parent
commit
9f5f969205
  1. 1
      .gitignore
  2. 6
      example/mock/.local-web-server.json
  3. 7
      example/mock/css/style.css
  4. 8
      example/mock/index.html
  5. 9
      example/mock/index.js
  6. 8
      example/mock/mocks/tree.mock.js
  7. 23
      example/mock/mocks/trees.mock.js
  8. 2
      jsdoc2md/README.hbs
  9. 49
      lib/local-web-server.js
  10. 1
      package.json

1
.gitignore

@ -1,2 +1,3 @@
node_modules node_modules
tmp tmp
*bundle.js

6
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" }
]
}

7
example/mock/css/style.css

@ -0,0 +1,7 @@
body {
background-color: #AA3939;
color: #FFE2E2
}
svg {
fill: #000
}

8
example/mock/index.html

@ -0,0 +1,8 @@
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<h1>Mock responses</h1>
<ul>
<li>list data</li>
</ul>
<script src="bundle.js"></script>

9
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 `<li>${tree.name}</li>`
}).join('')
})

8
example/mock/mocks/tree.mock.js

@ -0,0 +1,8 @@
module.exports = [
{
response: {
status: 200,
body: { id: 2, name: 'eucalyptus', maxHeight: 210 }
}
}
]

23
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'
}
}
]

2
jsdoc2md/README.hbs

@ -192,7 +192,7 @@ $ npm install local-web-server --save-dev
"version": "1.0.0", "version": "1.0.0",
"local-web-server": { "local-web-server": {
"port": 8100, "port": 8100,
"forbid": "\\.json$"
"forbid": "*.json"
}, },
"scripts": { "scripts": {
"start": "ws" "start": "ws"

49
lib/local-web-server.js

@ -2,11 +2,8 @@
const path = require('path') const path = require('path')
const http = require('http') const http = require('http')
const url = require('url') 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 * @module local-web-server
@ -43,15 +40,30 @@ function localWebServer (options) {
verbose: false verbose: false
}, options) }, 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 const log = options.log
log.options = log.options || {} log.options = log.options || {}
const app = new Koa() const app = new Koa()
const _use = app.use const _use = app.use
app.use = x => _use.call(app, convert(x)) app.use = x => _use.call(app, convert(x))
function verbose (category, message) { function verbose (category, message) {
if (options.verbose) { if (options.verbose) {
process.nextTick(() => app.emit('verbose', category, message))
debug(category, message)
// process.nextTick(() => {
// app.emit('verbose', category, message)
// })
} }
} }
app._verbose = verbose app._verbose = verbose
@ -89,7 +101,7 @@ 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')
// verbose('etag caching', 'enabled')
app.use(conditional()) app.use(conditional())
app.use(etag()) app.use(etag())
} }
@ -131,17 +143,20 @@ function localWebServer (options) {
} }
} }
/* Mock Responses */
app.use(mockResponses({ root: options.static.root, verbose: verbose }))
/* serve static files */ /* serve static files */
if (options.static.root) { if (options.static.root) {
const serve = require('koa-static') 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)) app.use(serve(options.static.root, options.static.options))
} }
/* serve directory index */ /* serve directory index */
if (options.serveIndex.path) { if (options.serveIndex.path) {
const serveIndex = require('koa-serve-index') 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)) 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) => { process.on('unhandledRejection', (reason, p) => {
throw reason throw reason
}) })

1
package.json

@ -29,6 +29,7 @@
"repository": "https://github.com/75lb/local-web-server", "repository": "https://github.com/75lb/local-web-server",
"author": "Lloyd Brookes <75pound@gmail.com>", "author": "Lloyd Brookes <75pound@gmail.com>",
"dependencies": { "dependencies": {
"array-back": "^1.0.2",
"command-line-args": "^2.0.2", "command-line-args": "^2.0.2",
"config-master": "^2", "config-master": "^2",
"http-proxy": "^1.12.0", "http-proxy": "^1.12.0",

Loading…
Cancel
Save