Files
hiring-test-one/test/test.js

204 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-11-08 22:09:07 +00:00
'use strict'
const test = require('tape')
const request = require('req-then')
const localWebServer = require('../')
const http = require('http')
2015-11-10 21:50:56 +00:00
const PassThrough = require('stream').PassThrough
2015-11-08 22:09:07 +00:00
2015-11-13 19:55:52 +00:00
function launchServer (app, options) {
options = options || {}
const path = `http://localhost:8100${options.path || '/'}`
2015-11-13 11:26:02 +00:00
const server = http.createServer(app.callback())
return server.listen(options.port || 8100, () => {
2015-11-13 19:55:52 +00:00
const req = request(path, options.reqOptions)
if (options.onSuccess) req.then(options.onSuccess)
if (!options.leaveOpen) req.then(() => server.close())
2015-11-13 11:26:02 +00:00
req.catch(err => console.error('LAUNCH ERROR', err.stack))
})
}
2015-11-08 22:09:07 +00:00
test('static', function (t) {
t.plan(1)
const app = localWebServer({
log: { format: 'none' },
2015-11-08 22:09:07 +00:00
static: {
2015-11-13 11:42:16 +00:00
root: __dirname + '/fixture',
2015-11-08 22:09:07 +00:00
options: {
index: 'file.txt'
}
}
})
2015-11-13 19:55:52 +00:00
launchServer(app, { onSuccess: response => {
t.ok(/test/.test(response.data))
2015-11-13 19:55:52 +00:00
}})
2015-11-08 22:09:07 +00:00
})
test('serve-index', function (t) {
t.plan(2)
const app = localWebServer({
log: { format: 'none' },
2015-11-08 22:09:07 +00:00
serveIndex: {
2015-11-13 11:42:16 +00:00
path: __dirname + '/fixture',
2015-11-08 22:09:07 +00:00
options: {
icons: true
}
}
})
2015-11-13 19:55:52 +00:00
launchServer(app, { onSuccess: response => {
t.ok(/listing directory/.test(response.data))
t.ok(/class="icon/.test(response.data))
2015-11-13 19:55:52 +00:00
}})
2015-11-08 22:09:07 +00:00
})
2015-11-10 21:50:56 +00:00
2015-11-19 19:27:36 +00:00
test('single page app', function (t) {
2015-11-17 16:13:57 +00:00
t.plan(4)
const app = localWebServer({
log: { format: 'none' },
static: { root: __dirname + '/fixture/spa' },
spa: 'one.txt'
})
const server = launchServer(app, { leaveOpen: true })
request('http://localhost:8100/test').then(response => {
t.strictEqual(response.res.statusCode, 200)
t.ok(/one/.test(response.data))
2015-11-17 16:13:57 +00:00
request('http://localhost:8100/two.txt').then(response => {
t.strictEqual(response.res.statusCode, 200)
t.ok(/two/.test(response.data))
2015-11-17 16:13:57 +00:00
server.close()
})
})
})
2015-11-17 15:13:22 +00:00
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)
})
2015-11-19 19:27:36 +00:00
test('compress', function (t) {
2015-11-10 21:50:56 +00:00
t.plan(1)
const app = localWebServer({
compress: true,
log: { format: 'none' },
2015-11-13 11:42:16 +00:00
static: { root: __dirname + '/fixture' }
})
2015-11-13 19:55:52 +00:00
launchServer(
app,
{
reqOptions: { headers: { 'Accept-Encoding': 'gzip' } },
path: '/big-file.txt',
onSuccess: response => {
t.strictEqual(response.res.headers['content-encoding'], 'gzip')
}
}
)
2015-11-12 23:02:38 +00:00
})
2015-11-19 19:27:36 +00:00
test('mime', function (t) {
2015-11-17 15:13:22 +00:00
t.plan(2)
2015-11-13 11:26:02 +00:00
const app = localWebServer({
log: { format: 'none' },
2015-11-13 11:42:16 +00:00
static: { root: __dirname + '/fixture' },
2015-11-19 19:27:36 +00:00
mime: { 'text/plain': [ 'php' ] }
})
2015-11-13 19:55:52 +00:00
launchServer(app, { path: '/something.php', onSuccess: response => {
2015-11-17 15:13:22 +00:00
t.strictEqual(response.res.statusCode, 200)
2015-11-13 11:26:02 +00:00
t.ok(/text\/plain/.test(response.res.headers['content-type']))
2015-11-13 19:55:52 +00:00
}})
})
2015-11-15 23:52:13 +00:00
test('forbid', function (t) {
2015-11-13 19:55:52 +00:00
t.plan(2)
const app = localWebServer({
log: { format: 'none' },
2015-11-17 15:13:22 +00:00
static: { root: __dirname + '/fixture/forbid' },
forbid: [ '*.php', '*.html' ]
2015-11-13 11:26:02 +00:00
})
2015-11-13 19:55:52 +00:00
const server = launchServer(app, { leaveOpen: true })
2015-11-17 15:13:22 +00:00
request('http://localhost:8100/two.php')
2015-11-13 19:55:52 +00:00
.then(response => {
t.strictEqual(response.res.statusCode, 403)
2015-11-17 15:13:22 +00:00
request('http://localhost:8100/one.html')
2015-11-13 19:55:52 +00:00
.then(response => {
t.strictEqual(response.res.statusCode, 403)
server.close()
})
})
2015-11-13 11:26:02 +00:00
})
2015-11-15 11:59:45 +00:00
2015-11-19 19:27:36 +00:00
test('rewrite: local', function (t) {
2015-11-15 11:59:45 +00:00
t.plan(1)
const app = localWebServer({
log: { format: 'none' },
2015-11-17 15:13:22 +00:00
static: { root: __dirname + '/fixture/rewrite' },
2015-11-19 19:27:36 +00:00
rewrite: [ { from: '/two.html', to: '/one.html' } ]
2015-11-15 11:59:45 +00:00
})
2015-11-17 15:13:22 +00:00
launchServer(app, { path: '/two.html', onSuccess: response => {
t.ok(/one/.test(response.data))
2015-11-15 11:59:45 +00:00
}})
})
2015-11-19 19:27:36 +00:00
test('rewrite: proxy', function (t) {
2015-11-17 15:13:22 +00:00
t.plan(2)
2015-11-15 11:59:45 +00:00
const app = localWebServer({
log: { format: 'none' },
2015-11-17 15:13:22 +00:00
static: { root: __dirname + '/fixture/rewrite' },
2015-11-19 19:27:36 +00:00
rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ]
2015-11-15 11:59:45 +00:00
})
2015-11-17 15:13:22 +00:00
launchServer(app, { path: '/test/', onSuccess: response => {
t.strictEqual(response.res.statusCode, 200)
t.ok(/db_name/.test(response.data))
2015-11-15 11:59:45 +00:00
}})
})
2015-11-22 19:38:08 +00:00
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))
}})
})
2015-11-19 19:27:36 +00:00
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' },
2015-11-19 19:27:36 +00:00
rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ]
})
const server1 = http.createServer(one.callback())
const server2 = http.createServer(two.callback())
server1.listen(9000, () => {
2015-11-19 19:27:36 +00:00
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()
})
})
})
})