forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreply-code.test.js
148 lines (127 loc) · 3.43 KB
/
reply-code.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict'
const { test } = require('node:test')
const { Readable } = require('node:stream')
const Fastify = require('..')
test('code should handle null/undefined/float', (t, done) => {
t.plan(8)
const fastify = Fastify()
fastify.get('/null', function (request, reply) {
reply.status(null).send()
})
fastify.get('/undefined', function (request, reply) {
reply.status(undefined).send()
})
fastify.get('/404.5', function (request, reply) {
reply.status(404.5).send()
})
fastify.inject({
method: 'GET',
url: '/null'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 500)
t.assert.deepStrictEqual(res.json(), {
statusCode: 500,
code: 'FST_ERR_BAD_STATUS_CODE',
error: 'Internal Server Error',
message: 'Called reply with an invalid status code: null'
})
})
fastify.inject({
method: 'GET',
url: '/undefined'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 500)
t.assert.deepStrictEqual(res.json(), {
statusCode: 500,
code: 'FST_ERR_BAD_STATUS_CODE',
error: 'Internal Server Error',
message: 'Called reply with an invalid status code: undefined'
})
})
fastify.inject({
method: 'GET',
url: '/404.5'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
test('code should handle 204', (t, done) => {
t.plan(13)
const fastify = Fastify()
fastify.get('/204', function (request, reply) {
reply.status(204)
return null
})
fastify.get('/undefined/204', function (request, reply) {
reply.status(204).send({ message: 'hello' })
})
fastify.get('/stream/204', function (request, reply) {
const stream = new Readable({
read () {
this.push(null)
}
})
stream.on('end', () => {
t.assert.ok('stream ended')
})
reply.status(204).send(stream)
})
fastify.inject({
method: 'GET',
url: '/204'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 204)
t.assert.strictEqual(res.payload, '')
t.assert.strictEqual(res.headers['content-length'], undefined)
})
fastify.inject({
method: 'GET',
url: '/undefined/204'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 204)
t.assert.strictEqual(res.payload, '')
t.assert.strictEqual(res.headers['content-length'], undefined)
})
fastify.inject({
method: 'GET',
url: '/stream/204'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 204)
t.assert.strictEqual(res.payload, '')
t.assert.strictEqual(res.headers['content-length'], undefined)
done()
})
})
test('code should handle onSend hook on 204', (t, done) => {
t.plan(5)
const fastify = Fastify()
fastify.addHook('onSend', async function (request, reply, payload) {
return {
...payload,
world: 'hello'
}
})
fastify.get('/204', function (request, reply) {
reply.status(204).send({
hello: 'world'
})
})
fastify.inject({
method: 'GET',
url: '/204'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 204)
t.assert.strictEqual(res.payload, '')
t.assert.strictEqual(res.headers['content-length'], undefined)
t.assert.strictEqual(res.headers['content-type'], undefined)
done()
})
})