forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-type.test.js
42 lines (38 loc) · 1.31 KB
/
content-type.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
const { test } = require('node:test')
const Fastify = require('..')
test('should remove content-type for setErrorHandler', async t => {
t.plan(8)
let count = 0
const fastify = Fastify()
fastify.setErrorHandler(function (error, request, reply) {
t.assert.strictEqual(error.message, 'kaboom')
t.assert.strictEqual(reply.hasHeader('content-type'), false)
reply.code(400).send({ foo: 'bar' })
})
fastify.addHook('onSend', async function (request, reply, payload) {
count++
t.assert.strictEqual(typeof payload, 'string')
switch (count) {
case 1: {
// should guess the correct content-type based on payload
t.assert.strictEqual(reply.getHeader('content-type'), 'text/plain; charset=utf-8')
throw Error('kaboom')
}
case 2: {
// should guess the correct content-type based on payload
t.assert.strictEqual(reply.getHeader('content-type'), 'application/json; charset=utf-8')
return payload
}
default: {
t.fail('should not reach')
}
}
})
fastify.get('/', function (request, reply) {
reply.send('plain-text')
})
const { statusCode, body } = await fastify.inject({ method: 'GET', path: '/' })
t.assert.strictEqual(statusCode, 400)
t.assert.strictEqual(body, JSON.stringify({ foo: 'bar' }))
})