flaska/test/middlewares.test.mjs

49 lines
1.4 KiB
JavaScript
Raw Normal View History

import { Eltro as t, assert} from 'eltro'
import { QueryHandler, JsonHandler } from '../flaska.mjs'
t.describe('#QueryHandler()', function() {
let queryHandler = QueryHandler()
t.test('should return a handler', function() {
assert.strictEqual(typeof(queryHandler), 'function')
})
t.test('should support separating query from request url', function() {
const assertItem1 = 'safdsfdsag'
const assertItem2 = 'hello%20world'
const ctx = {
req: {
url: `/some/path?item1=${assertItem1}&ITEM2=${assertItem2}`
}
}
queryHandler(ctx)
assert.strictEqual(ctx.query.get('item1'), assertItem1)
assert.strictEqual(ctx.query.get('ITEM2'), 'hello world')
})
})
t.describe('#JsonHandler()', function() {
let jsonHandler = JsonHandler()
t.test('should return a handler', function() {
assert.strictEqual(typeof(jsonHandler), 'function')
})
t.test('should support separating query from request url', async function() {
const assertBody = { a: 1, temp: 'test', hello: 'world'}
let parsed = JSON.stringify(assertBody)
const ctx = {
req: [
Promise.resolve(Buffer.from(parsed.slice(0, parsed.length / 2))),
Promise.resolve(Buffer.from(parsed.slice(parsed.length / 2))),
]
}
await jsonHandler(ctx)
assert.notStrictEqual(ctx.req.body, assertBody)
assert.deepStrictEqual(ctx.req.body, assertBody)
})
})