Update eltro, clean up a few tests
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2022-07-04 13:24:19 +00:00
parent e9c600b869
commit baf2d896c1
4 changed files with 321 additions and 452 deletions

View file

@ -39,7 +39,7 @@
}, },
"homepage": "https://git.nfp.is/TheThing/flaska/#readme", "homepage": "https://git.nfp.is/TheThing/flaska/#readme",
"devDependencies": { "devDependencies": {
"eltro": "^1.2.3", "eltro": "^1.3.2",
"formidable": "^1.2.2" "formidable": "^1.2.2"
}, },
"files": [ "files": [

View file

@ -80,8 +80,7 @@ t.describe('#constructor', function() {
flaska._before[0](ctx) flaska._before[0](ctx)
let keys = Object.keys(defaultHeaders) 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) assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1)
for (let key of keys) { for (let key of keys) {
assert.strictEqual(ctx.headers[key], defaultHeaders[key]) assert.strictEqual(ctx.headers[key], defaultHeaders[key])
@ -946,6 +945,7 @@ t.describe('#listenAsync()', function() {
checkIp = ip checkIp = ip
cb() cb()
}) })
let flaska = new Flaska({}, testFaker) let flaska = new Flaska({}, testFaker)
assert.ok(flaska.requestStart) assert.ok(flaska.requestStart)
flaska.requestStart = function() { flaska.requestStart = function() {

View file

@ -19,13 +19,11 @@ t.describe('#requestStart()', function() {
flaska.onreserror(onResError) flaska.onreserror(onResError)
flaska.requestEnded = onEnded flaska.requestEnded = onEnded
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
try {
assert.ok(err) assert.ok(err)
assert.strictEqual(assertReq.on.callCount, 1) assert.strictEqual(assertReq.on.callCount, 1)
assert.strictEqual(assertRes.on.callCount, 2) assert.strictEqual(assertRes.on.callCount, 2)
assert.strictEqual(assertRes.on.firstCall[0], 'error') assert.strictEqual(assertRes.on.firstCall[0], 'error')
assert.strictEqual(typeof(assertRes.on.firstCall[1]), 'function') assert.strictEqual(typeof(assertRes.on.firstCall[1]), 'function')
assertRes.on.firstCall[1](assertErrorTwo, ctx) assertRes.on.firstCall[1](assertErrorTwo, ctx)
@ -46,12 +44,7 @@ t.describe('#requestStart()', function() {
assert.strictEqual(onReqError.callCount, 1) assert.strictEqual(onReqError.callCount, 1)
assert.strictEqual(onReqError.firstCall[0], assertErrorOne) assert.strictEqual(onReqError.firstCall[0], assertErrorOne)
assert.strictEqual(onReqError.firstCall[1], ctx) assert.strictEqual(onReqError.firstCall[1], ctx)
})
// Test abort and close
cb()
} catch (err) { cb(err) }
}
flaska._beforeCompiled = function(ctx) { flaska._beforeCompiled = function(ctx) {
throw new Error() throw new Error()
} }
@ -65,18 +58,13 @@ t.describe('#requestStart()', function() {
const assertRes = createRes({ b: 2 }) const assertRes = createRes({ b: 2 })
let flaska = new Flaska({}, faker) let flaska = new Flaska({}, faker)
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.deepStrictEqual(ctx.state, {}) assert.deepStrictEqual(ctx.state, {})
assert.strictEqual(ctx.req, assertReq) assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(ctx.res, assertRes) assert.strictEqual(ctx.res, assertRes)
cb() })
} catch (err) { cb(err) }
}
flaska._beforeCompiled = function(ctx) { flaska._beforeCompiled = function(ctx) {
assert.strictEqual(ctx.req, assertReq) assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(ctx.res, assertRes) assert.strictEqual(ctx.res, assertRes)
@ -99,18 +87,13 @@ t.describe('#requestStart()', function() {
return Promise.resolve().then(function() { return Promise.reject(assertError) }) return Promise.resolve().then(function() { return Promise.reject(assertError) })
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.deepStrictEqual(ctx.state, {}) assert.deepStrictEqual(ctx.state, {})
assert.strictEqual(ctx.req, assertReq) assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(ctx.res, assertRes) assert.strictEqual(ctx.res, assertRes)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(assertReq, assertRes) flaska.requestStart(assertReq, assertRes)
}) })
@ -132,10 +115,8 @@ t.describe('#requestStart()', function() {
} }
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx.url, assertPath) assert.strictEqual(ctx.url, assertPath)
@ -162,9 +143,7 @@ t.describe('#requestStart()', function() {
assert.strictEqual(ctx.headers['Cross-Origin-Resource-Policy'], 'same-origin') assert.strictEqual(ctx.headers['Cross-Origin-Resource-Policy'], 'same-origin')
assert.strictEqual(ctx.headers['Cross-Origin-Embedder-Policy'], 'require-corp') assert.strictEqual(ctx.headers['Cross-Origin-Embedder-Policy'], 'require-corp')
assert.ok(new Date(ctx.headers['Date']).getDate()) assert.ok(new Date(ctx.headers['Date']).getDate())
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: assertPath + assertSearch, url: assertPath + assertSearch,
@ -189,10 +168,7 @@ t.describe('#requestStart()', function() {
} }
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
let keys = Object.keys(defaultHeaders) let keys = Object.keys(defaultHeaders)
@ -201,9 +177,7 @@ t.describe('#requestStart()', function() {
assert.strictEqual(ctx.headers[key], defaultHeaders[key]) assert.strictEqual(ctx.headers[key], defaultHeaders[key])
} }
assert.ok(ctx.headers['Date']) assert.ok(ctx.headers['Date'])
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/', url: '/',
@ -216,9 +190,13 @@ t.describe('#requestStart()', function() {
const assertMethod = 'test' const assertMethod = 'test'
const assertPath = '/test/me' const assertPath = '/test/me'
const assertSearch = '?asdf=test' const assertSearch = '?asdf=test'
let calledBefore = false
let flaska = new Flaska({}, faker) let flaska = new Flaska({}, faker)
flaska.compile() flaska.compile()
flaska._beforeAsyncCompiled = function() { return Promise.resolve() } flaska._beforeAsyncCompiled = function() {
calledBefore = true
return Promise.resolve()
}
flaska.routers.test = { flaska.routers.test = {
match: function(path) { match: function(path) {
@ -227,18 +205,14 @@ t.describe('#requestStart()', function() {
} }
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(calledBefore)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx.url, assertPath) assert.strictEqual(ctx.url, assertPath)
assert.strictEqual(ctx.search, assertSearch) assert.strictEqual(ctx.search, assertSearch)
assert.strictEqual(ctx.method, assertMethod) assert.strictEqual(ctx.method, assertMethod)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: assertPath + assertSearch, url: assertPath + assertSearch,
@ -269,18 +243,13 @@ t.describe('#requestStart()', function() {
throw assertError throw assertError
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx, checkMiddleCtx) assert.strictEqual(ctx, checkMiddleCtx)
assert.strictEqual(ctx.params, assertParams) assert.strictEqual(ctx.params, assertParams)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '', url: '',
@ -301,17 +270,12 @@ t.describe('#requestStart()', function() {
flaska.get('/:id', handler) flaska.get('/:id', handler)
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx, checkHandlerCtx) assert.strictEqual(ctx, checkHandlerCtx)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test', url: '/test',
@ -331,16 +295,12 @@ t.describe('#requestStart()', function() {
flaska.get('/test', function() { throw new Error('should not be called') }) flaska.get('/test', function() { throw new Error('should not be called') })
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err) return cb(err) assert.notOk(err)
try {
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(on404Error.callCount, 1) assert.strictEqual(on404Error.callCount, 1)
assert.strictEqual(on404Error.firstCall[0], ctx) assert.strictEqual(on404Error.firstCall[0], ctx)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/nope', url: '/nope',
@ -361,17 +321,11 @@ t.describe('#requestStart()', function() {
flaska.get('/test', function() { throw new Error('should not be called') }) flaska.get('/test', function() { throw new Error('should not be called') })
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err) assert.strictEqual(err, assertError)
try {
assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx, checkCtx) assert.strictEqual(ctx, checkCtx)
assert.strictEqual(err, assertError) })
cb()
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/nope', url: '/nope',
@ -391,17 +345,11 @@ t.describe('#requestStart()', function() {
flaska.get('/test', function() { throw new Error('should not be called') }) flaska.get('/test', function() { throw new Error('should not be called') })
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err) assert.strictEqual(err, assertError)
try {
assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx, checkCtx) assert.strictEqual(ctx, checkCtx)
assert.strictEqual(err, assertError) })
cb()
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/nope', url: '/nope',
@ -421,17 +369,11 @@ t.describe('#requestStart()', function() {
flaska.get('/test', middles, function() { throw new Error('should not be called') }) flaska.get('/test', middles, function() { throw new Error('should not be called') })
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err) assert.strictEqual(err, assertError)
try {
assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx, checkCtx) assert.strictEqual(ctx, checkCtx)
assert.strictEqual(err, assertError) })
cb()
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test', url: '/test',
@ -453,14 +395,11 @@ t.describe('#requestStart()', function() {
throw new Error('should not be called') throw new Error('should not be called')
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
try {
assert.notOk(err) assert.notOk(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test/something/here', url: '/test/something/here',
@ -485,16 +424,11 @@ t.describe('#requestStart()', function() {
return Promise.resolve().then(function() { return Promise.reject(assertError) }) return Promise.resolve().then(function() { return Promise.reject(assertError) })
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '', url: '',
@ -515,17 +449,12 @@ t.describe('#requestStart()', function() {
flaska.get('/:id', [function() { return Promise.resolve() }], handler) flaska.get('/:id', [function() { return Promise.resolve() }], handler)
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx, checkHandlerCtx) assert.strictEqual(ctx, checkHandlerCtx)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test', url: '/test',
@ -546,17 +475,12 @@ t.describe('#requestStart()', function() {
flaska.get('/:id', [function() { return Promise.resolve() }], handler) flaska.get('/:id', [function() { return Promise.resolve() }], handler)
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx, checkHandlerCtx) assert.strictEqual(ctx, checkHandlerCtx)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test', url: '/test',
@ -585,15 +509,12 @@ t.describe('#requestStart()', function() {
flaska.get('/::path', [middle], handler) flaska.get('/::path', [middle], handler)
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
try {
assert.notOk(err) assert.notOk(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.state, assertState) assert.strictEqual(ctx.state, assertState)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test/something/here', url: '/test/something/here',
@ -616,17 +537,12 @@ t.describe('#requestStart()', function() {
flaska.get('/:id', handler) flaska.get('/:id', handler)
flaska.compile() flaska.compile()
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err)
try {
assert.ok(err) assert.ok(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(err, assertError) assert.strictEqual(err, assertError)
assert.strictEqual(ctx, checkHandlerCtx) assert.strictEqual(ctx, checkHandlerCtx)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test', url: '/test',
@ -651,14 +567,11 @@ t.describe('#requestStart()', function() {
throw new Error('should not be called') throw new Error('should not be called')
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
try {
assert.notOk(err) assert.notOk(err)
assert.ok(ctx) assert.ok(ctx)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
cb() })
} catch (err) { cb(err) }
}
flaska.requestStart(createReq({ flaska.requestStart(createReq({
url: '/test/something/here', url: '/test/something/here',

View file

@ -12,8 +12,7 @@ t.describe('#requestEnd()', function() {
const assertStatus = 501 const assertStatus = 501
// Calculated manually just in case // Calculated manually just in case
const assertBodyLength = 7 const assertBodyLength = 7
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8')
@ -23,9 +22,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({}, onFinish) const ctx = createCtx({}, onFinish)
let flaska = new Flaska({}, fakerHttp, fakeStream) let flaska = new Flaska({}, fakerHttp, fakeStream)
@ -43,14 +40,11 @@ t.describe('#requestEnd()', function() {
const assertErrorNotSeen = new Error('should not be seen') const assertErrorNotSeen = new Error('should not be seen')
const assertError = new Error('test') const assertError = new Error('test')
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, 500) assert.strictEqual(ctx.status, 500)
assert.strictEqual(ctx.body.status, 500) assert.strictEqual(ctx.body.status, 500)
assert.strictEqual(ctx.body.message, 'Internal Server Error') assert.strictEqual(ctx.body.message, 'Internal Server Error')
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({}, onFinish) const ctx = createCtx({}, onFinish)
let flaska = new Flaska({}, fakerHttp, fakeStream) let flaska = new Flaska({}, fakerHttp, fakeStream)
@ -70,8 +64,7 @@ t.describe('#requestEnd()', function() {
// Calculated manually just in case // Calculated manually just in case
const assertBodyLength = 7 const assertBodyLength = 7
const assertBody = { a: 1 } const assertBody = { a: 1 }
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8') assert.strictEqual(ctx.headers['Content-Type'], 'application/json; charset=utf-8')
@ -87,9 +80,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: assertStatus, status: assertStatus,
@ -104,8 +95,7 @@ t.describe('#requestEnd()', function() {
// Calculated manually just in case // Calculated manually just in case
const assertBodyLength = 0 const assertBodyLength = 0
const assertBody = null const assertBody = null
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, 204) assert.strictEqual(ctx.status, 204)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.notOk(ctx.headers['Content-Type']) assert.notOk(ctx.headers['Content-Type'])
@ -116,9 +106,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: 200, status: 200,
@ -133,8 +121,7 @@ t.describe('#requestEnd()', function() {
const assertStatus = 202 const assertStatus = 202
// Calculated manually just in case // Calculated manually just in case
const assertBody = null const assertBody = null
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.notOk(ctx.headers['Content-Type']) assert.notOk(ctx.headers['Content-Type'])
@ -145,9 +132,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: assertStatus, status: assertStatus,
@ -163,8 +148,7 @@ t.describe('#requestEnd()', function() {
// Calculated manually just in case // Calculated manually just in case
const assertBodyLength = 4 const assertBodyLength = 4
const assertBody = 'eða' const assertBody = 'eða'
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8')
@ -180,9 +164,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: assertStatus, status: assertStatus,
@ -198,8 +180,7 @@ t.describe('#requestEnd()', function() {
// Calculated manually just in case // Calculated manually just in case
const assertBodyLength = 7 const assertBodyLength = 7
const assertBody = 4214124 const assertBody = 4214124
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8') assert.strictEqual(ctx.headers['Content-Type'], 'text/plain; charset=utf-8')
@ -214,9 +195,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: assertStatus, status: assertStatus,
@ -242,8 +221,7 @@ t.describe('#requestEnd()', function() {
let body = new FileResponse() let body = new FileResponse()
let assertBody = { pipe: function() {} } let assertBody = { pipe: function() {} }
let onFinish = function(source, target, callback) { let onFinish = cb.finish(function(source, target, callback) {
try {
assert.ok(assertHandle.called) assert.ok(assertHandle.called)
assert.ok(ctx.res.writeHead) assert.ok(ctx.res.writeHead)
assert.strictEqual(ctx.body, assertBody) assert.strictEqual(ctx.body, assertBody)
@ -255,9 +233,7 @@ t.describe('#requestEnd()', function() {
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
assert.strictEqual(ctx.headers['Content-Type'], assertContentType) assert.strictEqual(ctx.headers['Content-Type'], assertContentType)
assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) assert.strictEqual(ctx.headers['Content-Length'], assertContentLength)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
}) })
ctx.body = body ctx.body = body
@ -278,8 +254,7 @@ t.describe('#requestEnd()', function() {
const assertType = 'herp/derp' const assertType = 'herp/derp'
const assertBody = { pipe: function() {} } const assertBody = { pipe: function() {} }
let onFinish = function(source, target, callback) { let onFinish = cb.finish(function(source, target, callback) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.headers['Content-Type'], assertType) assert.strictEqual(ctx.headers['Content-Type'], assertType)
assert.strictEqual(source, assertBody) assert.strictEqual(source, assertBody)
@ -288,9 +263,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
status: assertStatus, status: assertStatus,
}) })
@ -315,8 +288,7 @@ t.describe('#requestEnd()', function() {
}) })
let body = new FileResponse() let body = new FileResponse()
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.ok(assertHandle.called) assert.ok(assertHandle.called)
assert.ok(ctx.res.writeHead) assert.ok(ctx.res.writeHead)
assert.strictEqual(ctx.body, null) assert.strictEqual(ctx.body, null)
@ -326,9 +298,8 @@ t.describe('#requestEnd()', function() {
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
assert.strictEqual(ctx.headers['Content-Type'], assertContentType) assert.strictEqual(ctx.headers['Content-Type'], assertContentType)
assert.strictEqual(ctx.headers['Content-Length'], assertContentLength) assert.strictEqual(ctx.headers['Content-Length'], assertContentLength)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
}, onFinish) }, onFinish)
@ -349,8 +320,7 @@ t.describe('#requestEnd()', function() {
const assertType = 'herp/derp' const assertType = 'herp/derp'
const assertBody = { pipe: function() {}, destroy: stub() } const assertBody = { pipe: function() {}, destroy: stub() }
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.headers['Content-Type'], assertType) assert.strictEqual(ctx.headers['Content-Type'], assertType)
assert.notOk(ctx.headers['Content-Length']) assert.notOk(ctx.headers['Content-Length'])
@ -360,9 +330,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
method: method, method: method,
status: assertStatus, status: assertStatus,
@ -405,17 +373,14 @@ t.describe('#requestEnd()', function() {
const assertStatus = 209 const assertStatus = 209
const assertBody = 'test' const assertBody = 'test'
const assertType = 'something/else' const assertType = 'something/else'
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.headers['Content-Type'], assertType) assert.strictEqual(ctx.headers['Content-Type'], assertType)
assert.strictEqual(body, assertBody) assert.strictEqual(body, assertBody)
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
status: assertStatus, status: assertStatus,
body: assertBody, body: assertBody,
@ -427,8 +392,7 @@ t.describe('#requestEnd()', function() {
}) })
t.test('call pipe should have default type', function(cb) { t.test('call pipe should have default type', function(cb) {
let onFinish = function(source, target) { let onFinish = cb.finish(function(source, target) {
try {
assert.strictEqual(ctx.status, 200) assert.strictEqual(ctx.status, 200)
assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream') assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream')
assert.strictEqual(source, ctx.body) assert.strictEqual(source, ctx.body)
@ -436,9 +400,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({}) const ctx = createCtx({})
ctx.body = { pipe: function() {} } ctx.body = { pipe: function() {} }
fakeStream.pipeline = onFinish fakeStream.pipeline = onFinish
@ -456,8 +418,7 @@ t.describe('#requestEnd()', function() {
tests.forEach(function(test) { tests.forEach(function(test) {
t.test(`call pipe with file extension ${test[0]} should mimetype ${test[1]}`, function(cb) { t.test(`call pipe with file extension ${test[0]} should mimetype ${test[1]}`, function(cb) {
let onFinish = function(source, target) { let onFinish = cb.finish(function(source, target) {
try {
assert.strictEqual(ctx.status, 200) assert.strictEqual(ctx.status, 200)
assert.strictEqual(ctx.headers['Content-Type'], test[1]) assert.strictEqual(ctx.headers['Content-Type'], test[1])
assert.strictEqual(source, ctx.body) assert.strictEqual(source, ctx.body)
@ -465,9 +426,7 @@ t.describe('#requestEnd()', function() {
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({}) const ctx = createCtx({})
ctx.body = { pipe: function() {}, path: './bla/test/temp.' + test[0] } ctx.body = { pipe: function() {}, path: './bla/test/temp.' + test[0] }
fakeStream.pipeline = onFinish fakeStream.pipeline = onFinish
@ -495,17 +454,14 @@ t.describe('#requestEnd()', function() {
const assertStatus = status const assertStatus = status
const assertNotBody = 'test' const assertNotBody = 'test'
const assertNotType = 'something/else' const assertNotType = 'something/else'
let onFinish = function(body) { let onFinish = cb.finish(function(body) {
try {
assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.res.setHeader.callCount, 0) assert.strictEqual(ctx.res.setHeader.callCount, 0)
assert.ok(ctx.res.writeHead.called) assert.ok(ctx.res.writeHead.called)
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status) assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers) assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
assert.notOk(body) assert.notOk(body)
cb() })
} catch (err) { cb(err) }
}
const ctx = createCtx({ const ctx = createCtx({
status: assertStatus, status: assertStatus,
body: assertNotBody, body: assertNotBody,