Browse Source

blacklist tests

master
Lloyd Brookes 9 years ago
parent
commit
fc2548e40e
  1. 6
      bin/ws.js
  2. 9
      lib/local-web-server.js
  3. 5
      test/fixture/.local-web-server.json
  4. 53
      test/test.js

6
bin/ws.js

@ -22,7 +22,8 @@ try {
options.stored = loadConfig('local-web-server') options.stored = loadConfig('local-web-server')
options.builtIn = { options.builtIn = {
port: 8000, port: 8000,
directory: process.cwd()
directory: process.cwd(),
blacklist: []
} }
/* override built-in defaults with stored config and then command line args */ /* 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 } }, serveIndex: { path: options.cli.server.directory, options: { icons: true } },
log: { format: options.cli.server['log-format'] }, log: { format: options.cli.server['log-format'] },
compress: options.cli.server.compress, 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) }).listen(options.cli.server.port, onServerUp)
function halt (message) { function halt (message) {

9
lib/local-web-server.js

@ -21,7 +21,8 @@ function getApp (options) {
static: {}, static: {},
serveIndex: {}, serveIndex: {},
log: {}, log: {},
compress: false
compress: false,
blacklist: []
}, options) }, options)
const log = options.log const log = options.log
@ -33,13 +34,15 @@ function getApp (options) {
app.use(convert(cors())) app.use(convert(cors()))
/* path blacklist */ /* path blacklist */
if (options.blacklist.length) {
app.use(function pathBlacklist (ctx, next) { app.use(function pathBlacklist (ctx, next) {
if (/css$/.test(ctx.path)) {
ctx.throw(403, 'FUCK NO.')
if (options.blacklist.some(regexp => regexp.test(ctx.path))) {
ctx.throw(403, 'Blacklisted')
} else { } else {
return next() return next()
} }
}) })
}
app.use(convert(conditional())) app.use(convert(conditional()))
app.use(convert(etag())) app.use(convert(etag()))

5
test/fixture/.local-web-server.json

@ -1,5 +1,8 @@
{ {
"mime": { "mime": {
"text/plain": [ "php" ] "text/plain": [ "php" ]
}
},
"blacklist": [
"php$"
]
} }

53
test/test.js

@ -5,13 +5,14 @@ const localWebServer = require('../')
const http = require('http') const http = require('http')
const PassThrough = require('stream').PassThrough 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()) 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)) 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') t.strictEqual(response.data, 'test\n')
})
}})
}) })
test('serve-index', function (t) { 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(/listing directory/.test(response.data))
t.ok(/class="icon/.test(response.data)) t.ok(/class="icon/.test(response.data))
})
}})
}) })
test('compress', function(t){ test('compress', function(t){
@ -76,9 +77,16 @@ test('compress', function(t){
log: { format: 'none' }, log: { format: 'none' },
static: { root: __dirname + '/fixture' } static: { root: __dirname + '/fixture' }
}) })
launchServer(app, { headers: { 'Accept-Encoding': 'gzip' } }, '/big-file.txt', response => {
launchServer(
app,
{
reqOptions: { headers: { 'Accept-Encoding': 'gzip' } },
path: '/big-file.txt',
onSuccess: response => {
t.strictEqual(response.res.headers['content-encoding'], 'gzip') t.strictEqual(response.res.headers['content-encoding'], 'gzip')
})
}
}
)
}) })
test('mime', function(t){ test('mime', function(t){
@ -88,7 +96,26 @@ test('mime', function(t){
static: { root: __dirname + '/fixture' }, static: { root: __dirname + '/fixture' },
mime: { 'text/plain': [ 'php' ]} 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'])) 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()
})
}) })
}) })
Loading…
Cancel
Save