From 343399c1d91caefce0a6290c7ecccaecc781b225 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 18 Jun 2016 21:17:20 +0100 Subject: [PATCH] update tests.. refactor --- bin/cli.js | 2 +- extend/cache-control/server.js | 2 +- extend/live-reload-optional/server.js | 2 +- extend/live-reload/server.js | 2 +- lib/local-web-server.js | 51 ++++++------- lib/middleware-stack.js | 2 +- package.json | 2 +- test/{fixture/rewrite/one.html => proxy/file.txt} | 0 test/proxy/one.html | 1 + test/proxy/rewrite-proxy.js | 84 +++++++++++++++++++++ test/rewrite-proxy.js | 89 ----------------------- 11 files changed, 114 insertions(+), 123 deletions(-) rename test/{fixture/rewrite/one.html => proxy/file.txt} (100%) create mode 100644 test/proxy/one.html create mode 100644 test/proxy/rewrite-proxy.js delete mode 100644 test/rewrite-proxy.js diff --git a/bin/cli.js b/bin/cli.js index 328ec15..371ba28 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -16,4 +16,4 @@ ws.addCors() .addSpa() .addStatic() .addIndex() - .start() + .listen() diff --git a/extend/cache-control/server.js b/extend/cache-control/server.js index cf01b8d..f72e532 100644 --- a/extend/cache-control/server.js +++ b/extend/cache-control/server.js @@ -15,4 +15,4 @@ ws.addLogging('dev') }) .addStatic() .addIndex() - .start() + .listen() diff --git a/extend/live-reload-optional/server.js b/extend/live-reload-optional/server.js index 4c29757..7c48b99 100644 --- a/extend/live-reload-optional/server.js +++ b/extend/live-reload-optional/server.js @@ -16,4 +16,4 @@ ws.addLogging('dev') } }) .addStatic() - .start() + .listen() diff --git a/extend/live-reload/server.js b/extend/live-reload/server.js index c79fc28..9f2a00e 100644 --- a/extend/live-reload/server.js +++ b/extend/live-reload/server.js @@ -6,4 +6,4 @@ const ws = new Cli() ws.addLogging('dev') .add({ middleware: liveReload }) .addStatic() - .start() + .listen() diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 7351e95..d914464 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -11,19 +11,23 @@ const debug = require('./debug') const tool = new CommandLineTool() class LocalWebServer extends MiddlewareStack { - getApplication () { + _init (options) { + this.options = this.options || Object.assign(options || {}, collectUserOptions(this.getOptionDefinitions())) + } + getApplication (options) { + this._init(options) const Koa = require('koa') const app = new Koa() app.use(this.compose(this.options)) return app } - getServer () { - const options = this.options + getServer (options) { + const app = this.getApplication(options) + options = this.options let key = options.key let cert = options.cert - const app = this.getApplication() app.on('error', err => { if (options['log-format']) { console.error(ansi.format(err.message, 'red')) @@ -53,9 +57,9 @@ class LocalWebServer extends MiddlewareStack { return server } - start () { - const options = collectOptions(this.getOptionDefinitions()) - this.options = options + listen (options, callback) { + this._init(options) + options = this.options if (options.verbose) { debug.setLevel(1) @@ -68,21 +72,25 @@ class LocalWebServer extends MiddlewareStack { tool.stop(pkg.version) } else { const server = this.getServer() - server.listen(options.port, onServerUp.bind(null, options, server.isHttps)) + const port = options.port || 8000 + server.listen(port, () => { + onServerUp(port, options.directory, server.isHttps) + if (callback) callback() + }) return server } } } -function onServerUp (options, isHttps) { +function onServerUp (port, directory, isHttps) { const ipList = getIPList(isHttps) - .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.port}}`) + .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`) .join(', ') console.error(ansi.format( - path.resolve(options.directory) === process.cwd() + path.resolve(directory || '') === process.cwd() ? `serving at ${ipList}` - : `serving [underline]{${options.directory}} at ${ipList}` + : `serving [underline]{${directory}} at ${ipList}` )) } @@ -101,7 +109,7 @@ function getIPList (isHttps) { /** * Return default, stored and command-line options combined */ -function collectOptions (mwOptionDefinitions) { +function collectUserOptions (mwOptionDefinitions) { const loadConfig = require('config-master') const stored = loadConfig('local-web-server') const cli = require('../lib/cli-data') @@ -110,24 +118,11 @@ function collectOptions (mwOptionDefinitions) { const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions)) let cliOptions = tool.getOptions(definitions, cli.usage(definitions)) - /* override built-in defaults with stored config and then command line options */ - const options = Object.assign({ - port: 8000, - directory: process.cwd() - }, stored, cliOptions.server, cliOptions.middleware, cliOptions.misc) + /* override stored config with command line options */ + const options = Object.assign(stored, cliOptions.server, cliOptions.middleware, cliOptions.misc) // console.error(require('util').inspect(options, { depth: 3, colors: true })) - - validateOptions(options) return options } -function validateOptions (options) { - if (!t.isNumber(options.port)) { - tool.printError('--port must be numeric') - console.error(tool.usage) - tool.halt() - } -} - module.exports = LocalWebServer diff --git a/lib/middleware-stack.js b/lib/middleware-stack.js index e1103e5..e918f41 100644 --- a/lib/middleware-stack.js +++ b/lib/middleware-stack.js @@ -246,9 +246,9 @@ class MiddlewareStack extends Array { description: 'Root directory, defaults to the current directory.' }, middleware: function (cliOptions) { + /* update global cliOptions */ cliOptions.directory = cliOptions.directory || root || process.cwd() options = Object.assign({ hidden: true }, options) - // console.log(root, options, cliOptions) if (cliOptions.directory) { const serve = require('koa-static') return serve(cliOptions.directory, options) diff --git a/package.json b/package.json index 4440fc8..87da531 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "dependencies": { "ansi-escape-sequences": "^2.2.2", "array-back": "^1.0.3", - "command-line-tool": "75lb/command-line-tool", + "command-line-tool": "~0.3.0", "config-master": "^2.0.2", "http-proxy": "^1.13.3", "kcors": "^1.2.1", diff --git a/test/fixture/rewrite/one.html b/test/proxy/file.txt similarity index 100% rename from test/fixture/rewrite/one.html rename to test/proxy/file.txt diff --git a/test/proxy/one.html b/test/proxy/one.html new file mode 100644 index 0000000..5626abf --- /dev/null +++ b/test/proxy/one.html @@ -0,0 +1 @@ +one diff --git a/test/proxy/rewrite-proxy.js b/test/proxy/rewrite-proxy.js new file mode 100644 index 0000000..0085174 --- /dev/null +++ b/test/proxy/rewrite-proxy.js @@ -0,0 +1,84 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const http = require('http') + +function checkResponse (t, status, body) { + return function (response) { + if (status) t.strictEqual(response.res.statusCode, status) + if (body) t.ok(body.test(response.data), 'correct data') + } +} + +function fail (t) { + return function (err) { + t.fail('failed: ' + err.stack) + } +} + +test('rewrite: proxy', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addRewrite([ + { from: '/test/*', to: 'http://registry.npmjs.org/$1' } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test/') + .then(checkResponse(t, 200, /db_name/)) + .then(server.close.bind(server)) + .catch(fail(t)) + }) +}) + +test('rewrite: proxy, POST', function (t) { + t.plan(1) + const ws = new LocalWebServer() + ws.addRewrite([ + { from: '/test/*', to: 'http://registry.npmjs.org/' } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test/', { data: {} }) + .then(checkResponse(t, 405)) + .then(server.close.bind(server)) + .catch(fail(t)) + }) +}) + +test('rewrite: proxy, two url tokens', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addRewrite([ + { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/command-line-args/1.0.0') + .then(checkResponse(t, 200, /command-line-args/)) + .then(server.close.bind(server)) + .catch(fail(t)) + }) +}) + +test('rewrite: proxy with port', function (t) { + t.plan(2) + const ws1 = new LocalWebServer() + ws1.addStatic(__dirname) + + const ws2 = new LocalWebServer() + ws2.addRewrite([ + { from: '/test/*', to: 'http://localhost:9000/$1' } + ]) + const server1 = ws1.getServer() + const server2 = ws2.getServer() + server1.listen(9000, () => { + server2.listen(8100, () => { + request('http://localhost:8100/test/file.txt') + .then(checkResponse(t, 200, /one/)) + .then(server1.close.bind(server1)) + .then(server2.close.bind(server2)) + }) + }) +}) diff --git a/test/rewrite-proxy.js b/test/rewrite-proxy.js deleted file mode 100644 index 3e884e2..0000000 --- a/test/rewrite-proxy.js +++ /dev/null @@ -1,89 +0,0 @@ -'use strict' -const test = require('tape') -const request = require('req-then') -const localWebServer = require('../') -const http = require('http') - -function launchServer (app, options) { - options = options || {} - const path = `http://localhost:8100${options.path || '/'}` - const server = http.createServer(app.callback()) - return server.listen(options.port || 8100, () => { - const req = request(path, options.reqOptions) - if (options.onSuccess) req.then(options.onSuccess) - if (!options.leaveOpen) req.then(() => server.close()) - req.catch(err => console.error('LAUNCH ERROR', err.stack)) - }) -} - -function checkResponse (t, status, body) { - return function (response) { - if (status) t.strictEqual(response.res.statusCode, status) - if (body) t.ok(body.test(response.data)) - } -} - -test('rewrite: proxy', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/rewrite' }, - rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ] - }) - launchServer(app, { path: '/test/', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/db_name/.test(response.data)) - }}) -}) - -test('rewrite: proxy, POST', function (t) { - t.plan(1) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/rewrite' }, - rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/' } ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test/', { data: {} }) - .then(checkResponse(t, 405)) - .then(server.close.bind(server)) - }) -}) - -test('rewrite: proxy, two url tokens', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ] - }) - launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/command-line-args/.test(response.data)) - }}) -}) - -test('rewrite: proxy with port', function (t) { - t.plan(2) - const one = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/one' } - }) - const two = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/spa' }, - rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ] - }) - const server1 = http.createServer(one.callback()) - const server2 = http.createServer(two.callback()) - server1.listen(9000, () => { - server2.listen(8100, () => { - request('http://localhost:8100/test/file.txt').then(response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/one/.test(response.data)) - server1.close() - server2.close() - }) - }) - }) -})