diff --git a/test/helpers.js b/test/helpers.js index 5cb4cc6..a3c2d02 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -6,16 +6,18 @@ const { createServer } = require('http') const closeServer = server => require('util').promisify(server.close.bind(server))() -const runServer = async (t, handler) => { +const runServer = async (t, handler, { throwErrors = true } = {}) => { const server = createServer(async (req, res) => { try { await handler({ req, res }) } catch (error) { console.error(error) + if (throwErrors) throw error res.statusCode = 500 res.end() } }) + const url = await listen(server) t.teardown(() => closeServer(server)) return url diff --git a/test/index.js b/test/index.js index b69ab99..0d3eddf 100644 --- a/test/index.js +++ b/test/index.js @@ -56,7 +56,7 @@ test('compress support', async t => { t.is(headersOne.etag, headersTwo.etag) }) -test('exit early is get is empty', async t => { +test('exit early is response was written', async t => { let isEnd = false const end = (res, msg) => { isEnd = true @@ -73,21 +73,22 @@ test('exit early is get is empty', async t => { t.is(res.body, 'get') }) -test('prevent send if data is undefined', async t => { +test('prevent send if get throws an error', async t => { t.plan(1) let isSendCalled = false const url = await runServer( t, cacheableResponse({ compress: true, - get: ({ req, res }) => { + get: () => { throw Error() }, - send: ({ data, headers, res, req, ...props }) => { + send: ({ res }) => { isSendCalled = true res.end('Hello World') } - }) + }), + { throwErrors: false } ) try { await got(`${url}/kikobeats`, { retry: 0 })