diff --git a/package.json b/package.json index c62345c..957c47e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "homepage": "https://git.nfp.is/TheThing/flaska/#readme", "devDependencies": { - "eltro": "^1.2.3", + "eltro": "^1.3.2", "formidable": "^1.2.2" }, "files": [ diff --git a/test/flaska.api.test.mjs b/test/flaska.api.test.mjs index e379069..0d1a1c7 100644 --- a/test/flaska.api.test.mjs +++ b/test/flaska.api.test.mjs @@ -80,8 +80,7 @@ t.describe('#constructor', function() { flaska._before[0](ctx) let keys = Object.keys(defaultHeaders) - console.log(Object.keys(ctx.headers).sort()) - console.log(keys.sort()) + assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1) for (let key of keys) { assert.strictEqual(ctx.headers[key], defaultHeaders[key]) @@ -946,6 +945,7 @@ t.describe('#listenAsync()', function() { checkIp = ip cb() }) + let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { diff --git a/test/flaska.in.test.mjs b/test/flaska.in.test.mjs index 49d0d55..76d47f7 100644 --- a/test/flaska.in.test.mjs +++ b/test/flaska.in.test.mjs @@ -19,39 +19,32 @@ t.describe('#requestStart()', function() { flaska.onreserror(onResError) flaska.requestEnded = onEnded - flaska.requestEnd = function(err, ctx) { - try { - assert.ok(err) - assert.strictEqual(assertReq.on.callCount, 1) - assert.strictEqual(assertRes.on.callCount, 2) + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.strictEqual(assertReq.on.callCount, 1) + assert.strictEqual(assertRes.on.callCount, 2) + assert.strictEqual(assertRes.on.firstCall[0], 'error') + assert.strictEqual(typeof(assertRes.on.firstCall[1]), 'function') + assertRes.on.firstCall[1](assertErrorTwo, ctx) + assert.strictEqual(onResError.callCount, 1) + assert.strictEqual(onResError.firstCall[0], assertErrorTwo) + assert.strictEqual(onResError.firstCall[1], ctx) + + assert.strictEqual(assertRes.on.secondCall[0], 'finish') + assert.strictEqual(typeof(assertRes.on.secondCall[1]), 'function') + assert.strictEqual(onEnded.callCount, 0) + assertRes.on.secondCall[1]() + assert.strictEqual(onEnded.callCount, 1) + assert.strictEqual(onEnded.firstCall[0], ctx) - assert.strictEqual(assertRes.on.firstCall[0], 'error') - assert.strictEqual(typeof(assertRes.on.firstCall[1]), 'function') - assertRes.on.firstCall[1](assertErrorTwo, ctx) - assert.strictEqual(onResError.callCount, 1) - assert.strictEqual(onResError.firstCall[0], assertErrorTwo) - assert.strictEqual(onResError.firstCall[1], ctx) - - assert.strictEqual(assertRes.on.secondCall[0], 'finish') - assert.strictEqual(typeof(assertRes.on.secondCall[1]), 'function') - assert.strictEqual(onEnded.callCount, 0) - assertRes.on.secondCall[1]() - assert.strictEqual(onEnded.callCount, 1) - assert.strictEqual(onEnded.firstCall[0], ctx) - - assert.strictEqual(assertReq.on.firstCall[0], 'error') - assert.strictEqual(typeof(assertReq.on.firstCall[1]), 'function') - assertReq.on.firstCall[1](assertErrorOne, ctx) - assert.strictEqual(onReqError.callCount, 1) - assert.strictEqual(onReqError.firstCall[0], assertErrorOne) - assert.strictEqual(onReqError.firstCall[1], ctx) - - // Test abort and close - - cb() - } catch (err) { cb(err) } - } + assert.strictEqual(assertReq.on.firstCall[0], 'error') + assert.strictEqual(typeof(assertReq.on.firstCall[1]), 'function') + assertReq.on.firstCall[1](assertErrorOne, ctx) + assert.strictEqual(onReqError.callCount, 1) + assert.strictEqual(onReqError.firstCall[0], assertErrorOne) + assert.strictEqual(onReqError.firstCall[1], ctx) + }) flaska._beforeCompiled = function(ctx) { throw new Error() } @@ -65,18 +58,13 @@ t.describe('#requestStart()', function() { const assertRes = createRes({ b: 2 }) let flaska = new Flaska({}, faker) - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.strictEqual(err, assertError) - assert.deepStrictEqual(ctx.state, {}) - assert.strictEqual(ctx.req, assertReq) - assert.strictEqual(ctx.res, assertRes) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.strictEqual(err, assertError) + assert.deepStrictEqual(ctx.state, {}) + assert.strictEqual(ctx.req, assertReq) + assert.strictEqual(ctx.res, assertRes) + }) flaska._beforeCompiled = function(ctx) { assert.strictEqual(ctx.req, assertReq) assert.strictEqual(ctx.res, assertRes) @@ -99,18 +87,13 @@ t.describe('#requestStart()', function() { return Promise.resolve().then(function() { return Promise.reject(assertError) }) } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.strictEqual(err, assertError) - assert.deepStrictEqual(ctx.state, {}) - assert.strictEqual(ctx.req, assertReq) - assert.strictEqual(ctx.res, assertRes) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.strictEqual(err, assertError) + assert.deepStrictEqual(ctx.state, {}) + assert.strictEqual(ctx.req, assertReq) + assert.strictEqual(ctx.res, assertRes) + }) flaska.requestStart(assertReq, assertRes) }) @@ -132,39 +115,35 @@ t.describe('#requestStart()', function() { } } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) + flaska.requestEnd = cb.finish(function(err, ctx) { - try { - assert.ok(err) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx.url, assertPath) - assert.strictEqual(ctx.search, assertSearch) - assert.strictEqual(ctx.method, assertMethod) - assert.strictEqual(ctx.status, 200) - assert.strictEqual(ctx.body, null) - assert.strictEqual(ctx.type, null) - assert.strictEqual(ctx.length, null) - assert.strictEqual(ctx.log, assertLog) - assert.ok(ctx.query) - assert.ok(ctx.query.get) - assert.ok(ctx.query.set) - assert.ok(ctx.query.delete) - assert.deepEqual( - Object.keys(ctx.headers).sort(), - ['Server','X-Content-Type-Options','Content-Security-Policy','Cross-Origin-Opener-Policy','Cross-Origin-Resource-Policy','Cross-Origin-Embedder-Policy','Date'].sort() - ) + assert.ok(err) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx.url, assertPath) + assert.strictEqual(ctx.search, assertSearch) + assert.strictEqual(ctx.method, assertMethod) + assert.strictEqual(ctx.status, 200) + assert.strictEqual(ctx.body, null) + assert.strictEqual(ctx.type, null) + assert.strictEqual(ctx.length, null) + assert.strictEqual(ctx.log, assertLog) + assert.ok(ctx.query) + assert.ok(ctx.query.get) + assert.ok(ctx.query.set) + assert.ok(ctx.query.delete) + assert.deepEqual( + Object.keys(ctx.headers).sort(), + ['Server','X-Content-Type-Options','Content-Security-Policy','Cross-Origin-Opener-Policy','Cross-Origin-Resource-Policy','Cross-Origin-Embedder-Policy','Date'].sort() + ) - assert.strictEqual(ctx.headers['Server'], 'Flaska') - assert.strictEqual(ctx.headers['X-Content-Type-Options'], 'nosniff') - assert.strictEqual(ctx.headers['Content-Security-Policy'], `default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; font-src 'self' data:; object-src 'none'; frame-ancestors 'none'`) - assert.strictEqual(ctx.headers['Cross-Origin-Opener-Policy'], 'same-origin') - assert.strictEqual(ctx.headers['Cross-Origin-Resource-Policy'], 'same-origin') - assert.strictEqual(ctx.headers['Cross-Origin-Embedder-Policy'], 'require-corp') - assert.ok(new Date(ctx.headers['Date']).getDate()) - cb() - } catch (err) { cb(err) } - } + assert.strictEqual(ctx.headers['Server'], 'Flaska') + assert.strictEqual(ctx.headers['X-Content-Type-Options'], 'nosniff') + assert.strictEqual(ctx.headers['Content-Security-Policy'], `default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; font-src 'self' data:; object-src 'none'; frame-ancestors 'none'`) + assert.strictEqual(ctx.headers['Cross-Origin-Opener-Policy'], 'same-origin') + assert.strictEqual(ctx.headers['Cross-Origin-Resource-Policy'], 'same-origin') + assert.strictEqual(ctx.headers['Cross-Origin-Embedder-Policy'], 'require-corp') + assert.ok(new Date(ctx.headers['Date']).getDate()) + }) flaska.requestStart(createReq({ url: assertPath + assertSearch, @@ -189,21 +168,16 @@ t.describe('#requestStart()', function() { } } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.strictEqual(err, assertError) - let keys = Object.keys(defaultHeaders) - assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1) - for (let key of keys) { - assert.strictEqual(ctx.headers[key], defaultHeaders[key]) - } - assert.ok(ctx.headers['Date']) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.strictEqual(err, assertError) + let keys = Object.keys(defaultHeaders) + assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1) + for (let key of keys) { + assert.strictEqual(ctx.headers[key], defaultHeaders[key]) + } + assert.ok(ctx.headers['Date']) + }) flaska.requestStart(createReq({ url: '/', @@ -216,9 +190,13 @@ t.describe('#requestStart()', function() { const assertMethod = 'test' const assertPath = '/test/me' const assertSearch = '?asdf=test' + let calledBefore = false let flaska = new Flaska({}, faker) flaska.compile() - flaska._beforeAsyncCompiled = function() { return Promise.resolve() } + flaska._beforeAsyncCompiled = function() { + calledBefore = true + return Promise.resolve() + } flaska.routers.test = { match: function(path) { @@ -227,18 +205,14 @@ t.describe('#requestStart()', function() { } } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx.url, assertPath) - assert.strictEqual(ctx.search, assertSearch) - assert.strictEqual(ctx.method, assertMethod) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(calledBefore) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx.url, assertPath) + assert.strictEqual(ctx.search, assertSearch) + assert.strictEqual(ctx.method, assertMethod) + }) flaska.requestStart(createReq({ url: assertPath + assertSearch, @@ -269,18 +243,13 @@ t.describe('#requestStart()', function() { throw assertError } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx, checkMiddleCtx) - assert.strictEqual(ctx.params, assertParams) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx, checkMiddleCtx) + assert.strictEqual(ctx.params, assertParams) + }) flaska.requestStart(createReq({ url: '', @@ -301,17 +270,12 @@ t.describe('#requestStart()', function() { flaska.get('/:id', handler) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx, checkHandlerCtx) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx, checkHandlerCtx) + }) flaska.requestStart(createReq({ url: '/test', @@ -331,16 +295,12 @@ t.describe('#requestStart()', function() { flaska.get('/test', function() { throw new Error('should not be called') }) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err) return cb(err) - - try { - assert.ok(ctx) - assert.strictEqual(on404Error.callCount, 1) - assert.strictEqual(on404Error.firstCall[0], ctx) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.notOk(err) + assert.ok(ctx) + assert.strictEqual(on404Error.callCount, 1) + assert.strictEqual(on404Error.firstCall[0], ctx) + }) flaska.requestStart(createReq({ url: '/nope', @@ -361,17 +321,11 @@ t.describe('#requestStart()', function() { flaska.get('/test', function() { throw new Error('should not be called') }) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(ctx, checkCtx) - assert.strictEqual(err, assertError) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.strictEqual(err, assertError) + assert.ok(ctx) + assert.strictEqual(ctx, checkCtx) + }) flaska.requestStart(createReq({ url: '/nope', @@ -391,17 +345,11 @@ t.describe('#requestStart()', function() { flaska.get('/test', function() { throw new Error('should not be called') }) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(ctx, checkCtx) - assert.strictEqual(err, assertError) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.strictEqual(err, assertError) + assert.ok(ctx) + assert.strictEqual(ctx, checkCtx) + }) flaska.requestStart(createReq({ url: '/nope', @@ -421,17 +369,11 @@ t.describe('#requestStart()', function() { flaska.get('/test', middles, function() { throw new Error('should not be called') }) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(ctx, checkCtx) - assert.strictEqual(err, assertError) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.strictEqual(err, assertError) + assert.ok(ctx) + assert.strictEqual(ctx, checkCtx) + }) flaska.requestStart(createReq({ url: '/test', @@ -453,14 +395,11 @@ t.describe('#requestStart()', function() { throw new Error('should not be called') } - flaska.requestEnd = function(err, ctx) { - try { - assert.notOk(err) - assert.ok(ctx) - assert.strictEqual(ctx.body, assertBody) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.notOk(err) + assert.ok(ctx) + assert.strictEqual(ctx.body, assertBody) + }) flaska.requestStart(createReq({ url: '/test/something/here', @@ -485,16 +424,11 @@ t.describe('#requestStart()', function() { return Promise.resolve().then(function() { return Promise.reject(assertError) }) } - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + }) flaska.requestStart(createReq({ url: '', @@ -515,17 +449,12 @@ t.describe('#requestStart()', function() { flaska.get('/:id', [function() { return Promise.resolve() }], handler) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx, checkHandlerCtx) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx, checkHandlerCtx) + }) flaska.requestStart(createReq({ url: '/test', @@ -546,17 +475,12 @@ t.describe('#requestStart()', function() { flaska.get('/:id', [function() { return Promise.resolve() }], handler) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx, checkHandlerCtx) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx, checkHandlerCtx) + }) flaska.requestStart(createReq({ url: '/test', @@ -585,15 +509,12 @@ t.describe('#requestStart()', function() { flaska.get('/::path', [middle], handler) flaska.compile() - flaska.requestEnd = function(err, ctx) { - try { + flaska.requestEnd = cb.finish(function(err, ctx) { assert.notOk(err) assert.ok(ctx) assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.state, assertState) - cb() - } catch (err) { cb(err) } - } + }) flaska.requestStart(createReq({ url: '/test/something/here', @@ -616,17 +537,12 @@ t.describe('#requestStart()', function() { flaska.get('/:id', handler) flaska.compile() - flaska.requestEnd = function(err, ctx) { - if (err && err !== assertError) return cb(err) - - try { - assert.ok(err) - assert.ok(ctx) - assert.strictEqual(err, assertError) - assert.strictEqual(ctx, checkHandlerCtx) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.ok(err) + assert.ok(ctx) + assert.strictEqual(err, assertError) + assert.strictEqual(ctx, checkHandlerCtx) + }) flaska.requestStart(createReq({ url: '/test', @@ -651,14 +567,11 @@ t.describe('#requestStart()', function() { throw new Error('should not be called') } - flaska.requestEnd = function(err, ctx) { - try { - assert.notOk(err) - assert.ok(ctx) - assert.strictEqual(ctx.body, assertBody) - cb() - } catch (err) { cb(err) } - } + flaska.requestEnd = cb.finish(function(err, ctx) { + assert.notOk(err) + assert.ok(ctx) + assert.strictEqual(ctx.body, assertBody) + }) flaska.requestStart(createReq({ url: '/test/something/here', diff --git a/test/flaska.out.test.mjs b/test/flaska.out.test.mjs index e8609bf..a6c1025 100644 --- a/test/flaska.out.test.mjs +++ b/test/flaska.out.test.mjs @@ -12,20 +12,17 @@ t.describe('#requestEnd()', function() { const assertStatus = 501 // Calculated manually just in case const assertBodyLength = 7 - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.body, assertBody) - assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') - assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) - assert.ok(body) - assert.strictEqual(body, '{"a":1}') - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') + assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) + assert.ok(body) + assert.strictEqual(body, '{"a":1}') + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({}, onFinish) let flaska = new Flaska({}, fakerHttp, fakeStream) @@ -43,14 +40,11 @@ t.describe('#requestEnd()', function() { const assertErrorNotSeen = new Error('should not be seen') const assertError = new Error('test') - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, 500) - assert.strictEqual(ctx.body.status, 500) - assert.strictEqual(ctx.body.message, 'Internal Server Error') - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, 500) + assert.strictEqual(ctx.body.status, 500) + assert.strictEqual(ctx.body.message, 'Internal Server Error') + }) const ctx = createCtx({}, onFinish) let flaska = new Flaska({}, fakerHttp, fakeStream) @@ -70,26 +64,23 @@ t.describe('#requestEnd()', function() { // Calculated manually just in case const assertBodyLength = 7 const assertBody = { a: 1 } - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.body, assertBody) - assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') - assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') + assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) - if (method === 'GET') { - assert.ok(body) - assert.strictEqual(body, '{"a":1}') - } else { - assert.notOk(body) - } + if (method === 'GET') { + assert.ok(body) + assert.strictEqual(body, '{"a":1}') + } else { + assert.notOk(body) + } - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: assertStatus, @@ -104,21 +95,18 @@ t.describe('#requestEnd()', function() { // Calculated manually just in case const assertBodyLength = 0 const assertBody = null - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, 204) - assert.strictEqual(ctx.body, assertBody) - assert.notOk(ctx.headers['Content-Type']) - assert.notOk(ctx.headers['Content-Length']) + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, 204) + assert.strictEqual(ctx.body, assertBody) + assert.notOk(ctx.headers['Content-Type']) + assert.notOk(ctx.headers['Content-Length']) - assert.strictEqual(body, undefined) + assert.strictEqual(body, undefined) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: 200, @@ -133,21 +121,18 @@ t.describe('#requestEnd()', function() { const assertStatus = 202 // Calculated manually just in case const assertBody = null - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.body, assertBody) - assert.notOk(ctx.headers['Content-Type']) - assert.strictEqual(ctx.headers['Content-Length'], 0) + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.notOk(ctx.headers['Content-Type']) + assert.strictEqual(ctx.headers['Content-Length'], 0) - assert.strictEqual(body, undefined) + assert.strictEqual(body, undefined) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: assertStatus, @@ -163,26 +148,23 @@ t.describe('#requestEnd()', function() { // Calculated manually just in case const assertBodyLength = 4 const assertBody = 'eða' - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.body, assertBody) - assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') - assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') + assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) - if (method === 'GET') { - assert.ok(body) - assert.strictEqual(body, assertBody) - } else { - assert.notOk(body) - } + if (method === 'GET') { + assert.ok(body) + assert.strictEqual(body, assertBody) + } else { + assert.notOk(body) + } - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: assertStatus, @@ -198,25 +180,22 @@ t.describe('#requestEnd()', function() { // Calculated manually just in case const assertBodyLength = 7 const assertBody = 4214124 - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.body, assertBody) - assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') - assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') + assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) - if (method === 'GET') { - assert.ok(body) - assert.strictEqual(body, assertBody.toString()) - } else { - assert.notOk(body) - } - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + if (method === 'GET') { + assert.ok(body) + assert.strictEqual(body, assertBody.toString()) + } else { + assert.notOk(body) + } + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: assertStatus, @@ -242,22 +221,19 @@ t.describe('#requestEnd()', function() { let body = new FileResponse() let assertBody = { pipe: function() {} } - let onFinish = function(source, target, callback) { - try { - assert.ok(assertHandle.called) - assert.ok(ctx.res.writeHead) - assert.strictEqual(ctx.body, assertBody) - assert.strictEqual(source, assertBody) - assert.strictEqual(target, ctx.res) - assert.strictEqual(typeof(callback), 'function') - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - assert.strictEqual(ctx.headers['Content-Type'], assertContentType) - assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(source, target, callback) { + assert.ok(assertHandle.called) + assert.ok(ctx.res.writeHead) + assert.strictEqual(ctx.body, assertBody) + assert.strictEqual(source, assertBody) + assert.strictEqual(target, ctx.res) + assert.strictEqual(typeof(callback), 'function') + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + assert.strictEqual(ctx.headers['Content-Type'], assertContentType) + assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) + }) const ctx = createCtx({ }) ctx.body = body @@ -278,19 +254,16 @@ t.describe('#requestEnd()', function() { const assertType = 'herp/derp' const assertBody = { pipe: function() {} } - let onFinish = function(source, target, callback) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.headers['Content-Type'], assertType) - assert.strictEqual(source, assertBody) - assert.strictEqual(target, ctx.res) - assert.strictEqual(typeof(callback), 'function') - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(source, target, callback) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.headers['Content-Type'], assertType) + assert.strictEqual(source, assertBody) + assert.strictEqual(target, ctx.res) + assert.strictEqual(typeof(callback), 'function') + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ status: assertStatus, }) @@ -315,20 +288,18 @@ t.describe('#requestEnd()', function() { }) let body = new FileResponse() - let onFinish = function(body) { - try { - assert.ok(assertHandle.called) - assert.ok(ctx.res.writeHead) - assert.strictEqual(ctx.body, null) - assert.notOk(body) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - assert.strictEqual(ctx.headers['Content-Type'], assertContentType) - assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.ok(assertHandle.called) + assert.ok(ctx.res.writeHead) + assert.strictEqual(ctx.body, null) + assert.notOk(body) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + assert.strictEqual(ctx.headers['Content-Type'], assertContentType) + assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) + }) + const ctx = createCtx({ method: method, }, onFinish) @@ -349,20 +320,17 @@ t.describe('#requestEnd()', function() { const assertType = 'herp/derp' const assertBody = { pipe: function() {}, destroy: stub() } - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.headers['Content-Type'], assertType) - assert.notOk(ctx.headers['Content-Length']) - assert.notOk(body) - - assert.ok(assertBody.destroy.called) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.headers['Content-Type'], assertType) + assert.notOk(ctx.headers['Content-Length']) + assert.notOk(body) + + assert.ok(assertBody.destroy.called) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ method: method, status: assertStatus, @@ -405,17 +373,14 @@ t.describe('#requestEnd()', function() { const assertStatus = 209 const assertBody = 'test' const assertType = 'something/else' - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.headers['Content-Type'], assertType) - assert.strictEqual(body, assertBody) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.headers['Content-Type'], assertType) + assert.strictEqual(body, assertBody) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({ status: assertStatus, body: assertBody, @@ -427,18 +392,15 @@ t.describe('#requestEnd()', function() { }) t.test('call pipe should have default type', function(cb) { - let onFinish = function(source, target) { - try { - assert.strictEqual(ctx.status, 200) - assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream') - assert.strictEqual(source, ctx.body) - assert.strictEqual(target, ctx.res) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(source, target) { + assert.strictEqual(ctx.status, 200) + assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream') + assert.strictEqual(source, ctx.body) + assert.strictEqual(target, ctx.res) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({}) ctx.body = { pipe: function() {} } fakeStream.pipeline = onFinish @@ -456,18 +418,15 @@ t.describe('#requestEnd()', function() { tests.forEach(function(test) { t.test(`call pipe with file extension ${test[0]} should mimetype ${test[1]}`, function(cb) { - let onFinish = function(source, target) { - try { - assert.strictEqual(ctx.status, 200) - assert.strictEqual(ctx.headers['Content-Type'], test[1]) - assert.strictEqual(source, ctx.body) - assert.strictEqual(target, ctx.res) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(source, target) { + assert.strictEqual(ctx.status, 200) + assert.strictEqual(ctx.headers['Content-Type'], test[1]) + assert.strictEqual(source, ctx.body) + assert.strictEqual(target, ctx.res) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + }) const ctx = createCtx({}) ctx.body = { pipe: function() {}, path: './bla/test/temp.' + test[0] } fakeStream.pipeline = onFinish @@ -495,17 +454,14 @@ t.describe('#requestEnd()', function() { const assertStatus = status const assertNotBody = 'test' const assertNotType = 'something/else' - let onFinish = function(body) { - try { - assert.strictEqual(ctx.status, assertStatus) - assert.strictEqual(ctx.res.setHeader.callCount, 0) - assert.ok(ctx.res.writeHead.called) - assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) - assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) - assert.notOk(body) - cb() - } catch (err) { cb(err) } - } + let onFinish = cb.finish(function(body) { + assert.strictEqual(ctx.status, assertStatus) + assert.strictEqual(ctx.res.setHeader.callCount, 0) + assert.ok(ctx.res.writeHead.called) + assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) + assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) + assert.notOk(body) + }) const ctx = createCtx({ status: assertStatus, body: assertNotBody,