2022-03-17 15:09:22 +00:00
|
|
|
import { Eltro as t, spy, assert, stub} from 'eltro'
|
2022-03-26 15:50:18 +00:00
|
|
|
import { FileResponse, Flaska, FlaskaRouter } from '../flaska.mjs'
|
2022-03-16 10:41:25 +00:00
|
|
|
import { fakeHttp, createCtx } from './helper.mjs'
|
2021-10-09 00:12:56 +00:00
|
|
|
|
|
|
|
const fakerHttp = fakeHttp()
|
|
|
|
const fakeStream = { pipeline: spy() }
|
|
|
|
|
|
|
|
t.describe('#requestEnd()', function() {
|
|
|
|
t.test('calls onerror correctly on error', function(cb) {
|
|
|
|
const assertError = new Error('test')
|
|
|
|
const assertBody = { a: 1 }
|
2021-10-10 20:08:43 +00:00
|
|
|
const assertStatus = 501
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 7
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
const ctx = createCtx({}, onFinish)
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
|
|
|
flaska.onerror(function(err, inctx) {
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
assert.strictEqual(inctx, ctx)
|
2021-10-10 20:08:43 +00:00
|
|
|
inctx.status = assertStatus
|
2021-10-09 00:12:56 +00:00
|
|
|
inctx.body = assertBody
|
|
|
|
})
|
|
|
|
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(assertError, ctx)
|
2021-10-09 00:12:56 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-01-05 14:06:06 +00:00
|
|
|
t.test('calls backup onerror correctly if error throws', function(cb) {
|
|
|
|
const assertErrorNotSeen = new Error('should not be seen')
|
|
|
|
const assertError = new Error('test')
|
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
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')
|
|
|
|
})
|
2022-01-05 14:06:06 +00:00
|
|
|
const ctx = createCtx({}, onFinish)
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
|
|
|
flaska.onerror(function() {
|
|
|
|
throw assertError
|
|
|
|
})
|
|
|
|
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(assertErrorNotSeen, ctx)
|
2022-01-05 14:06:06 +00:00
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let testMethods = ['GET', 'HEAD']
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
testMethods.forEach(function(method) {
|
|
|
|
t.describe(method, function() {
|
|
|
|
t.test('call res and end correctly when dealing with objects', function(cb) {
|
|
|
|
const assertStatus = 202
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 7
|
|
|
|
const assertBody = { a: 1 }
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-10-09 00:12:56 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.ok(ctx.res.writeHead.called)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
|
2023-05-11 11:12:23 +00:00
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
|
|
|
|
})
|
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2023-05-11 11:12:23 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2023-05-11 11:12:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('call res and end correctly when dealing with buffers', function(cb) {
|
|
|
|
const assertStatus = 202
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 11
|
|
|
|
const assertBody = Buffer.from('hello world')
|
|
|
|
let onFinish = cb.finish(function(body) {
|
|
|
|
assert.strictEqual(ctx.status, assertStatus)
|
|
|
|
assert.strictEqual(ctx.body, assertBody)
|
|
|
|
assert.strictEqual(ctx.headers['Content-Type'], 'application/octet-stream')
|
|
|
|
assert.strictEqual(ctx.headers['Content-Length'], assertBodyLength)
|
|
|
|
|
|
|
|
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)
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
t.test('call res and end correctly when dealing with null and no status override', function(cb) {
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 0
|
|
|
|
const assertBody = null
|
2022-07-04 13:24:19 +00:00
|
|
|
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'])
|
2022-03-26 15:50:18 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.strictEqual(body, undefined)
|
2022-03-27 00:30:56 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.ok(ctx.res.writeHead.called)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: 200,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
t.test('call res and end correctly when dealing with null and status override', function(cb) {
|
|
|
|
const assertStatus = 202
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBody = null
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
2022-03-27 00:30:56 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.strictEqual(body, undefined)
|
2022-03-27 00:30:56 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.ok(ctx.res.writeHead.called)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
t.test('call res and end correctly when dealing with strings', function(cb) {
|
|
|
|
const assertStatus = 206
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 4
|
|
|
|
const assertBody = 'eða'
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-03-27 00:30:56 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
assert.ok(ctx.res.writeHead.called)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[0], ctx.status)
|
|
|
|
assert.strictEqual(ctx.res.writeHead.firstCall[1], ctx.headers)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
t.test('call res and end correctly when dealing with numbers', function(cb) {
|
|
|
|
const assertStatus = 211
|
|
|
|
// Calculated manually just in case
|
|
|
|
const assertBodyLength = 7
|
|
|
|
const assertBody = 4214124
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = assertBody
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
if (method === 'GET') {
|
|
|
|
t.test('call handleRequest if body contains it', function(cb) {
|
|
|
|
const assertContentType = 'test/test'
|
|
|
|
const assertContentLength = 1241241
|
|
|
|
const assertHandle = stub().returnWith(function(checkCtx, secondary) {
|
|
|
|
assert.notOk(secondary)
|
|
|
|
assert.strictEqual(checkCtx, ctx)
|
|
|
|
assert.notOk(ctx.res.writeHead.called)
|
|
|
|
ctx.type = assertContentType
|
|
|
|
ctx.headers['Content-Length'] = assertContentLength
|
|
|
|
return assertBody
|
|
|
|
})
|
|
|
|
|
|
|
|
let body = new FileResponse()
|
|
|
|
let assertBody = { pipe: function() {} }
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
})
|
|
|
|
ctx.body = body
|
|
|
|
fakeStream.pipeline = onFinish
|
|
|
|
body.handleRequest = assertHandle
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
|
|
|
|
|
|
|
try {
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
} catch (err) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('call pipeline correctly when dealing with pipe', function(cb) {
|
|
|
|
const assertStatus = 211
|
|
|
|
const assertType = 'herp/derp'
|
|
|
|
const assertBody = { pipe: function() {} }
|
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
status: assertStatus,
|
|
|
|
})
|
|
|
|
fakeStream.pipeline = onFinish
|
|
|
|
|
|
|
|
ctx.body = assertBody
|
|
|
|
ctx.type = assertType
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
t.test('call handleRequest if body contains it', function(cb) {
|
|
|
|
const assertContentType = 'test/test'
|
|
|
|
const assertContentLength = 1241241
|
|
|
|
const assertHandle = stub().returnWith(function(checkCtx, secondary) {
|
|
|
|
assert.notOk(secondary)
|
|
|
|
assert.strictEqual(checkCtx, ctx)
|
|
|
|
assert.notOk(ctx.res.writeHead.called)
|
|
|
|
ctx.type = assertContentType
|
|
|
|
ctx.headers['Content-Length'] = assertContentLength
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
let body = new FileResponse()
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.body = body
|
|
|
|
body.handleRequest = assertHandle
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-27 00:30:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
|
|
|
|
|
|
|
try {
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
} catch (err) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('call pipeline correctly when dealing with pipe', function(cb) {
|
|
|
|
const assertStatus = 211
|
|
|
|
const assertType = 'herp/derp'
|
|
|
|
const assertBody = { pipe: function() {}, destroy: stub() }
|
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-27 00:30:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
method: method,
|
|
|
|
status: assertStatus,
|
|
|
|
}, onFinish)
|
|
|
|
|
|
|
|
ctx.body = assertBody
|
|
|
|
ctx.type = assertType
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-27 00:30:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-03-26 15:50:18 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-26 15:50:18 +00:00
|
|
|
t.test('call _onerror with error if handleRequest throws error', function() {
|
|
|
|
let body = new FileResponse()
|
|
|
|
const assertError = new Error('Secrets of the Goddess')
|
|
|
|
let assertBody = { a : 1 }
|
|
|
|
const ctx = createCtx({
|
|
|
|
status: 204,
|
|
|
|
})
|
|
|
|
ctx.body = body
|
|
|
|
const assertHandle = stub().throws(assertError)
|
|
|
|
body.handleRequest = assertHandle
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
|
|
|
flaska._onerror = stub().returnWith(function(err, checkCtx) {
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
checkCtx.body = assertBody
|
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-26 15:50:18 +00:00
|
|
|
assert.ok(assertHandle.called)
|
|
|
|
assert.ok(flaska._onerror.called)
|
|
|
|
assert.strictEqual(flaska._onerror.firstCall[0], assertError)
|
|
|
|
assert.ok(ctx.res.writeHead)
|
|
|
|
assert.strictEqual(ctx.body, assertBody)
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
t.test('call res and end correctly when dealing with custom type', function(cb) {
|
|
|
|
const assertStatus = 209
|
|
|
|
const assertBody = 'test'
|
|
|
|
const assertType = 'something/else'
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
status: assertStatus,
|
|
|
|
body: assertBody,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.type = assertType
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2021-10-09 00:12:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('call pipe should have default type', function(cb) {
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
const ctx = createCtx({})
|
|
|
|
ctx.body = { pipe: function() {} }
|
|
|
|
fakeStream.pipeline = onFinish
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2021-10-09 00:12:56 +00:00
|
|
|
})
|
2022-03-17 15:09:22 +00:00
|
|
|
|
|
|
|
let tests = [
|
|
|
|
['png', 'image/png'],
|
|
|
|
['jpg', 'image/jpeg'],
|
|
|
|
['bmp', 'image/bmp'],
|
|
|
|
['js', 'text/javascript'],
|
|
|
|
]
|
|
|
|
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
t.test(`call pipe with file extension ${test[0]} should mimetype ${test[1]}`, function(cb) {
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2022-03-17 15:09:22 +00:00
|
|
|
const ctx = createCtx({})
|
|
|
|
ctx.body = { pipe: function() {}, path: './bla/test/temp.' + test[0] }
|
|
|
|
fakeStream.pipeline = onFinish
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2022-03-17 15:09:22 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2022-03-17 15:09:22 +00:00
|
|
|
})
|
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2021-10-10 21:32:15 +00:00
|
|
|
t.test('should return immediately if req is finished', function() {
|
|
|
|
let onFinish = function() {
|
|
|
|
throw new Error('should not be called')
|
|
|
|
}
|
|
|
|
const ctx = createCtx({ }, onFinish)
|
|
|
|
ctx.res.writableEnded = true
|
|
|
|
|
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2021-10-10 21:32:15 +00:00
|
|
|
})
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
const emptyStatuses = [204, 205, 304]
|
|
|
|
|
|
|
|
emptyStatuses.forEach(function(status) {
|
|
|
|
t.test('call res and end correctly when dealing with status ' + status, function(cb) {
|
|
|
|
const assertStatus = status
|
|
|
|
const assertNotBody = 'test'
|
|
|
|
const assertNotType = 'something/else'
|
2022-07-04 13:24:19 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-10-09 00:12:56 +00:00
|
|
|
const ctx = createCtx({
|
|
|
|
status: assertStatus,
|
|
|
|
body: assertNotBody,
|
|
|
|
}, onFinish)
|
|
|
|
ctx.type = assertNotType
|
2024-11-18 03:57:51 +00:00
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
let flaska = new Flaska({}, fakerHttp, fakeStream)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnd(null, ctx)
|
2021-10-09 00:12:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.describe('#requestEnded()', function() {
|
2022-03-17 15:09:22 +00:00
|
|
|
t.test('calls destroy if body is pipe and is not closed', function() {
|
|
|
|
const ctx = createCtx({
|
|
|
|
body: {
|
|
|
|
pipe: {},
|
|
|
|
closed: false,
|
|
|
|
destroy: spy(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
let flaska = new Flaska({}, fakerHttp)
|
|
|
|
flaska._afterCompiled = function() {}
|
|
|
|
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnded(ctx)
|
2022-03-17 15:09:22 +00:00
|
|
|
|
|
|
|
assert.ok(ctx.body.destroy.called)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('not call destroy if body is pipe but is closed', function() {
|
|
|
|
const ctx = createCtx({
|
|
|
|
body: {
|
|
|
|
pipe: {},
|
|
|
|
closed: true,
|
|
|
|
destroy: spy(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let flaska = new Flaska({}, fakerHttp)
|
|
|
|
flaska._afterCompiled = function() {}
|
|
|
|
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnded(ctx)
|
2022-03-17 15:09:22 +00:00
|
|
|
|
|
|
|
assert.notOk(ctx.body.destroy.called)
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
t.test('calls afterCompiled correctly', function() {
|
|
|
|
const assertError = new Error('test')
|
|
|
|
const assertCtx = createCtx()
|
|
|
|
let flaska = new Flaska({}, fakerHttp)
|
|
|
|
|
|
|
|
flaska._afterCompiled = function(ctx) {
|
|
|
|
assert.strictEqual(ctx, assertCtx)
|
|
|
|
throw assertError
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.throws(function() {
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__requestEnded(assertCtx)
|
2021-10-09 00:12:56 +00:00
|
|
|
}, /test/)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('calls afterAsyncCompiled correctly if defined', async function() {
|
|
|
|
const assertError = new Error('test')
|
|
|
|
const assertCtx = createCtx()
|
|
|
|
let flaska = new Flaska({}, fakerHttp)
|
2024-11-18 03:57:51 +00:00
|
|
|
flaska.__compile()
|
2021-10-09 00:12:56 +00:00
|
|
|
|
|
|
|
flaska._afterAsyncCompiled = function(ctx) {
|
|
|
|
assert.strictEqual(ctx, assertCtx)
|
|
|
|
return Promise.resolve().then(function() { return Promise.reject(assertError) })
|
|
|
|
}
|
|
|
|
|
2024-11-18 03:57:51 +00:00
|
|
|
let err = await assert.isRejected(flaska.__requestEnded(assertCtx))
|
2021-10-09 00:12:56 +00:00
|
|
|
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
})
|
|
|
|
})
|