From fc2548e40e47ae15d2309a7de7a77b1e4f2e68f7 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Fri, 13 Nov 2015 19:55:52 +0000 Subject: [PATCH] blacklist tests --- bin/ws.js | 6 ++-- lib/local-web-server.js | 19 +++++++------ test/fixture/.local-web-server.json | 5 +++- test/test.js | 55 +++++++++++++++++++++++++++---------- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/bin/ws.js b/bin/ws.js index bc3fa10..c753ad2 100755 --- a/bin/ws.js +++ b/bin/ws.js @@ -22,7 +22,8 @@ try { options.stored = loadConfig('local-web-server') options.builtIn = { port: 8000, - directory: process.cwd() + directory: process.cwd(), + blacklist: [] } /* override built-in defaults with stored config and then command line args */ @@ -36,7 +37,8 @@ localWebServer({ serveIndex: { path: options.cli.server.directory, options: { icons: true } }, log: { format: options.cli.server['log-format'] }, compress: options.cli.server.compress, - mime: options.stored.mime + mime: options.stored.mime, + blacklist: options.stored.blacklist.map(regexp => RegExp(regexp, "i")) }).listen(options.cli.server.port, onServerUp) function halt (message) { diff --git a/lib/local-web-server.js b/lib/local-web-server.js index e9ac2cc..5d508f1 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -21,7 +21,8 @@ function getApp (options) { static: {}, serveIndex: {}, log: {}, - compress: false + compress: false, + blacklist: [] }, options) const log = options.log @@ -33,13 +34,15 @@ function getApp (options) { app.use(convert(cors())) /* path blacklist */ - app.use(function pathBlacklist (ctx, next) { - if (/css$/.test(ctx.path)) { - ctx.throw(403, 'FUCK NO.') - } else { - return next() - } - }) + if (options.blacklist.length) { + app.use(function pathBlacklist (ctx, next) { + if (options.blacklist.some(regexp => regexp.test(ctx.path))) { + ctx.throw(403, 'Blacklisted') + } else { + return next() + } + }) + } app.use(convert(conditional())) app.use(convert(etag())) diff --git a/test/fixture/.local-web-server.json b/test/fixture/.local-web-server.json index 611caff..99cbae7 100644 --- a/test/fixture/.local-web-server.json +++ b/test/fixture/.local-web-server.json @@ -1,5 +1,8 @@ { "mime": { "text/plain": [ "php" ] - } + }, + "blacklist": [ + "php$" + ] } diff --git a/test/test.js b/test/test.js index 4346346..8a998ba 100644 --- a/test/test.js +++ b/test/test.js @@ -5,13 +5,14 @@ const localWebServer = require('../') const http = require('http') const PassThrough = require('stream').PassThrough -function launchServer (app, reqOptions, path, onSuccess) { - path = `http://localhost:8100${path || '/'}` +function launchServer (app, options) { + options = options || {} + const path = `http://localhost:8100${options.path || '/'}` const server = http.createServer(app.callback()) - server.listen(8100, () => { - const req = request(path, reqOptions) - if (onSuccess) req.then(onSuccess) - req.then(() => server.close()) + return server.listen(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)) }) } @@ -47,9 +48,9 @@ test('static', function (t) { } } }) - launchServer(app, null, '/', response => { + launchServer(app, { onSuccess: response => { t.strictEqual(response.data, 'test\n') - }) + }}) }) test('serve-index', function (t) { @@ -63,10 +64,10 @@ test('serve-index', function (t) { } } }) - launchServer(app, null, null, response => { + launchServer(app, { onSuccess: response => { t.ok(/listing directory/.test(response.data)) t.ok(/class="icon/.test(response.data)) - }) + }}) }) test('compress', function(t){ @@ -76,9 +77,16 @@ test('compress', function(t){ log: { format: 'none' }, static: { root: __dirname + '/fixture' } }) - launchServer(app, { headers: { 'Accept-Encoding': 'gzip' } }, '/big-file.txt', response => { - t.strictEqual(response.res.headers['content-encoding'], 'gzip') - }) + 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){ @@ -88,7 +96,26 @@ test('mime', function(t){ static: { root: __dirname + '/fixture' }, mime: { 'text/plain': [ 'php' ]} }) - launchServer(app, null, '/something.php', response => { + launchServer(app, { path: '/something.php', onSuccess: response => { t.ok(/text\/plain/.test(response.res.headers['content-type'])) + }}) +}) + +test('blacklist', function (t) { + t.plan(2) + const app = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/fixture' }, + blacklist: [ /php$/, /html$/ ] }) + const server = launchServer(app, { leaveOpen: true }) + request('http://localhost:8100/something.php') + .then(response => { + t.strictEqual(response.res.statusCode, 403) + request('http://localhost:8100/ajax.html') + .then(response => { + t.strictEqual(response.res.statusCode, 403) + server.close() + }) + }) })