create-test-server
Advanced tools
Comparing version 2.4.0 to 3.0.0
{ | ||
"name": "create-test-server", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"description": "Creates a minimal Express server for testing", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -42,3 +42,3 @@ # create-test-server | ||
// You can return a body directly too | ||
server.get('/bar', () => 'foo'); | ||
server.get('/foo', () => 'bar'); | ||
server.get('/foo', 'bar'); | ||
@@ -57,2 +57,4 @@ | ||
You can change body parsing behaviour with the [`bodyParser`](#optionsbodyparser) option. | ||
`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava). | ||
@@ -171,7 +173,9 @@ | ||
Type: `object` | ||
Type: `object | boolean`<br> | ||
Default: `undefined` | ||
Body parser options to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods. | ||
Body parser options object to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods. | ||
If set to `false` then all body parsing middleware will be disabled. | ||
### server | ||
@@ -227,4 +231,16 @@ | ||
The CA certificate to validate the server certificate against.˜ | ||
The CA certificate to validate the server certificate against. | ||
#### server.http | ||
Type: [`http.server`](https://nodejs.org/api/http.html#http_class_http_server) | ||
The underlying HTTP server instance. | ||
#### server.https | ||
Type: [`https.server`](https://nodejs.org/api/https.html#https_class_https_server) | ||
The underlying HTTPS server instance. | ||
#### server.listen() | ||
@@ -231,0 +247,0 @@ |
@@ -10,12 +10,22 @@ 'use strict'; | ||
const createTestServer = opts => createCert(opts && opts.certificate) | ||
const createTestServer = (opts = {}) => createCert(opts.certificate) | ||
.then(keys => { | ||
const app = express(); | ||
const get = app.get.bind(app); | ||
const server = http.createServer(app); | ||
const sslServer = https.createServer(keys, app); | ||
const server = express(); | ||
server.http = http.createServer(server); | ||
server.https = https.createServer(keys, server); | ||
server.caCert = keys.caCert; | ||
server.set('etag', false); | ||
if (opts.bodyParser !== false) { | ||
server.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser))); | ||
server.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts.bodyParser))); | ||
server.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts.bodyParser))); | ||
server.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser))); | ||
} | ||
const send = fn => (req, res, next) => { | ||
const cb = typeof fn === 'function' ? fn(req, res, next) : fn; | ||
new Promise(resolve => resolve(cb)).then(val => { | ||
Promise.resolve(cb).then(val => { | ||
if (val) { | ||
@@ -27,45 +37,36 @@ res.send(val); | ||
app.set('etag', false); | ||
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts && opts.bodyParser))); | ||
app.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts && opts.bodyParser))); | ||
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts && opts.bodyParser))); | ||
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts && opts.bodyParser))); | ||
app.caCert = keys.caCert; | ||
const get = server.get.bind(server); | ||
server.get = function () { | ||
const [path, ...handlers] = [...arguments]; | ||
app.listen = () => Promise.all([ | ||
pify(server.listen.bind(server))().then(() => { | ||
app.port = server.address().port; | ||
app.url = `http://localhost:${app.port}`; | ||
for (const handler of handlers) { | ||
get(path, send(handler)); | ||
} | ||
}; | ||
server.listen = () => Promise.all([ | ||
pify(server.http.listen.bind(server.http))().then(() => { | ||
server.port = server.http.address().port; | ||
server.url = `http://localhost:${server.port}`; | ||
}), | ||
pify(sslServer.listen.bind(sslServer))().then(() => { | ||
app.sslPort = sslServer.address().port; | ||
app.sslUrl = `https://localhost:${app.sslPort}`; | ||
pify(server.https.listen.bind(server.https))().then(() => { | ||
server.sslPort = server.https.address().port; | ||
server.sslUrl = `https://localhost:${server.sslPort}`; | ||
}) | ||
]); | ||
app.close = () => Promise.all([ | ||
pify(server.close.bind(server))().then(() => { | ||
app.port = undefined; | ||
app.url = undefined; | ||
server.close = () => Promise.all([ | ||
pify(server.http.close.bind(server.http))().then(() => { | ||
server.port = undefined; | ||
server.url = undefined; | ||
}), | ||
pify(sslServer.close.bind(sslServer))().then(() => { | ||
app.sslPort = undefined; | ||
app.sslUrl = undefined; | ||
pify(server.https.close.bind(server.https))().then(() => { | ||
server.sslPort = undefined; | ||
server.sslUrl = undefined; | ||
}) | ||
]); | ||
app.get = function () { | ||
// TODO: Use destructuring when targeting Node.js v6 | ||
const args = Array.from(arguments); | ||
const path = args[0]; | ||
const fns = args.slice(1); | ||
for (const fn of fns) { | ||
get(path, send(fn)); | ||
} | ||
}; | ||
return app.listen().then(() => app); | ||
return server.listen().then(() => server); | ||
}); | ||
module.exports = createTestServer; |
@@ -189,2 +189,17 @@ import querystring from 'querystring'; | ||
test('if opts.bodyParser is false body parsing middleware is disabled', async t => { | ||
const server = await createTestServer({ bodyParser: false }); | ||
const text = 'foo'; | ||
server.post('/echo', (req, res) => { | ||
t.deepEqual(req.body, undefined); | ||
res.end(); | ||
}); | ||
await got.post(server.url + '/echo', { | ||
headers: { 'content-type': 'text/plain' }, | ||
body: text | ||
}); | ||
}); | ||
test('support returning body directly', async t => { | ||
@@ -231,1 +246,13 @@ const server = await createTestServer(); | ||
}); | ||
test('raw http and https servers are exposed', async t => { | ||
const server = await createTestServer(); | ||
t.true(server.http.listening); | ||
t.true(server.https.listening); | ||
await server.close(); | ||
t.false(server.http.listening); | ||
t.false(server.https.listening); | ||
}); |
Sorry, the diff of this file is not supported yet
18158
256
267