forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.test.js
2622 lines (2386 loc) · 79.4 KB
/
index.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-env jest */
import http from 'http'
import url from 'url'
import stripAnsi from 'strip-ansi'
import fs from 'fs-extra'
import { join } from 'path'
import WebSocket from 'ws'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
renderViaHTTP,
getBrowserBodyText,
waitFor,
normalizeRegEx,
initNextServerScript,
nextExport,
hasRedbox,
check,
} from 'next-test-utils'
let appDir = join(__dirname, '..')
const nextConfigPath = join(appDir, 'next.config.js')
let externalServerHits = new Set()
let nextConfigRestoreContent
let nextConfigContent
let externalServerPort
let externalServer
let stdout = ''
let stderr = ''
let buildId
let appPort
let app
const runTests = (isDev = false) => {
it('should successfully rewrite a WebSocket request', async () => {
const messages = []
const ws = await new Promise((resolve, reject) => {
let socket = new WebSocket(`ws://localhost:${appPort}/to-websocket`)
socket.on('message', (data) => {
messages.push(data.toString())
})
socket.on('open', () => resolve(socket))
socket.on('error', (err) => {
console.error(err)
socket.close()
reject()
})
})
await check(
() => (messages.length > 0 ? 'success' : JSON.stringify(messages)),
'success'
)
ws.close()
expect([...externalServerHits]).toEqual(['/_next/webpack-hmr?page=/about'])
})
it('should not rewrite for _next/data route when a match is found', async () => {
const initial = await fetchViaHTTP(appPort, '/overridden/first')
expect(initial.status).toBe(200)
expect(await initial.text()).toContain('this page is overridden')
const nextData = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/overridden/first.json`
)
expect(nextData.status).toBe(200)
expect(await nextData.json()).toEqual({
pageProps: { params: { slug: 'first' } },
__N_SSG: true,
})
})
it('should handle has query encoding correctly', async () => {
for (const expected of [
{
post: 'first',
slug: ['first'],
},
{
post: 'hello%20world',
slug: ['hello world'],
},
{
post: 'hello/world',
slug: ['hello', 'world'],
},
{
post: 'hello%2fworld',
slug: ['hello', 'world'],
},
]) {
const { status = 200, post } = expected
const res = await fetchViaHTTP(
appPort,
'/has-rewrite-8',
`?post=${post}`,
{
redirect: 'manual',
}
)
expect(res.status).toBe(status)
if (status === 200) {
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#props').text())).toEqual({
params: {
slug: expected.slug,
},
})
}
}
})
it('should handle external beforeFiles rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/overridden')
const html = await res.text()
if (res.status !== 200) {
console.error('Invalid response', html)
}
expect(res.status).toBe(200)
expect(html).toContain('Example Domain')
const browser = await webdriver(appPort, '/nav')
await browser.elementByCss('#to-before-files-overridden').click()
await check(
() => browser.eval('document.documentElement.innerHTML'),
/Example Domain/
)
})
it('should handle beforeFiles rewrite to dynamic route correctly', async () => {
const res = await fetchViaHTTP(appPort, '/nfl')
const html = await res.text()
if (res.status !== 200) {
console.error('Invalid response', html)
}
expect(res.status).toBe(200)
expect(html).toContain('/_sport/[slug]')
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser.elementByCss('#to-before-files-dynamic').click()
await check(
() => browser.eval('document.documentElement.innerHTML'),
/_sport\/\[slug\]/
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'nfl',
})
expect(await browser.elementByCss('#pathname').text()).toBe(
'/_sport/[slug]'
)
expect(await browser.eval('window.beforeNav')).toBe(1)
})
it('should handle beforeFiles rewrite to partly dynamic route correctly', async () => {
const res = await fetchViaHTTP(appPort, '/nfl')
const html = await res.text()
if (res.status !== 200) {
console.error('Invalid response', html)
}
expect(res.status).toBe(200)
expect(html).toContain('/_sport/[slug]')
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser.elementByCss('#to-before-files-dynamic-again').click()
await check(
() => browser.eval('document.documentElement.innerHTML'),
/_sport\/\[slug\]\/test/
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'nfl',
})
expect(await browser.elementByCss('#pathname').text()).toBe(
'/_sport/[slug]/test'
)
expect(await browser.eval('window.beforeNav')).toBe(1)
})
it('should support long URLs for rewrites', async () => {
const res = await fetchViaHTTP(
appPort,
'/catchall-rewrite/a9btBxtHQALZ6cxfuj18X6OLGNSkJVzrOXz41HG4QwciZfn7ggRZzPx21dWqGiTBAqFRiWvVNm5ko2lpyso5jtVaXg88dC1jKfqI2qmIcdeyJat8xamrIh2LWnrYRrsBcoKfQU65KHod8DPANuzPS3fkVYWlmov05GQbc82HwR1exOvPVKUKb5gBRWiN0WOh7hN4QyezIuq3dJINAptFQ6m2bNGjYACBRk4MOSHdcQG58oq5Ch7luuqrl9EcbWSa'
)
const html = await res.text()
expect(res.status).toBe(200)
expect(html).toContain('/with-params')
})
it('should resolveHref correctly navigating through history', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1')
expect(await browser.eval('document.documentElement.innerHTML')).toContain(
'multi-rewrites'
)
await browser.eval('next.router.push("/rewriting-to-auto-export")')
await browser.waitForElementByCss('#auto-export')
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'hello',
rewrite: '1',
})
expect(await browser.eval('window.beforeNav')).toBe(1)
await browser.eval('next.router.push("/nav")')
await browser.waitForElementByCss('#nav')
expect(await browser.elementByCss('#nav').text()).toBe('Nav')
await browser.back()
await browser.waitForElementByCss('#auto-export')
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'hello',
rewrite: '1',
})
expect(await browser.eval('window.beforeNav')).toBe(1)
if (isDev) {
expect(await hasRedbox(browser, false)).toBe(false)
}
})
it('should continue in beforeFiles rewrites', async () => {
const res = await fetchViaHTTP(appPort, '/old-blog/about')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#hello').text()).toContain('Hello')
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-old-blog')
.click()
.waitForElementByCss('#hello')
expect(await browser.elementByCss('#hello').text()).toContain('Hello')
})
it('should not hang when proxy rewrite fails', async () => {
const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
timeout: 5000,
})
expect(res.status).toBe(500)
})
it('should parse params correctly for rewrite to auto-export dynamic page', async () => {
const browser = await webdriver(appPort, '/rewriting-to-auto-export')
await check(
() => browser.eval(() => document.documentElement.innerHTML),
/auto-export.*?hello/
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
rewrite: '1',
slug: 'hello',
})
})
it('should provide params correctly for rewrite to auto-export non-dynamic page', async () => {
const browser = await webdriver(
appPort,
'/rewriting-to-another-auto-export/first'
)
expect(await browser.elementByCss('#auto-export-another').text()).toBe(
'auto-export another'
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
rewrite: '1',
path: ['first'],
})
})
it('should handle one-to-one rewrite successfully', async () => {
const html = await renderViaHTTP(appPort, '/first')
expect(html).toMatch(/hello/)
})
it('should handle chained rewrites successfully', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/multi-rewrites/)
})
it('should handle param like headers properly', async () => {
const res = await fetchViaHTTP(appPort, '/my-other-header/my-path')
expect(res.headers.get('x-path')).toBe('my-path')
expect(res.headers.get('somemy-path')).toBe('hi')
expect(res.headers.get('x-test')).toBe('some:value*')
expect(res.headers.get('x-test-2')).toBe('value*')
expect(res.headers.get('x-test-3')).toBe(':value?')
expect(res.headers.get('x-test-4')).toBe(':value+')
expect(res.headers.get('x-test-5')).toBe('something https:')
expect(res.headers.get('x-test-6')).toBe(':hello(world)')
expect(res.headers.get('x-test-7')).toBe('hello(world)')
expect(res.headers.get('x-test-8')).toBe('hello{1,}')
expect(res.headers.get('x-test-9')).toBe(':hello{1,2}')
expect(res.headers.get('content-security-policy')).toBe(
"default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com/my-path"
)
})
it('should not match dynamic route immediately after applying header', async () => {
const res = await fetchViaHTTP(appPort, '/blog/post-321')
expect(res.headers.get('x-something')).toBe('applied-everywhere')
const $ = cheerio.load(await res.text())
expect(JSON.parse($('p').text()).path).toBe('blog')
})
it('should handle chained redirects successfully', async () => {
const res1 = await fetchViaHTTP(appPort, '/redir-chain1', undefined, {
redirect: 'manual',
})
const res1location = url.parse(res1.headers.get('location')).pathname
expect(res1.status).toBe(301)
expect(res1location).toBe('/redir-chain2')
const res2 = await fetchViaHTTP(appPort, res1location, undefined, {
redirect: 'manual',
})
const res2location = url.parse(res2.headers.get('location')).pathname
expect(res2.status).toBe(302)
expect(res2location).toBe('/redir-chain3')
const res3 = await fetchViaHTTP(appPort, res2location, undefined, {
redirect: 'manual',
})
const res3location = url.parse(res3.headers.get('location')).pathname
expect(res3.status).toBe(303)
expect(res3location).toBe('/')
})
it('should not match redirect for /_next', async () => {
const res = await fetchViaHTTP(
appPort,
'/_next/has-redirect-5',
undefined,
{
headers: {
'x-test-next': 'true',
},
redirect: 'manual',
}
)
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(
appPort,
'/another/has-redirect-5',
undefined,
{
headers: {
'x-test-next': 'true',
},
redirect: 'manual',
}
)
expect(res2.status).toBe(307)
})
it('should redirect successfully with permanent: false', async () => {
const res = await fetchViaHTTP(appPort, '/redirect1', undefined, {
redirect: 'manual',
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(307)
expect(pathname).toBe('/')
})
it('should redirect with params successfully', async () => {
const res = await fetchViaHTTP(appPort, '/hello/123/another', undefined, {
redirect: 'manual',
})
const { pathname } = url.parse(res.headers.get('location'))
expect(res.status).toBe(307)
expect(pathname).toBe('/blog/123')
})
it('should redirect with hash successfully', async () => {
const res = await fetchViaHTTP(
appPort,
'/docs/router-status/500',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hash, query } = url.parse(
res.headers.get('location'),
true
)
expect(res.status).toBe(301)
expect(pathname).toBe('/docs/v2/network/status-codes')
expect(hash).toBe('#500')
expect(query).toEqual({})
})
it('should redirect successfully with provided statusCode', async () => {
const res = await fetchViaHTTP(appPort, '/redirect2', undefined, {
redirect: 'manual',
})
const { pathname, query } = url.parse(res.headers.get('location'), true)
expect(res.status).toBe(301)
expect(pathname).toBe('/')
expect(query).toEqual({})
})
it('should redirect successfully with catchall', async () => {
const res = await fetchViaHTTP(
appPort,
'/catchall-redirect/hello/world',
undefined,
{
redirect: 'manual',
}
)
const { pathname, query } = url.parse(res.headers.get('location'), true)
expect(res.status).toBe(307)
expect(pathname).toBe('/somewhere')
expect(query).toEqual({})
})
it('should server static files through a rewrite', async () => {
const text = await renderViaHTTP(appPort, '/hello-world')
expect(text).toBe('hello world!')
})
it('should rewrite with params successfully', async () => {
const html = await renderViaHTTP(appPort, '/test/hello')
expect(html).toMatch(/Hello/)
})
it('should not append params when one is used in destination path', async () => {
const html = await renderViaHTTP(appPort, '/test/with-params?a=b')
const $ = cheerio.load(html)
expect(JSON.parse($('p').text())).toEqual({ a: 'b' })
})
it('should double redirect successfully', async () => {
const html = await renderViaHTTP(appPort, '/docs/github')
expect(html).toMatch(/hi there/)
})
it('should allow params in query for rewrite', async () => {
const html = await renderViaHTTP(appPort, '/query-rewrite/hello/world?a=b')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').html()).query).toEqual({
first: 'hello',
second: 'world',
a: 'b',
section: 'hello',
name: 'world',
})
})
it('should have correct params for catchall rewrite', async () => {
const html = await renderViaHTTP(
appPort,
'/catchall-rewrite/hello/world?a=b'
)
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').html()).query).toEqual({
a: 'b',
path: ['hello', 'world'],
})
})
it('should have correct encoding for params with catchall rewrite', async () => {
const html = await renderViaHTTP(
appPort,
'/catchall-rewrite/hello%20world%3Fw%3D24%26focalpoint%3Dcenter?a=b'
)
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').html()).query).toEqual({
a: 'b',
path: ['hello%20world%3Fw%3D24%26focalpoint%3Dcenter'],
})
})
it('should have correct query for catchall rewrite', async () => {
const html = await renderViaHTTP(appPort, '/catchall-query/hello/world?a=b')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').html()).query).toEqual({
a: 'b',
another: 'hello/world',
path: ['hello', 'world'],
})
})
it('should have correct header for catchall rewrite', async () => {
const res = await fetchViaHTTP(appPort, '/catchall-header/hello/world?a=b')
const headerValue = res.headers.get('x-value')
expect(headerValue).toBe('hello/world')
})
it('should allow params in query for redirect', async () => {
const res = await fetchViaHTTP(
appPort,
'/query-redirect/hello/world?a=b',
undefined,
{
redirect: 'manual',
}
)
const { pathname, query } = url.parse(res.headers.get('location'), true)
expect(res.status).toBe(307)
expect(pathname).toBe('/with-params')
expect(query).toEqual({
first: 'hello',
second: 'world',
a: 'b',
})
})
it('should have correctly encoded params in query for redirect', async () => {
const res = await fetchViaHTTP(
appPort,
'/query-redirect/hello%20world%3Fw%3D24%26focalpoint%3Dcenter/world?a=b',
undefined,
{
redirect: 'manual',
}
)
const { pathname, query } = url.parse(res.headers.get('location'), true)
expect(res.status).toBe(307)
expect(pathname).toBe('/with-params')
expect(query).toEqual({
// this should be decoded since url.parse decodes query values
first: 'hello world?w=24&focalpoint=center',
second: 'world',
a: 'b',
})
})
it('should overwrite param values correctly', async () => {
const html = await renderViaHTTP(appPort, '/test-overwrite/first/second')
expect(html).toMatch(/this-should-be-the-value/)
expect(html).not.toMatch(/first/)
expect(html).toMatch(/second/)
})
it('should handle query for rewrite correctly', async () => {
// query merge order lowest priority to highest
// 1. initial URL query values
// 2. path segment values
// 3. destination specified query values
const html = await renderViaHTTP(
appPort,
'/query-rewrite/first/second?section=overridden&name=overridden&first=overridden&second=overridden&keep=me'
)
const data = JSON.parse(cheerio.load(html)('p').text())
expect(data).toEqual({
first: 'first',
second: 'second',
section: 'first',
name: 'second',
keep: 'me',
})
})
// current routes order do not allow rewrites to override page
// but allow redirects to
it('should not allow rewrite to override page file', async () => {
const html = await renderViaHTTP(appPort, '/nav')
expect(html).toContain('to-hello')
})
it('show allow redirect to override the page', async () => {
const res = await fetchViaHTTP(appPort, '/redirect-override', undefined, {
redirect: 'manual',
})
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/thank-you-next')
})
it('should work successfully on the client', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.elementByCss('#to-hello').click()
await browser.waitForElementByCss('#hello')
expect(await browser.eval('window.location.href')).toMatch(/\/first$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello/)
await browser.eval('window.location.href = window.location.href')
await waitFor(500)
expect(await browser.eval('window.location.href')).toMatch(/\/first$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello/)
await browser.elementByCss('#to-nav').click()
await browser.waitForElementByCss('#to-hello-again')
await browser.elementByCss('#to-hello-again').click()
await browser.waitForElementByCss('#hello-again')
expect(await browser.eval('window.location.href')).toMatch(/\/second$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello again/)
await browser.eval('window.location.href = window.location.href')
await waitFor(500)
expect(await browser.eval('window.location.href')).toMatch(/\/second$/)
expect(await getBrowserBodyText(browser)).toMatch(/Hello again/)
})
it('should work with rewrite when manually specifying href/as', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-params-manual')
.click()
.waitForElementByCss('#query')
expect(await browser.eval('window.beforeNav')).toBe(1)
const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({
something: '1',
another: 'value',
})
})
it('should work with rewrite when only specifying href', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-params')
.click()
.waitForElementByCss('#query')
expect(await browser.eval('window.beforeNav')).toBe(1)
const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({
something: '1',
another: 'value',
})
})
it('should work with rewrite when only specifying href and ends in dynamic route', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-rewritten-dynamic')
.click()
.waitForElementByCss('#auto-export')
expect(await browser.eval('window.beforeNav')).toBe(1)
const text = await browser.eval(() => document.documentElement.innerHTML)
expect(text).toContain('auto-export hello')
})
it('should match a page after a rewrite', async () => {
const html = await renderViaHTTP(appPort, '/to-hello')
expect(html).toContain('Hello')
})
it('should match dynamic route after rewrite', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
expect(html).toMatch(/post:.*?post-2/)
})
it('should match public file after rewrite', async () => {
const data = await renderViaHTTP(appPort, '/blog/data.json')
expect(JSON.parse(data)).toEqual({ hello: 'world' })
})
it('should match /_next file after rewrite', async () => {
await renderViaHTTP(appPort, '/hello')
const data = await renderViaHTTP(
appPort,
`/hidden/_next/static/${buildId}/_buildManifest.js`
)
expect(data).toContain('/hello')
})
it('should allow redirecting to external resource', async () => {
const res = await fetchViaHTTP(appPort, '/to-external', undefined, {
redirect: 'manual',
})
const location = res.headers.get('location')
expect(res.status).toBe(307)
expect(location).toBe('https://google.com/')
})
it('should apply headers for exact match', async () => {
const res = await fetchViaHTTP(appPort, '/add-header')
expect(res.headers.get('x-custom-header')).toBe('hello world')
expect(res.headers.get('x-another-header')).toBe('hello again')
})
it('should apply headers for multi match', async () => {
const res = await fetchViaHTTP(appPort, '/my-headers/first')
expect(res.headers.get('x-first-header')).toBe('first')
expect(res.headers.get('x-second-header')).toBe('second')
})
it('should apply params for header key/values', async () => {
const res = await fetchViaHTTP(appPort, '/my-other-header/first')
expect(res.headers.get('x-path')).toBe('first')
expect(res.headers.get('somefirst')).toBe('hi')
})
it('should support URL for header key/values', async () => {
const res = await fetchViaHTTP(appPort, '/without-params/url')
expect(res.headers.get('x-origin')).toBe('https://example.com')
})
it('should apply params header key/values with URL', async () => {
const res = await fetchViaHTTP(appPort, '/with-params/url/first')
expect(res.headers.get('x-url')).toBe('https://example.com/first')
})
it('should apply params header key/values with URL that has port', async () => {
const res = await fetchViaHTTP(appPort, '/with-params/url2/first')
expect(res.headers.get('x-url')).toBe(
'https://example.com:8080?hello=first'
)
})
it('should support named pattern for header key/values', async () => {
const res = await fetchViaHTTP(appPort, '/named-pattern/hello')
expect(res.headers.get('x-something')).toBe('value=hello')
expect(res.headers.get('path-hello')).toBe('end')
})
it('should support proxying to external resource', async () => {
const res = await fetchViaHTTP(appPort, '/proxy-me/first?keep=me&and=me')
expect(res.status).toBe(200)
expect(
[...externalServerHits].map((u) => {
const { pathname, query } = url.parse(u, true)
return {
pathname,
query,
}
})
).toEqual([
{
pathname: '/first',
query: {
keep: 'me',
and: 'me',
},
},
])
const nextHost = `localhost:${appPort}`
const externalHost = `localhost:${externalServerPort}`
expect(await res.text()).toContain(`hi ${nextHost} from ${externalHost}`)
})
it('should support unnamed parameters correctly', async () => {
const res = await fetchViaHTTP(appPort, '/unnamed/first/final', undefined, {
redirect: 'manual',
})
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/got-unnamed')
})
it('should support named like unnamed parameters correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/named-like-unnamed/first',
undefined,
{
redirect: 'manual',
}
)
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/first')
})
it('should add refresh header for 308 redirect', async () => {
const res = await fetchViaHTTP(appPort, '/redirect4', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(308)
expect(res.headers.get('refresh')).toBe(`0;url=/`)
})
it('should have correctly encoded query in location and refresh headers', async () => {
const res = await fetchViaHTTP(
appPort,
// Query unencoded is ?テスト=あ
'/redirect4?%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82',
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(308)
expect(res.headers.get('location').split('?')[1]).toBe(
'%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82'
)
expect(res.headers.get('refresh')).toBe(
'0;url=/?%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82'
)
})
it('should handle basic api rewrite successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello')
expect(JSON.parse(data)).toEqual({ query: {} })
})
it('should handle api rewrite with un-named param successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello-regex/hello/world')
expect(JSON.parse(data)).toEqual({
query: { name: 'hello/world', first: 'hello/world' },
})
})
it('should handle api rewrite with param successfully', async () => {
const data = await renderViaHTTP(appPort, '/api-hello-param/hello')
expect(JSON.parse(data)).toEqual({
query: { name: 'hello', hello: 'hello' },
})
})
it('should handle encoded value in the pathname correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/redirect/me/to-about/' + encodeURI('\\google.com'),
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname, query } = url.parse(
res.headers.get('location') || '',
true
)
expect(res.status).toBe(307)
expect(pathname).toBe(encodeURI('/\\google.com/about'))
expect(hostname).not.toBe('google.com')
expect(query).toEqual({})
})
it('should handle unnamed parameters with multi-match successfully', async () => {
const html = await renderViaHTTP(
appPort,
'/unnamed-params/nested/first/second/hello/world'
)
const params = JSON.parse(cheerio.load(html)('p').text())
expect(params).toEqual({ test: 'hello' })
})
it('should handle named regex parameters with multi-match successfully', async () => {
const res = await fetchViaHTTP(
appPort,
'/docs/integrations/v2-some/thing',
undefined,
{
redirect: 'manual',
}
)
const { pathname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe('/integrations/-some/thing')
})
it('should redirect with URL in query correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/to-external-with-query',
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(307)
expect(res.headers.get('location')).toBe(
'https://authserver.example.com/set-password?returnUrl=https://www.example.com/login'
)
})
it('should redirect with URL in query correctly non-encoded', async () => {
const res = await fetchViaHTTP(
appPort,
'/to-external-with-query',
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(307)
expect(res.headers.get('location')).toBe(
'https://authserver.example.com/set-password?returnUrl=https://www.example.com/login'
)
})
it('should match has header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, {
headers: {
'x-my-header': 'hello world!!',
},
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
myHeader: 'hello world!!',
})
const res2 = await fetchViaHTTP(appPort, '/has-rewrite-1')
expect(res2.status).toBe(404)
})
it('should match has query rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-2', {
'my-query': 'hellooo',
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
'my-query': 'hellooo',
myquery: 'hellooo',
value: 'hellooo',
})
const res2 = await fetchViaHTTP(appPort, '/has-rewrite-2')
expect(res2.status).toBe(404)
})
it('should match has cookie rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-3', undefined, {
headers: {
cookie: 'loggedIn=true',
},
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
loggedIn: 'true',
authorized: '1',
})
const res2 = await fetchViaHTTP(appPort, '/has-rewrite-3')
expect(res2.status).toBe(404)
})
it('should match has host rewrite correctly', async () => {
const res1 = await fetchViaHTTP(appPort, '/has-rewrite-4')
expect(res1.status).toBe(404)
const res = await fetchViaHTTP(appPort, '/has-rewrite-4', undefined, {
headers: {
host: 'example.com',
},
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
host: '1',
})
const res2 = await fetchViaHTTP(appPort, '/has-rewrite-3')
expect(res2.status).toBe(404)
})
it('should pass has segment for rewrite correctly', async () => {
const res1 = await fetchViaHTTP(appPort, '/has-rewrite-5')
expect(res1.status).toBe(404)
const res = await fetchViaHTTP(appPort, '/has-rewrite-5', {
hasParam: 'with-params',
})