create-test-server
Advanced tools
Comparing version 2.1.1 to 2.2.0
{ | ||
"name": "create-test-server", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Creates a minimal Express server for testing", | ||
@@ -44,6 +44,6 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"ava": "^0.22.0", | ||
"ava": "^0.25.0", | ||
"coveralls": "^3.0.0", | ||
"eslint-config-xo-lukechilds": "^1.0.0", | ||
"got": "^7.0.0", | ||
"got": "^8.0.0", | ||
"nyc": "^11.0.2", | ||
@@ -50,0 +50,0 @@ "xo": "^0.19.0" |
@@ -41,2 +41,6 @@ # create-test-server | ||
// You can return a body directly too | ||
server.get('/foo', 'foo'); | ||
server.get('/foo', () => 'foo'); | ||
// server.url + '/foo' and server.sslUrl + '/foo' will respond with 'bar' | ||
@@ -43,0 +47,0 @@ }); |
@@ -12,5 +12,15 @@ 'use strict'; | ||
const app = express(); | ||
const get = app.get.bind(app); | ||
const server = http.createServer(app); | ||
const sslServer = https.createServer(keys, app); | ||
const send = fn => (req, res, next) => { | ||
const cb = typeof fn === 'function' ? fn(req, res, next) : fn; | ||
new Promise(resolve => resolve(cb)).then(val => { | ||
if (val) { | ||
res.send(val); | ||
} | ||
}); | ||
}; | ||
app.set('etag', false); | ||
@@ -42,2 +52,13 @@ | ||
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); | ||
@@ -44,0 +65,0 @@ }); |
@@ -94,1 +94,43 @@ import test from 'ava'; | ||
}); | ||
test('support returning body directly', async t => { | ||
const server = await createTestServer(); | ||
server.get('/foo', () => 'bar'); | ||
server.get('/bar', () => ({ foo: 'bar' })); | ||
server.get('/async', () => Promise.resolve('bar')); | ||
const bodyString = (await got(server.url + '/foo')).body; | ||
const bodyJson = (await got(server.url + '/bar', { json: true })).body; | ||
const bodyAsync = (await got(server.url + '/async')).body; | ||
t.deepEqual(bodyString, 'bar'); | ||
t.deepEqual(bodyJson, { foo: 'bar' }); | ||
t.deepEqual(bodyAsync, 'bar'); | ||
}); | ||
test('support returning body directly without wrapping in function', async t => { | ||
const server = await createTestServer(); | ||
server.get('/foo', 'bar'); | ||
server.get('/bar', ({ foo: 'bar' })); | ||
server.get('/async', Promise.resolve('bar')); | ||
const bodyString = (await got(server.url + '/foo')).body; | ||
const bodyJson = (await got(server.url + '/bar', { json: true })).body; | ||
const bodyAsync = (await got(server.url + '/async')).body; | ||
t.deepEqual(bodyString, 'bar'); | ||
t.deepEqual(bodyJson, { foo: 'bar' }); | ||
t.deepEqual(bodyAsync, 'bar'); | ||
}); | ||
test('accepts multiple callbacks in `.get()`', async t => { | ||
const server = await createTestServer(); | ||
server.get('/foo', (req, res, next) => { | ||
res.set('foo', 'bar'); | ||
next(); | ||
}, (req, res) => res.get('foo')); | ||
const { body } = await got(server.url + '/foo'); | ||
t.is(body, 'bar'); | ||
}); |
13714
157
237
6