@fastify/express 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,338 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const fp = require('fastify-plugin')
5
+ const Fastify = require('fastify')
6
+ const sget = require('simple-get').concat
7
+ const expressPlugin = require('../index')
8
+
9
+ test('run hooks and middleware on default 404', t => {
10
+ t.plan(8)
11
+
12
+ const fastify = Fastify()
13
+
14
+ fastify
15
+ .register(expressPlugin)
16
+ .after(() => {
17
+ fastify.use(function (req, res, next) {
18
+ t.pass('middleware called')
19
+ next()
20
+ })
21
+ })
22
+
23
+ fastify.addHook('onRequest', function (req, res, next) {
24
+ t.pass('onRequest called')
25
+ next()
26
+ })
27
+
28
+ fastify.addHook('preHandler', function (request, reply, next) {
29
+ t.pass('preHandler called')
30
+ next()
31
+ })
32
+
33
+ fastify.addHook('onSend', function (request, reply, payload, next) {
34
+ t.pass('onSend called')
35
+ next()
36
+ })
37
+
38
+ fastify.addHook('onResponse', function (request, reply, next) {
39
+ t.pass('onResponse called')
40
+ next()
41
+ })
42
+
43
+ fastify.get('/', function (req, reply) {
44
+ reply.send({ hello: 'world' })
45
+ })
46
+
47
+ t.tearDown(fastify.close.bind(fastify))
48
+
49
+ fastify.listen(0, err => {
50
+ t.error(err)
51
+
52
+ sget({
53
+ method: 'PUT',
54
+ url: 'http://localhost:' + fastify.server.address().port,
55
+ body: JSON.stringify({ hello: 'world' }),
56
+ headers: { 'Content-Type': 'application/json' }
57
+ }, (err, response, body) => {
58
+ t.error(err)
59
+ t.strictEqual(response.statusCode, 404)
60
+ })
61
+ })
62
+ })
63
+
64
+ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
65
+ t.plan(8)
66
+
67
+ const fastify = Fastify()
68
+ t.teardown(fastify.close)
69
+ fastify.register(expressPlugin)
70
+
71
+ fastify.register(fp(function (instance, options, next) {
72
+ instance.addHook('onRequest', function (req, res, next) {
73
+ t.pass('onRequest called')
74
+ next()
75
+ })
76
+
77
+ instance.use(function (req, res, next) {
78
+ t.pass('middleware called')
79
+ next()
80
+ })
81
+
82
+ instance.addHook('preHandler', function (request, reply, next) {
83
+ t.pass('preHandler called')
84
+ next()
85
+ })
86
+
87
+ instance.addHook('onSend', function (request, reply, payload, next) {
88
+ t.pass('onSend called')
89
+ next()
90
+ })
91
+
92
+ instance.addHook('onResponse', function (request, reply, next) {
93
+ t.pass('onResponse called')
94
+ next()
95
+ })
96
+
97
+ next()
98
+ }))
99
+
100
+ fastify.get('/', function (req, reply) {
101
+ reply.send({ hello: 'world' })
102
+ })
103
+
104
+ fastify.listen(0, (err, address) => {
105
+ t.error(err)
106
+ sget({
107
+ method: 'POST',
108
+ url: address,
109
+ body: JSON.stringify({ hello: 'world' }),
110
+ headers: { 'Content-Type': 'application/json' }
111
+ }, (err, response, body) => {
112
+ t.error(err)
113
+ t.strictEqual(response.statusCode, 404)
114
+ })
115
+ })
116
+ })
117
+
118
+ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
119
+ t.plan(14)
120
+
121
+ const fastify = Fastify()
122
+ t.teardown(fastify.close)
123
+ fastify.register(expressPlugin)
124
+
125
+ const plugin = fp((instance, opts, next) => {
126
+ instance.addHook('onRequest', function (req, res, next) {
127
+ t.pass('onRequest called')
128
+ next()
129
+ })
130
+
131
+ instance.use(function (req, res, next) {
132
+ t.pass('middleware called')
133
+ next()
134
+ })
135
+
136
+ instance.addHook('preHandler', function (request, reply, next) {
137
+ t.pass('preHandler called')
138
+ next()
139
+ })
140
+
141
+ instance.addHook('onSend', function (request, reply, payload, next) {
142
+ t.pass('onSend called')
143
+ next()
144
+ })
145
+
146
+ instance.addHook('onResponse', function (request, reply, next) {
147
+ t.pass('onResponse called')
148
+ next()
149
+ })
150
+
151
+ next()
152
+ })
153
+
154
+ fastify.register(plugin)
155
+
156
+ fastify.get('/', function (req, reply) {
157
+ reply.send({ hello: 'world' })
158
+ })
159
+
160
+ fastify.setNotFoundHandler(function (req, reply) {
161
+ reply.code(404).send('this was not found')
162
+ })
163
+
164
+ fastify.register(plugin) // Registering plugin after handler also works
165
+
166
+ fastify.listen(0, (err, address) => {
167
+ t.error(err)
168
+ sget({
169
+ method: 'GET',
170
+ url: address + '/not-found'
171
+ }, (err, response, body) => {
172
+ t.error(err)
173
+ t.strictEqual(body.toString(), 'this was not found')
174
+ t.strictEqual(response.statusCode, 404)
175
+ })
176
+ })
177
+ })
178
+
179
+ test('run hooks and middleware with encapsulated 404', t => {
180
+ t.plan(13)
181
+
182
+ const fastify = Fastify()
183
+
184
+ fastify
185
+ .register(expressPlugin)
186
+ .after(() => {
187
+ fastify.use(function (req, res, next) {
188
+ t.pass('middleware called')
189
+ next()
190
+ })
191
+ })
192
+
193
+ fastify.addHook('onRequest', function (req, res, next) {
194
+ t.pass('onRequest called')
195
+ next()
196
+ })
197
+
198
+ fastify.addHook('preHandler', function (request, reply, next) {
199
+ t.pass('preHandler called')
200
+ next()
201
+ })
202
+
203
+ fastify.addHook('onSend', function (request, reply, payload, next) {
204
+ t.pass('onSend called')
205
+ next()
206
+ })
207
+
208
+ fastify.addHook('onResponse', function (request, reply, next) {
209
+ t.pass('onResponse called')
210
+ next()
211
+ })
212
+
213
+ fastify.register(function (f, opts, next) {
214
+ f.setNotFoundHandler(function (req, reply) {
215
+ reply.code(404).send('this was not found 2')
216
+ })
217
+
218
+ f.addHook('onRequest', function (req, res, next) {
219
+ t.pass('onRequest 2 called')
220
+ next()
221
+ })
222
+
223
+ f.use(function (req, res, next) {
224
+ t.pass('middleware 2 called')
225
+ next()
226
+ })
227
+
228
+ f.addHook('preHandler', function (request, reply, next) {
229
+ t.pass('preHandler 2 called')
230
+ next()
231
+ })
232
+
233
+ f.addHook('onSend', function (request, reply, payload, next) {
234
+ t.pass('onSend 2 called')
235
+ next()
236
+ })
237
+
238
+ f.addHook('onResponse', function (request, reply, next) {
239
+ t.pass('onResponse 2 called')
240
+ next()
241
+ })
242
+
243
+ next()
244
+ }, { prefix: '/test' })
245
+
246
+ t.tearDown(fastify.close.bind(fastify))
247
+
248
+ fastify.listen(0, err => {
249
+ t.error(err)
250
+
251
+ sget({
252
+ method: 'PUT',
253
+ url: 'http://localhost:' + fastify.server.address().port + '/test',
254
+ body: JSON.stringify({ hello: 'world' }),
255
+ headers: { 'Content-Type': 'application/json' }
256
+ }, (err, response, body) => {
257
+ t.error(err)
258
+ t.strictEqual(response.statusCode, 404)
259
+ })
260
+ })
261
+ })
262
+
263
+ test('run middlewares on default 404', t => {
264
+ t.plan(4)
265
+
266
+ const fastify = Fastify()
267
+ fastify
268
+ .register(expressPlugin)
269
+ .after(() => {
270
+ fastify.use(function (req, res, next) {
271
+ t.pass('middleware called')
272
+ next()
273
+ })
274
+ })
275
+
276
+ fastify.get('/', function (req, reply) {
277
+ reply.send({ hello: 'world' })
278
+ })
279
+
280
+ t.tearDown(fastify.close.bind(fastify))
281
+
282
+ fastify.listen(0, err => {
283
+ t.error(err)
284
+
285
+ sget({
286
+ method: 'PUT',
287
+ url: 'http://localhost:' + fastify.server.address().port,
288
+ body: JSON.stringify({ hello: 'world' }),
289
+ headers: { 'Content-Type': 'application/json' }
290
+ }, (err, response, body) => {
291
+ t.error(err)
292
+ t.strictEqual(response.statusCode, 404)
293
+ })
294
+ })
295
+ })
296
+
297
+ test('run middlewares with encapsulated 404', t => {
298
+ t.plan(5)
299
+
300
+ const fastify = Fastify()
301
+ fastify
302
+ .register(expressPlugin)
303
+ .after(() => {
304
+ fastify.use(function (req, res, next) {
305
+ t.pass('middleware called')
306
+ next()
307
+ })
308
+ })
309
+
310
+ fastify.register(function (f, opts, next) {
311
+ f.setNotFoundHandler(function (req, reply) {
312
+ reply.code(404).send('this was not found 2')
313
+ })
314
+
315
+ f.use(function (req, res, next) {
316
+ t.pass('middleware 2 called')
317
+ next()
318
+ })
319
+
320
+ next()
321
+ }, { prefix: '/test' })
322
+
323
+ t.tearDown(fastify.close.bind(fastify))
324
+
325
+ fastify.listen(0, err => {
326
+ t.error(err)
327
+
328
+ sget({
329
+ method: 'PUT',
330
+ url: 'http://localhost:' + fastify.server.address().port + '/test',
331
+ body: JSON.stringify({ hello: 'world' }),
332
+ headers: { 'Content-Type': 'application/json' }
333
+ }, (err, response, body) => {
334
+ t.error(err)
335
+ t.strictEqual(response.statusCode, 404)
336
+ })
337
+ })
338
+ })
@@ -0,0 +1,179 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('fastify')
5
+ const Express = require('express')
6
+ const sget = require('simple-get').concat
7
+
8
+ const expressPlugin = require('../index')
9
+
10
+ test('Register express application', t => {
11
+ t.plan(5)
12
+ const fastify = Fastify()
13
+ const express = Express()
14
+ t.teardown(fastify.close)
15
+
16
+ express.use(function (req, res, next) {
17
+ res.setHeader('x-custom', true)
18
+ next()
19
+ })
20
+
21
+ express.get('/hello', (req, res) => {
22
+ res.status(201)
23
+ res.json({ hello: 'world' })
24
+ })
25
+
26
+ fastify.register(expressPlugin)
27
+ .after(() => { fastify.use(express) })
28
+
29
+ fastify.listen(0, (err, address) => {
30
+ t.error(err)
31
+ sget({
32
+ method: 'GET',
33
+ url: address + '/hello'
34
+ }, (err, res, data) => {
35
+ t.error(err)
36
+ t.strictEqual(res.statusCode, 201)
37
+ t.match(res.headers, { 'x-custom': 'true' })
38
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
39
+ })
40
+ })
41
+ })
42
+
43
+ test('Register express application that uses Router', t => {
44
+ t.plan(9)
45
+ const fastify = Fastify()
46
+ t.teardown(fastify.close)
47
+
48
+ const router = Express.Router()
49
+
50
+ router.use(function (req, res, next) {
51
+ res.setHeader('x-custom', true)
52
+ next()
53
+ })
54
+
55
+ router.get('/hello', (req, res) => {
56
+ res.status(201)
57
+ res.json({ hello: 'world' })
58
+ })
59
+
60
+ router.get('/foo', (req, res) => {
61
+ res.status(400)
62
+ res.json({ foo: 'bar' })
63
+ })
64
+
65
+ fastify.register(expressPlugin)
66
+ .after(() => { fastify.use(router) })
67
+
68
+ fastify.listen(0, (err, address) => {
69
+ t.error(err)
70
+ sget({
71
+ method: 'GET',
72
+ url: address + '/hello'
73
+ }, (err, res, data) => {
74
+ t.error(err)
75
+ t.strictEqual(res.statusCode, 201)
76
+ t.match(res.headers, { 'x-custom': 'true' })
77
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
78
+ })
79
+ sget({
80
+ method: 'GET',
81
+ url: address + '/foo'
82
+ }, (err, res, data) => {
83
+ t.error(err)
84
+ t.strictEqual(res.statusCode, 400)
85
+ t.match(res.headers, { 'x-custom': 'true' })
86
+ t.deepEqual(JSON.parse(data), { foo: 'bar' })
87
+ })
88
+ })
89
+ })
90
+
91
+ test('Should remove x-powered-by header', t => {
92
+ t.plan(3)
93
+ const fastify = Fastify()
94
+ t.teardown(fastify.close)
95
+
96
+ const router = Express.Router()
97
+
98
+ router.get('/', (req, res) => {
99
+ res.status(201)
100
+ res.json({ hello: 'world' })
101
+ })
102
+
103
+ fastify
104
+ .register(expressPlugin)
105
+ .after(() => { fastify.use(router) })
106
+
107
+ fastify.listen(0, (err, address) => {
108
+ t.error(err)
109
+ sget({
110
+ method: 'GET',
111
+ url: address
112
+ }, (err, res) => {
113
+ t.error(err)
114
+ t.strictEqual(res.headers['x-powered-by'], undefined)
115
+ })
116
+ })
117
+ })
118
+
119
+ test('Should expose the express app on the fastify instance', t => {
120
+ t.plan(3)
121
+ const fastify = Fastify()
122
+ t.teardown(fastify.close)
123
+
124
+ const router = Express.Router()
125
+
126
+ router.get('/', (req, res) => {
127
+ res.status(201)
128
+ res.json({ hello: 'world' })
129
+ })
130
+
131
+ fastify
132
+ .register(expressPlugin)
133
+ .after(() => { fastify.use(router) })
134
+
135
+ fastify.listen(0, (err, address) => {
136
+ t.error(err)
137
+ sget({
138
+ method: 'GET',
139
+ url: address
140
+ }, (err, res) => {
141
+ t.error(err)
142
+ t.strictEqual(fastify.express.disabled('x-powered-by'), true)
143
+ })
144
+ })
145
+ })
146
+
147
+ test('Should flush headers if express handles request', t => {
148
+ t.plan(3)
149
+ const fastify = Fastify()
150
+ t.teardown(fastify.close)
151
+
152
+ fastify.addHook('onRequest', (_, reply, done) => {
153
+ reply.header('foo', 'bar')
154
+
155
+ done()
156
+ })
157
+
158
+ const router = Express.Router()
159
+
160
+ router.get('/', (req, res) => {
161
+ res.status(201)
162
+ res.json({ hello: 'world' })
163
+ })
164
+
165
+ fastify
166
+ .register(expressPlugin)
167
+ .after(() => { fastify.use(router) })
168
+
169
+ fastify.listen(0, (err, address) => {
170
+ t.error(err)
171
+ sget({
172
+ method: 'GET',
173
+ url: address
174
+ }, (err, res) => {
175
+ t.error(err)
176
+ t.strictEqual(res.headers.foo, 'bar')
177
+ })
178
+ })
179
+ })