From 71b2f4dcb4a988dee0e822107864f6d9a8e7dd2c Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Mon, 20 Jun 2016 01:02:51 +0100 Subject: [PATCH] update tests --- bin/cli.js | 2 +- lib/local-web-server.js | 2 +- lib/middleware-stack.js | 13 +- lib/middleware.js | 2 +- package.json | 2 +- test/common.js | 22 +++ test/{fixture => compress}/big-file.txt | 0 test/compress/compress.js | 22 +++ test/forbid/forbid.js | 21 +++ test/{fixture => }/forbid/one.html | 0 test/{fixture => }/forbid/two.php | 0 test/log/log.js | 25 +++ test/mime/mime.js | 22 +++ test/{fixture => mime}/something.php | 0 test/mock/mock.js | 150 ++++++++++++++++ test/mock/one.html | 1 + test/proxy/rewrite-proxy.js | 29 +-- test/rewrite/one.html | 1 + test/rewrite/rewrite.js | 19 ++ test/serve-index/serve-index.js | 18 ++ test/spa/one.txt | 1 + test/spa/spa.js | 28 +++ test/spa/two.txt | 1 + test/static.js | 33 ---- test/static/file.txt | 1 + test/static/static.js | 18 ++ test/test.js | 305 -------------------------------- 27 files changed, 371 insertions(+), 367 deletions(-) create mode 100644 test/common.js rename test/{fixture => compress}/big-file.txt (100%) create mode 100644 test/compress/compress.js create mode 100644 test/forbid/forbid.js rename test/{fixture => }/forbid/one.html (100%) rename test/{fixture => }/forbid/two.php (100%) create mode 100644 test/log/log.js create mode 100644 test/mime/mime.js rename test/{fixture => mime}/something.php (100%) create mode 100644 test/mock/mock.js create mode 100644 test/mock/one.html create mode 100644 test/rewrite/one.html create mode 100644 test/rewrite/rewrite.js create mode 100644 test/serve-index/serve-index.js create mode 100644 test/spa/one.txt create mode 100644 test/spa/spa.js create mode 100644 test/spa/two.txt delete mode 100644 test/static.js create mode 100644 test/static/file.txt create mode 100644 test/static/static.js delete mode 100644 test/test.js diff --git a/bin/cli.js b/bin/cli.js index 371ba28..d4fbb15 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -9,7 +9,7 @@ ws.addCors() .addBodyParser() .addBlacklist() .addCache() - .addMimeType() + .addMimeOverride() .addCompression() .addLogging() .addMockResponses() diff --git a/lib/local-web-server.js b/lib/local-web-server.js index d914464..2852c2e 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -30,7 +30,7 @@ class LocalWebServer extends MiddlewareStack { app.on('error', err => { if (options['log-format']) { - console.error(ansi.format(err.message, 'red')) + console.error(ansi.format(err.stack, 'red')) } }) diff --git a/lib/middleware-stack.js b/lib/middleware-stack.js index e918f41..fda6a26 100644 --- a/lib/middleware-stack.js +++ b/lib/middleware-stack.js @@ -82,8 +82,7 @@ class MiddlewareStack extends Array { debug('forbid', forbidList.join(', ')) return function blacklist (ctx, next) { if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) { - const http = require('http') - ctx.throw(403, http.STATUS_CODES[403]) + ctx.status = 403 } else { return next() } @@ -115,7 +114,7 @@ class MiddlewareStack extends Array { } /* mime-type overrides */ - addMimeType (mime) { + addMimeOverride (mime) { this.push({ middleware: function (cliOptions) { mime = cliOptions.mime || mime @@ -229,8 +228,12 @@ class MiddlewareStack extends Array { debug('SPA', spa) return _.get('*', function spaMw (ctx, route, next) { const root = path.resolve(cliOptions.directory || process.cwd()) - debug(`SPA request. Route: ${route}, isAsset: ${assetTest.test(route)}`) - return send(ctx, assetTest.test(route) ? route : spa, { root: root }).then(next) + if (ctx.accepts('text/html') && !assetTest.test(route)) { + debug(`SPA request. Route: ${route}, isAsset: ${assetTest.test(route)}`) + return send(ctx, spa, { root: root }).then(next) + } else { + return send(ctx, route, { root: root }).then(next) + } }) } } diff --git a/lib/middleware.js b/lib/middleware.js index 7de477f..964b2b2 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -83,7 +83,7 @@ function mockResponses (route, targets) { target = targets.find(target => !target.request) } - debug(`mock path: ${ctx.path} target: ${target.name || 'unnamed'}`) + debug(`mock path: ${ctx.path} target: ${target && target.name || 'unnamed'}`) if (target) { if (t.isFunction(target.response)) { diff --git a/package.json b/package.json index 87da531..f348e49 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "node": ">=4.0.0" }, "scripts": { - "test": "tape test/*.js", + "test": "tape test/*/*.js", "docs": "jsdoc2md -t jsdoc2md/README.hbs -p list lib/*.js > README.md; echo", "cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | coveralls && rm -rf coverage; echo" }, diff --git a/test/common.js b/test/common.js new file mode 100644 index 0000000..43d4ff1 --- /dev/null +++ b/test/common.js @@ -0,0 +1,22 @@ +'use strict' +const arrayify = require('array-back') + +exports.checkResponse = checkResponse +exports.fail = fail + +function checkResponse (t, status, bodyTests) { + return function (response) { + if (status) t.strictEqual(response.res.statusCode, status) + if (bodyTests) { + arrayify(bodyTests).forEach(body => { + t.ok(body.test(response.data), 'correct data') + }) + } + } +} + +function fail (t) { + return function (err) { + t.fail('failed: ' + err.stack) + } +} diff --git a/test/fixture/big-file.txt b/test/compress/big-file.txt similarity index 100% rename from test/fixture/big-file.txt rename to test/compress/big-file.txt diff --git a/test/compress/compress.js b/test/compress/compress.js new file mode 100644 index 0000000..84f057b --- /dev/null +++ b/test/compress/compress.js @@ -0,0 +1,22 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('compress', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addCompression(true) + ws.addStatic(__dirname) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/big-file.txt', { headers: { 'Accept-Encoding': 'gzip' } }) + .then(response => { + t.strictEqual(response.res.statusCode, 200) + t.strictEqual(response.res.headers['content-encoding'], 'gzip') + }) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/forbid/forbid.js b/test/forbid/forbid.js new file mode 100644 index 0000000..c2d4910 --- /dev/null +++ b/test/forbid/forbid.js @@ -0,0 +1,21 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('forbid', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addBlacklist([ '*.php', '*.html' ]) + ws.addStatic(__dirname) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/two.php') + .then(c.checkResponse(t, 403)) + .then(() => request('http://localhost:8100/one.html')) + .then(c.checkResponse(t, 403)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/fixture/forbid/one.html b/test/forbid/one.html similarity index 100% rename from test/fixture/forbid/one.html rename to test/forbid/one.html diff --git a/test/fixture/forbid/two.php b/test/forbid/two.php similarity index 100% rename from test/fixture/forbid/two.php rename to test/forbid/two.php diff --git a/test/log/log.js b/test/log/log.js new file mode 100644 index 0000000..a90e2f6 --- /dev/null +++ b/test/log/log.js @@ -0,0 +1,25 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('logging', function (t) { + t.plan(2) + const ws = new LocalWebServer() + + const stream = require('stream').PassThrough() + stream.on('readable', () => { + let chunk = stream.read() + if (chunk) t.ok(/GET/.test(chunk.toString())) + }) + + ws.addLogging('common', { stream: stream }) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/') + .then(c.checkResponse(t, 404)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/mime/mime.js b/test/mime/mime.js new file mode 100644 index 0000000..bb8cc44 --- /dev/null +++ b/test/mime/mime.js @@ -0,0 +1,22 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('mime override', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addMimeOverride({ 'text/plain': [ 'php' ] }) + ws.addStatic(__dirname) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/something.php') + .then(response => { + t.strictEqual(response.res.statusCode, 200) + t.ok(/text\/plain/.test(response.res.headers['content-type'])) + }) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/fixture/something.php b/test/mime/something.php similarity index 100% rename from test/fixture/something.php rename to test/mime/something.php diff --git a/test/mock/mock.js b/test/mock/mock.js new file mode 100644 index 0000000..28dccac --- /dev/null +++ b/test/mock/mock.js @@ -0,0 +1,150 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('mock: simple response', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addMockResponses([ + { route: '/test', response: { body: 'test' } } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test') + .then(c.checkResponse(t, 200, /test/)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) + +test('mock: method request filter', function (t) { + t.plan(3) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test', + request: { method: 'POST' }, + response: { body: 'test' } + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test') + .then(c.checkResponse(t, 404)) + .then(() => request('http://localhost:8100/test', { data: 'something' })) + .then(c.checkResponse(t, 200, /test/)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) + +test('mock: accepts request filter', function (t) { + t.plan(3) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test', + request: { accepts: 'text' }, + response: { body: 'test' } + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test', { headers: { Accept: '*/json' } }) + .then(c.checkResponse(t, 404)) + .then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } })) + .then(c.checkResponse(t, 200, /test/)) + .then(server.close.bind(server)) + }) +}) + +test('mock: responses array', function (t) { + t.plan(4) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test', + responses: [ + { request: { method: 'GET' }, response: { body: 'get' } }, + { request: { method: 'POST' }, response: { body: 'post' } } + ] + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test') + .then(c.checkResponse(t, 200, /get/)) + .then(() => request('http://localhost:8100/test', { method: 'POST' })) + .then(c.checkResponse(t, 200, /post/)) + .then(server.close.bind(server)) + }) +}) + +test('mock: response function', function (t) { + t.plan(4) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test', + responses: [ + { request: { method: 'GET' }, response: ctx => ctx.body = 'get' }, + { request: { method: 'POST' }, response: ctx => ctx.body = 'post' } + ] + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test') + .then(c.checkResponse(t, 200, /get/)) + .then(() => request('http://localhost:8100/test', { method: 'POST' })) + .then(c.checkResponse(t, 200, /post/)) + .then(server.close.bind(server)) + }) +}) + +test('mock: response function args', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test/:one', + responses: [ + { request: { method: 'GET' }, response: (ctx, one) => ctx.body = one } + ] + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test/yeah') + .then(c.checkResponse(t, 200, /yeah/)) + .then(server.close.bind(server)) + }) +}) + +test('mock: async response function', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addMockResponses([ + { + route: '/test', + responses: { + response: function (ctx) { + return new Promise((resolve, reject) => { + setTimeout(() => { + ctx.body = 'test' + resolve() + }, 10) + }) + } + } + } + ]) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/test') + .then(c.checkResponse(t, 200, /test/)) + .then(server.close.bind(server)) + }) +}) diff --git a/test/mock/one.html b/test/mock/one.html new file mode 100644 index 0000000..5626abf --- /dev/null +++ b/test/mock/one.html @@ -0,0 +1 @@ +one diff --git a/test/proxy/rewrite-proxy.js b/test/proxy/rewrite-proxy.js index 0085174..3fea168 100644 --- a/test/proxy/rewrite-proxy.js +++ b/test/proxy/rewrite-proxy.js @@ -3,19 +3,7 @@ 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) - } -} +const c = require('../common') test('rewrite: proxy', function (t) { t.plan(2) @@ -26,9 +14,9 @@ test('rewrite: proxy', function (t) { const server = ws.getServer() server.listen(8100, () => { request('http://localhost:8100/test/') - .then(checkResponse(t, 200, /db_name/)) + .then(c.checkResponse(t, 200, /db_name/)) .then(server.close.bind(server)) - .catch(fail(t)) + .catch(c.fail(t)) }) }) @@ -41,9 +29,9 @@ test('rewrite: proxy, POST', function (t) { const server = ws.getServer() server.listen(8100, () => { request('http://localhost:8100/test/', { data: {} }) - .then(checkResponse(t, 405)) + .then(c.checkResponse(t, 405)) .then(server.close.bind(server)) - .catch(fail(t)) + .catch(c.fail(t)) }) }) @@ -56,9 +44,9 @@ test('rewrite: proxy, two url tokens', function (t) { 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(c.checkResponse(t, 200, /command-line-args/)) .then(server.close.bind(server)) - .catch(fail(t)) + .catch(c.fail(t)) }) }) @@ -76,9 +64,10 @@ test('rewrite: proxy with port', function (t) { server1.listen(9000, () => { server2.listen(8100, () => { request('http://localhost:8100/test/file.txt') - .then(checkResponse(t, 200, /one/)) + .then(c.checkResponse(t, 200, /one/)) .then(server1.close.bind(server1)) .then(server2.close.bind(server2)) + .catch(c.fail(t)) }) }) }) diff --git a/test/rewrite/one.html b/test/rewrite/one.html new file mode 100644 index 0000000..5626abf --- /dev/null +++ b/test/rewrite/one.html @@ -0,0 +1 @@ +one diff --git a/test/rewrite/rewrite.js b/test/rewrite/rewrite.js new file mode 100644 index 0000000..cdce7e0 --- /dev/null +++ b/test/rewrite/rewrite.js @@ -0,0 +1,19 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('rewrite local', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addRewrite([ { from: '/two.html', to: '/one.html' } ]) + ws.addStatic(__dirname) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/two.html') + .then(c.checkResponse(t, 200, /one/)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/serve-index/serve-index.js b/test/serve-index/serve-index.js new file mode 100644 index 0000000..3ca09d6 --- /dev/null +++ b/test/serve-index/serve-index.js @@ -0,0 +1,18 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('static', function (t) { + t.plan(3) + const ws = new LocalWebServer() + ws.addIndex(__dirname, { icons: true }) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/') + .then(c.checkResponse(t, 200, [ /listing directory/, /class="icon/ ])) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/spa/one.txt b/test/spa/one.txt new file mode 100644 index 0000000..5626abf --- /dev/null +++ b/test/spa/one.txt @@ -0,0 +1 @@ +one diff --git a/test/spa/spa.js b/test/spa/spa.js new file mode 100644 index 0000000..38dbc50 --- /dev/null +++ b/test/spa/spa.js @@ -0,0 +1,28 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('spa', function (t) { + t.plan(6) + const ws = new LocalWebServer() + ws.addSpa('one.txt') + ws.addStatic(__dirname) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/asdf', { headers: { accept: 'text/html' } }) + .then(c.checkResponse(t, 200, /one/)) + /* html requests for missing files with extensions do not redirect to spa */ + .then(() => request('http://localhost:8100/asdf.txt', { headers: { accept: 'text/html' } })) + .then(c.checkResponse(t, 404)) + /* existing static file */ + .then(() => request('http://localhost:8100/two.txt')) + .then(c.checkResponse(t, 200, /two/)) + /* not a text/html request - does not redirect to spa */ + .then(() => request('http://localhost:8100/asdf', { headers: { accept: 'application/json' } })) + .then(c.checkResponse(t, 404)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/spa/two.txt b/test/spa/two.txt new file mode 100644 index 0000000..f719efd --- /dev/null +++ b/test/spa/two.txt @@ -0,0 +1 @@ +two diff --git a/test/static.js b/test/static.js deleted file mode 100644 index 837d684..0000000 --- a/test/static.js +++ /dev/null @@ -1,33 +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)) - }) -} - -test('static', function (t) { - t.plan(1) - const app = localWebServer({ - log: { format: 'none' }, - static: { - root: __dirname + '/fixture', - options: { - index: 'file.txt' - } - } - }) - launchServer(app, { onSuccess: response => { - t.ok(/test/.test(response.data)) - }}) -}) diff --git a/test/static/file.txt b/test/static/file.txt new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/test/static/file.txt @@ -0,0 +1 @@ +test diff --git a/test/static/static.js b/test/static/static.js new file mode 100644 index 0000000..92cf1f8 --- /dev/null +++ b/test/static/static.js @@ -0,0 +1,18 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const LocalWebServer = require('../../') +const c = require('../common') + +test('static', function (t) { + t.plan(2) + const ws = new LocalWebServer() + ws.addStatic(__dirname, { index: 'file.txt' }) + const server = ws.getServer() + server.listen(8100, () => { + request('http://localhost:8100/') + .then(c.checkResponse(t, 200, /test/)) + .then(server.close.bind(server)) + .catch(c.fail(t)) + }) +}) diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 838605d..0000000 --- a/test/test.js +++ /dev/null @@ -1,305 +0,0 @@ -'use strict' -const test = require('tape') -const request = require('req-then') -const localWebServer = require('../') -const http = require('http') -const PassThrough = require('stream').PassThrough - -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('serve-index', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - serveIndex: { - path: __dirname + '/fixture', - options: { - icons: true - } - } - }) - launchServer(app, { onSuccess: response => { - t.ok(/listing directory/.test(response.data)) - t.ok(/class="icon/.test(response.data)) - }}) -}) - -test('single page app', function (t) { - t.plan(6) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/spa' }, - spa: 'one.txt' - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - /* text/html requests for missing files redirect to spa */ - request('http://localhost:8100/asdf', { headers: { accept: 'text/html' } }) - .then(checkResponse(t, 200, /one/)) - /* html requests for missing files with extensions do not redirect to spa */ - .then(() => request('http://localhost:8100/asdf.txt', { headers: { accept: 'text/html' } })) - .then(checkResponse(t, 404)) - /* existing static file */ - .then(() => request('http://localhost:8100/two.txt')) - .then(checkResponse(t, 200, /two/)) - /* not a text/html request - does not redirect to spa */ - .then(() => request('http://localhost:8100/asdf')) - .then(checkResponse(t, 404)) - .then(server.close.bind(server)) - }) -}) - -test('log: common', function (t) { - t.plan(1) - const stream = PassThrough() - - stream.on('readable', () => { - let chunk = stream.read() - if (chunk) t.ok(/GET/.test(chunk.toString())) - }) - - const app = localWebServer({ - log: { - format: 'common', - options: { - stream: stream - } - } - }) - launchServer(app) -}) - -test('compress', function (t) { - t.plan(1) - const app = localWebServer({ - compress: true, - log: { format: 'none' }, - static: { root: __dirname + '/fixture' } - }) - launchServer( - app, - { - reqOptions: { headers: { 'Accept-Encoding': 'gzip' } }, - path: '/big-file.txt', - onSuccess: response => { - t.strictEqual(response.res.headers['content-encoding'], 'gzip') - } - } - ) -}) - -test('mime', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture' }, - mime: { 'text/plain': [ 'php' ] } - }) - launchServer(app, { path: '/something.php', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/text\/plain/.test(response.res.headers['content-type'])) - }}) -}) - -test('forbid', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/forbid' }, - forbid: [ '*.php', '*.html' ] - }) - const server = launchServer(app, { leaveOpen: true }) - request('http://localhost:8100/two.php') - .then(response => { - t.strictEqual(response.res.statusCode, 403) - request('http://localhost:8100/one.html') - .then(response => { - t.strictEqual(response.res.statusCode, 403) - server.close() - }) - }) -}) - -test('rewrite: local', function (t) { - t.plan(1) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/rewrite' }, - rewrite: [ { from: '/two.html', to: '/one.html' } ] - }) - launchServer(app, { path: '/two.html', onSuccess: response => { - t.ok(/one/.test(response.data)) - }}) -}) - -test('mock: simple response', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { route: '/test', response: { body: 'test' } } - ] - }) - launchServer(app, { path: '/test', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/test/.test(response.data)) - }}) -}) - -test('mock: method request filter', function (t) { - t.plan(3) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test', - request: { method: 'POST' }, - response: { body: 'test' } - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test') - .then(checkResponse(t, 404)) - .then(() => request('http://localhost:8100/test', { data: 'something' })) - .then(checkResponse(t, 200, /test/)) - .then(server.close.bind(server)) - }) -}) - -test('mock: accepts request filter', function (t) { - t.plan(3) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test', - request: { accepts: 'text' }, - response: { body: 'test' } - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test', { headers: { Accept: '*/json' } }) - .then(checkResponse(t, 404)) - .then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } })) - .then(checkResponse(t, 200, /test/)) - .then(server.close.bind(server)) - }) -}) - -test('mock: responses array', function (t) { - t.plan(4) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test', - responses: [ - { request: { method: 'GET' }, response: { body: 'get' } }, - { request: { method: 'POST' }, response: { body: 'post' } } - ] - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test') - .then(checkResponse(t, 200, /get/)) - .then(() => request('http://localhost:8100/test', { method: 'POST' })) - .then(checkResponse(t, 200, /post/)) - .then(server.close.bind(server)) - }) -}) - -test('mock: response function', function (t) { - t.plan(4) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test', - responses: [ - { request: { method: 'GET' }, response: ctx => ctx.body = 'get' }, - { request: { method: 'POST' }, response: ctx => ctx.body = 'post' } - ] - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test') - .then(checkResponse(t, 200, /get/)) - .then(() => request('http://localhost:8100/test', { method: 'POST' })) - .then(checkResponse(t, 200, /post/)) - .then(server.close.bind(server)) - }) -}) - -test('mock: response function args', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test/:one', - responses: [ - { request: { method: 'GET' }, response: (ctx, one) => ctx.body = one } - ] - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test/yeah') - .then(checkResponse(t, 200, /yeah/)) - .then(server.close.bind(server)) - }) -}) - -test('mock: async response function', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - mocks: [ - { - route: '/test', - responses: { - response: function (ctx) { - return new Promise((resolve, reject) => { - setTimeout(() => { - ctx.body = 'test' - resolve() - }, 10) - }) - } - } - } - ] - }) - const server = http.createServer(app.callback()) - server.listen(8100, () => { - request('http://localhost:8100/test') - .then(checkResponse(t, 200, /test/)) - .then(server.close.bind(server)) - }) -})