Update eltro, clean up a few tests
continuous-integration/appveyor/branch AppVeyor build succeeded Details

master
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,39 +19,32 @@ 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(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(assertReq.on.firstCall[0], 'error')
assert.strictEqual(typeof(assertRes.on.firstCall[1]), 'function') assert.strictEqual(typeof(assertReq.on.firstCall[1]), 'function')
assertRes.on.firstCall[1](assertErrorTwo, ctx) assertReq.on.firstCall[1](assertErrorOne, ctx)
assert.strictEqual(onResError.callCount, 1) assert.strictEqual(onReqError.callCount, 1)
assert.strictEqual(onResError.firstCall[0], assertErrorTwo) assert.strictEqual(onReqError.firstCall[0], assertErrorOne)
assert.strictEqual(onResError.firstCall[1], ctx) assert.strictEqual(onReqError.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) }
}
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) assert.ok(err)
assert.strictEqual(err, assertError)
try { assert.deepStrictEqual(ctx.state, {})
assert.ok(err) assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(err, assertError) assert.strictEqual(ctx.res, assertRes)
assert.deepStrictEqual(ctx.state, {}) })
assert.strictEqual(ctx.req, assertReq)
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) assert.ok(err)
assert.strictEqual(err, assertError)
try { assert.deepStrictEqual(ctx.state, {})
assert.ok(err) assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(err, assertError) assert.strictEqual(ctx.res, assertRes)
assert.deepStrictEqual(ctx.state, {}) })
assert.strictEqual(ctx.req, assertReq)
assert.strictEqual(ctx.res, assertRes)
cb()
} catch (err) { cb(err) }
}
flaska.requestStart(assertReq, assertRes) flaska.requestStart(assertReq, assertRes)
}) })
@ -132,39 +115,35 @@ 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) assert.strictEqual(ctx.search, assertSearch)
assert.strictEqual(ctx.search, assertSearch) assert.strictEqual(ctx.method, assertMethod)
assert.strictEqual(ctx.method, assertMethod) assert.strictEqual(ctx.status, 200)
assert.strictEqual(ctx.status, 200) assert.strictEqual(ctx.body, null)
assert.strictEqual(ctx.body, null) assert.strictEqual(ctx.type, null)
assert.strictEqual(ctx.type, null) assert.strictEqual(ctx.length, null)
assert.strictEqual(ctx.length, null) assert.strictEqual(ctx.log, assertLog)
assert.strictEqual(ctx.log, assertLog) assert.ok(ctx.query)
assert.ok(ctx.query) assert.ok(ctx.query.get)
assert.ok(ctx.query.get) assert.ok(ctx.query.set)
assert.ok(ctx.query.set) assert.ok(ctx.query.delete)
assert.ok(ctx.query.delete) assert.deepEqual(
assert.deepEqual( Object.keys(ctx.headers).sort(),
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()
['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['Server'], 'Flaska')
assert.strictEqual(ctx.headers['X-Content-Type-Options'], 'nosniff') 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['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-Opener-Policy'], 'same-origin')
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,21 +168,16 @@ t.describe('#requestStart()', function() {
} }
} }
flaska.requestEnd = function(err, ctx) { flaska.requestEnd = cb.finish(function(err, ctx) {
if (err && err !== assertError) return cb(err) assert.ok(err)
assert.strictEqual(err, assertError)
try { let keys = Object.keys(defaultHeaders)
assert.ok(err) assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1)
assert.strictEqual(err, assertError) for (let key of keys) {
let keys = Object.keys(defaultHeaders) assert.strictEqual(ctx.headers[key], defaultHeaders[key])
assert.strictEqual(Object.keys(ctx.headers).length, keys.length + 1) }
for (let key of keys) { assert.ok(ctx.headers['Date'])
assert.strictEqual(ctx.headers[key], defaultHeaders[key]) })
}
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) assert.ok(err)
assert.ok(calledBefore)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx.url, assertPath)
assert.strictEqual(err, assertError) assert.strictEqual(ctx.search, assertSearch)
assert.strictEqual(ctx.url, assertPath) assert.strictEqual(ctx.method, assertMethod)
assert.strictEqual(ctx.search, assertSearch) })
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx, checkMiddleCtx)
assert.ok(ctx) assert.strictEqual(ctx.params, assertParams)
assert.strictEqual(err, assertError) })
assert.strictEqual(ctx, checkMiddleCtx)
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx, checkHandlerCtx)
assert.ok(ctx) })
assert.strictEqual(err, assertError)
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)
assert.ok(ctx)
try { assert.strictEqual(on404Error.callCount, 1)
assert.ok(ctx) assert.strictEqual(on404Error.firstCall[0], ctx)
assert.strictEqual(on404Error.callCount, 1) })
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)
assert.ok(ctx)
try { assert.strictEqual(ctx, checkCtx)
assert.ok(err) })
assert.ok(ctx)
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)
assert.ok(ctx)
try { assert.strictEqual(ctx, checkCtx)
assert.ok(err) })
assert.ok(ctx)
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)
assert.ok(ctx)
try { assert.strictEqual(ctx, checkCtx)
assert.ok(err) })
assert.ok(ctx)
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) })
assert.ok(ctx)
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx, checkHandlerCtx)
assert.ok(ctx) })
assert.strictEqual(err, assertError)
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx, checkHandlerCtx)
assert.ok(ctx) })
assert.strictEqual(err, assertError)
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) assert.ok(err)
assert.ok(ctx)
try { assert.strictEqual(err, assertError)
assert.ok(err) assert.strictEqual(ctx, checkHandlerCtx)
assert.ok(ctx) })
assert.strictEqual(err, assertError)
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,20 +12,17 @@ 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') assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength) assert.ok(body)
assert.ok(body) assert.strictEqual(body, '{"a":1}')
assert.strictEqual(body, '{"a":1}') 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,26 +64,23 @@ 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') assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
if (method === 'GET') { if (method === 'GET') {
assert.ok(body) assert.ok(body)
assert.strictEqual(body, '{"a":1}') assert.strictEqual(body, '{"a":1}')
} else { } else {
assert.notOk(body) assert.notOk(body)
} }
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,21 +95,18 @@ 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']) assert.notOk(ctx.headers['Content-Length'])
assert.notOk(ctx.headers['Content-Length'])
assert.strictEqual(body, undefined) assert.strictEqual(body, undefined)
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,21 +121,18 @@ 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']) assert.strictEqual(ctx.headers['Content-Length'], 0)
assert.strictEqual(ctx.headers['Content-Length'], 0)
assert.strictEqual(body, undefined) assert.strictEqual(body, undefined)
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,26 +148,23 @@ 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') assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
if (method === 'GET') { if (method === 'GET') {
assert.ok(body) assert.ok(body)
assert.strictEqual(body, assertBody) assert.strictEqual(body, assertBody)
} else { } else {
assert.notOk(body) assert.notOk(body)
} }
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,25 +180,22 @@ 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') assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
if (method === 'GET') { if (method === 'GET') {
assert.ok(body) assert.ok(body)
assert.strictEqual(body, assertBody.toString()) assert.strictEqual(body, assertBody.toString())
} else { } else {
assert.notOk(body) assert.notOk(body)
} }
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,22 +221,19 @@ 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) assert.strictEqual(source, assertBody)
assert.strictEqual(source, assertBody) assert.strictEqual(target, ctx.res)
assert.strictEqual(target, ctx.res) assert.strictEqual(typeof(callback), 'function')
assert.strictEqual(typeof(callback), '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) 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,19 +254,16 @@ 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) assert.strictEqual(target, ctx.res)
assert.strictEqual(target, ctx.res) assert.strictEqual(typeof(callback), 'function')
assert.strictEqual(typeof(callback), '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,20 +288,18 @@ 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) assert.notOk(body)
assert.notOk(body) 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.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,20 +320,17 @@ 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']) assert.notOk(body)
assert.notOk(body)
assert.ok(assertBody.destroy.called)
assert.ok(assertBody.destroy.called) 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,18 +392,15 @@ 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) assert.strictEqual(target, ctx.res)
assert.strictEqual(target, ctx.res) 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,18 +418,15 @@ 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) assert.strictEqual(target, ctx.res)
assert.strictEqual(target, ctx.res) 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,