-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.test.ts
152 lines (135 loc) · 4.88 KB
/
index.test.ts
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
/* eslint-env jest */
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import { check, fetchViaHTTP } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'
describe('Middleware Redirect', () => {
let next: NextInstance
afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, '../app/pages')),
'middleware.js': new FileRef(join(__dirname, '../app/middleware.js')),
'next.config.js': new FileRef(join(__dirname, '../app/next.config.js')),
},
})
})
tests()
testsWithLocale()
testsWithLocale('/fr')
function tests() {
it('does not include the locale in redirects by default', async () => {
const res = await fetchViaHTTP(next.url, `/old-home`, undefined, {
redirect: 'manual',
})
expect(res.headers.get('location')?.endsWith('/default/about')).toEqual(
false
)
})
it(`should redirect to data urls with data requests and internal redirects`, async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/es/old-home.json`,
{ override: 'internal' },
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(
res.headers
.get('x-nextjs-redirect')
?.endsWith(`/es/new-home?override=internal`)
).toEqual(true)
expect(res.headers.get('location')).toEqual(null)
})
it(`should redirect to external urls with data requests and external redirects`, async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/es/old-home.json`,
{ override: 'external' },
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(res.headers.get('x-nextjs-redirect')).toEqual(
'https://example.com/'
)
expect(res.headers.get('location')).toEqual(null)
const browser = await webdriver(next.url, '/')
await browser.elementByCss('#old-home-external').click()
await check(async () => {
expect(await browser.elementByCss('h1').text()).toEqual(
'Example Domain'
)
return 'yes'
}, 'yes')
})
}
function testsWithLocale(locale = '') {
const label = locale ? `${locale} ` : ``
it(`${label}should redirect`, async () => {
const res = await fetchViaHTTP(next.url, `${locale}/old-home`)
const html = await res.text()
const $ = cheerio.load(html)
const browser = await webdriver(next.url, `${locale}/old-home`)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(
`${locale}/new-home`
)
} finally {
await browser.close()
}
expect($('.title').text()).toBe('Welcome to a new page')
})
it(`${label}should implement internal redirects`, async () => {
const browser = await webdriver(next.url, `${locale}`)
await browser.eval('window.__SAME_PAGE = true')
await browser.elementByCss('#old-home').click()
await browser.waitForElementByCss('#new-home-title')
expect(await browser.eval('window.__SAME_PAGE')).toBe(true)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(
`${locale}/new-home`
)
} finally {
await browser.close()
}
})
it(`${label}should redirect cleanly with the original url param`, async () => {
const browser = await webdriver(next.url, `${locale}/blank-page?foo=bar`)
try {
expect(
await browser.eval(
`window.location.href.replace(window.location.origin, '')`
)
).toBe(`${locale}/new-home`)
} finally {
await browser.close()
}
})
it(`${label}should redirect multiple times`, async () => {
const res = await fetchViaHTTP(next.url, `${locale}/redirect-me-alot`)
const browser = await webdriver(next.url, `${locale}/redirect-me-alot`)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(
`${locale}/new-home`
)
} finally {
await browser.close()
}
const html = await res.text()
const $ = cheerio.load(html)
expect($('.title').text()).toBe('Welcome to a new page')
})
it(`${label}should redirect (infinite-loop)`, async () => {
await expect(
fetchViaHTTP(next.url, `${locale}/infinite-loop`)
).rejects.toThrow()
})
it(`${label}should redirect to api route with locale`, async () => {
const browser = await webdriver(next.url, `${locale}`)
await browser.elementByCss('#link-to-api-with-locale').click()
await browser.waitForCondition('window.location.pathname === "/api/ok"')
await check(() => browser.elementByCss('body').text(), 'ok')
})
}
})