forked from fastify/fastify-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
615 lines (489 loc) · 13.4 KB
/
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
'use strict'
const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const Next = require('next')
const pino = require('pino')
const proxyquire = require('proxyquire')
const sinon = require('sinon')
test('should construct next with proper environment', t => {
t.plan(2)
process.env.NODE_ENV = 'production'
let options
const dev = process.env.NODE_ENV !== 'production'
t.equal(dev, false)
const app = Next(Object.assign({}, { dev }, options))
app.prepare()
.then(() => {
t.equal(app.dev, undefined)
})
app.close()
})
test('should return an html document', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(require('./index'))
.after(() => {
fastify.next('/hello')
})
fastify.inject({
url: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
})
})
test('should support different methods', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(require('./index'))
.after(() => {
fastify.next('/hello', { method: 'options' })
})
fastify.inject({
url: '/hello',
method: 'OPTIONS'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
})
})
test('should support a custom handler', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(require('./index'))
.after(() => {
fastify.next('/hello', (app, req, reply) => {
app.render(req.raw, reply.raw, '/hello', req.query, {})
})
})
fastify.inject({
url: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
})
})
test('should return 404 on undefined route', t => {
t.plan(2)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(require('./index'))
.after(() => {
fastify.next('/hello')
})
fastify.inject({
url: '/test',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
})
})
test('should throw if path is not a string', t => {
t.plan(2)
const fastify = Fastify()
fastify
.register(require('./index'))
.after(err => {
t.error(err)
try {
fastify.next(null)
t.fail()
} catch (e) {
t.equal(e.message, 'path must be a string')
}
})
fastify.close()
})
test('should throw if opts.method is not a string', t => {
t.plan(2)
const fastify = Fastify()
fastify
.register(require('./index'))
.after(err => {
t.error(err)
try {
fastify.next('/hello', { method: 1 })
t.fail()
} catch (e) {
t.equal(e.message, 'options.method must be a string')
}
})
fastify.close()
})
test('should throw if opts.schema is not an object', t => {
t.plan(2)
const fastify = Fastify()
fastify
.register(require('./index'))
.after(err => {
t.error(err)
try {
fastify.next('/hello', { schema: 1 })
t.fail()
} catch (e) {
t.equal(e.message, 'options.schema must be an object')
}
})
fastify.close()
})
test('should throw if callback is not a function', t => {
t.plan(2)
const fastify = Fastify()
fastify
.register(require('./index'))
.after(err => {
t.error(err)
try {
fastify.next('/hello', {}, 1)
t.fail()
} catch (e) {
t.equal(e.message, 'callback must be a function')
}
})
fastify.close()
})
test('should serve /_next/* static assets', async t => {
const manifest = require('./.next/build-manifest.json')
const fastify = Fastify()
fastify
.register(require('./index'))
.after(() => {
fastify.next('/hello')
})
t.teardown(() => fastify.close())
const commonAssets = manifest.pages['/hello']
t.ok(commonAssets.length > 0)
await Promise.all(commonAssets.map(suffix => testNextAsset(t, fastify, `/_next/${suffix}`)))
})
test('should serve /base_path/_next/* static assets when basePath defined', async t => {
const manifest = require('./.next/build-manifest.json')
const fastify = Fastify()
fastify
.register(require('./index'), {
conf: {
basePath: '/base_path'
}
})
.after(() => {
fastify.next('/hello')
})
t.teardown(() => fastify.close())
const commonAssets = manifest.pages['/hello']
t.ok(commonAssets.length > 0)
await Promise.all(commonAssets.map(suffix => testNextAsset(t, fastify, `/base_path/_next/${suffix}`)))
})
test('should not serve static assets with provided option noServeAssets: true', async t => {
const manifest = require('./.next/build-manifest.json')
const fastify = Fastify()
fastify
.register(require('./index'), {
noServeAssets: true,
underPressure: false
})
.after(() => {
fastify.next('/hello')
})
t.teardown(() => fastify.close())
const commonAssets = manifest.pages['/hello']
t.ok(commonAssets.length > 0)
await Promise.all(commonAssets.map(suffix => testNoServeNextAsset(t, fastify, `/_next/${suffix}`)))
})
test('should return a json data on api route', t => {
t.plan(3)
const fastify = Fastify()
fastify
.register(require('./index'))
.after(() => {
fastify.next('/api/*')
})
t.teardown(() => fastify.close())
fastify.inject({
url: '/api/user',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json')
})
})
test('should not log any errors', t => {
t.plan(5)
let showedError = false
const logger = pino({
level: 'error',
formatters: {
log: (obj) => {
showedError = true
return obj
}
}
})
const fastify = Fastify({
logger
})
fastify
.register(require('./index')).after(() => {
fastify.next('/hello')
})
fastify.inject({
url: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.match(res.payload, '<div>hello world</div>')
t.equal(showedError, false, 'Should not show any error')
})
})
test('should respect plugin logLevel', t => {
t.plan(9)
let didLog = false
const logger = pino({
formatters: {
log: (obj) => {
didLog = true
return obj
}
}
})
const fastify = Fastify({
logger
})
fastify
.register(require('./index'), {
logLevel: 'error'
})
.after(() => {
fastify.next('/hello')
fastify.next('/api/user', (nextApp, req, reply) => {
return nextApp
.getRequestHandler()(req.raw, reply.raw)
.then(() => {
reply.sent = true
})
})
})
t.teardown(() => fastify.close())
fastify.inject({
url: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.match(res.payload, '<div>hello world</div>')
t.equal(didLog, false)
})
fastify.inject({
url: '/api/user',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json')
t.equal(didLog, false)
})
})
test('should preserve Fastify response headers set by plugins and hooks', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify
.register(require('./index'))
.after(() => {
fastify.addHook('onRequest', (req, reply, done) => {
reply.header('test-header', 'hello')
done()
})
fastify.next('/hello', (app, req, reply) => {
app.render(req.raw, reply.raw, '/hello', req.query, {})
})
})
fastify.inject({
url: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers['test-header'], 'hello')
})
})
test('should handle Next initialization errors', t => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
const error = new Error('boom')
const plugin = proxyquire('./', {
next: function () {
return {
getRequestHandler () { },
prepare () { return Promise.reject(error) }
}
}
})
fastify
.register(plugin)
.ready(err => {
t.strictSame(err, error)
})
})
test('should not register under-pressure by default', t => {
const fastify = Fastify()
t.teardown(() => fastify.close())
const registerSpy = sinon.spy(fastify, 'register')
const underPressureStub = sinon.stub().resolves()
const plugin = proxyquire('./index', {
'under-pressure': underPressureStub
})
fastify.register(plugin)
sinon.assert.neverCalledWith(registerSpy, underPressureStub)
t.end()
})
test('should register under-pressure with default options when underPressure: true', async t => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
const plugin = proxyquire('./index', {
'under-pressure': async function (app, opts) {
t.same(opts, {})
}
})
await fastify.register(plugin, { underPressure: true })
})
test('should register under-pressure with provided options when it is an object', async t => {
t.plan(1)
const fastify = Fastify()
t.teardown(() => fastify.close())
const plugin = proxyquire('./index', {
'under-pressure': async function (app, opts) {
t.same(opts, { some: 'option' })
}
})
await fastify.register(plugin, { underPressure: { some: 'option' } })
})
test('should register under-pressure with underPressure: true - and expose route if configured', t => {
t.plan(2)
const fastify = Fastify()
t.teardown(() => fastify.close())
fastify.register(require('./index'), {
underPressure: {
exposeStatusRoute: true,
maxEventLoopDelay: 10000,
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000,
maxEventLoopUtilization: 0.98
}
})
fastify.inject({
url: '/status',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
})
})
test('should decorate with next render function', async t => {
t.plan(2)
const fastify = Fastify()
t.teardown(() => fastify.close())
await fastify.register(require('./index'))
fastify.addHook('onRequest', (req, reply, done) => {
reply.header('test-header', 'hello')
done()
})
fastify.get('/hello', (req, reply) => {
return reply.nextRender('/hello')
})
const res = await fastify.inject({
url: '/hello',
method: 'GET'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['test-header'], 'hello')
})
test('should let next render error page', async t => {
t.plan(4)
const fastify = Fastify()
t.teardown(() => fastify.close())
await fastify.register(require('./index'))
fastify.addHook('onRequest', (req, reply, done) => {
reply.header('test-header', 'hello')
done()
})
fastify.get('/hello', (req, reply) => {
throw new Error('boom')
})
fastify.setErrorHandler((err, req, reply) => {
reply.status(err.statusCode || 500)
return reply.nextRender('/hello')
})
const res = await fastify.inject({
url: '/hello',
method: 'GET'
})
t.equal(res.statusCode, 500)
t.equal(res.headers['test-header'], 'hello')
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.match(res.payload, '<div>hello world</div>')
})
async function testNextAsset (t, fastify, url) {
const res = await fastify.inject(url)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
}
async function testNoServeNextAsset (t, fastify, url) {
const res = await fastify.inject(url)
t.equal(res.statusCode, 404)
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
}
test('should preserve custom properties on the request when using onRequest hook', async t => {
t.plan(8)
const customProperty = { value: 'test' }
const fastify = Fastify()
t.teardown(() => fastify.close())
await fastify.register(require('./index'))
.after(() => {
fastify.next('/hello', (app, req, reply) => {
t.same(req.raw.customProperty, { value: 'test' })
app.render(req.raw, reply.raw, '/hello', req.query, {})
})
fastify.next('/custom_prop_page', (app, req, reply) => {
t.same(req.raw.customProperty, { value: 'test' })
app.render(req.raw, reply.raw, '/custom_prop_page', req.query, {})
})
})
fastify.addHook('onRequest', (req, reply, done) => {
req.raw.customProperty = customProperty
done()
})
let res = await fastify.inject({
url: '/hello',
method: 'GET'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.match(res.payload, '<div>hello world</div>')
res = await fastify.inject({
url: '/custom_prop_page',
method: 'GET'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
// for some reason React (or Next.js) adds <!-- --> in front of the property that is being evaluated on the page
t.match(res.payload, '<div>another hello world page, customProperty value: <!-- -->test</div>')
})