UNPKG

2.1 kBJavaScriptView Raw
1'use strict'
2
3const { test } = require('tap')
4const split = require('split2')
5const Fastify = require('..')
6
7test('skip automatic reply.send() with reply.sent = true and a body', (t) => {
8 const stream = split(JSON.parse)
9 const app = Fastify({
10 logger: {
11 stream: stream
12 }
13 })
14
15 stream.on('data', (line) => {
16 t.notEqual(line.level, 40) // there are no errors
17 t.notEqual(line.level, 50) // there are no errors
18 })
19
20 app.get('/', (req, reply) => {
21 reply.sent = true
22 reply.res.end('hello world')
23
24 return Promise.resolve('this will be skipped')
25 })
26
27 return app.inject({
28 method: 'GET',
29 url: '/'
30 }).then((res) => {
31 t.equal(res.statusCode, 200)
32 t.equal(res.body, 'hello world')
33 })
34})
35
36test('skip automatic reply.send() with reply.sent = true and no body', (t) => {
37 const stream = split(JSON.parse)
38 const app = Fastify({
39 logger: {
40 stream: stream
41 }
42 })
43
44 stream.on('data', (line) => {
45 t.notEqual(line.level, 40) // there are no error
46 t.notEqual(line.level, 50) // there are no error
47 })
48
49 app.get('/', (req, reply) => {
50 reply.sent = true
51 reply.res.end('hello world')
52
53 return Promise.resolve()
54 })
55
56 return app.inject({
57 method: 'GET',
58 url: '/'
59 }).then((res) => {
60 t.equal(res.statusCode, 200)
61 t.equal(res.body, 'hello world')
62 })
63})
64
65test('skip automatic reply.send() with reply.sent = true and an error', (t) => {
66 const stream = split(JSON.parse)
67 const app = Fastify({
68 logger: {
69 stream: stream
70 }
71 })
72
73 let errorSeen = false
74
75 stream.on('data', (line) => {
76 if (line.level === 50) {
77 errorSeen = true
78 t.equal(line.err.message, 'kaboom')
79 t.equal(line.msg, 'Promise errored, but reply.sent = true was set')
80 }
81 })
82
83 app.get('/', (req, reply) => {
84 reply.sent = true
85 reply.res.end('hello world')
86
87 return Promise.reject(new Error('kaboom'))
88 })
89
90 return app.inject({
91 method: 'GET',
92 url: '/'
93 }).then((res) => {
94 t.equal(errorSeen, true)
95 t.equal(res.statusCode, 200)
96 t.equal(res.body, 'hello world')
97 })
98})