/
index.test.ts
167 lines (144 loc) · 5.37 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-env jest */
/* eslint-disable jest/no-standalone-expect */
import { join } from 'path'
import webdriver from 'next-webdriver'
import { fetchViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
const itif = (condition: boolean) => (condition ? it : it.skip)
const isModeDeploy = process.env.NEXT_TEST_MODE === 'deploy'
describe('Middleware custom matchers', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, '../app')),
})
})
afterAll(() => next.destroy())
const runTests = () => {
it('should match missing header correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-1')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-1', undefined, {
headers: {
hello: 'world',
},
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match missing query correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-2')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-2', {
test: 'value',
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match source path', async () => {
const res = await fetchViaHTTP(next.url, '/source-match')
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
})
it('should match has header', async () => {
const res = await fetchViaHTTP(next.url, '/has-match-1', undefined, {
headers: {
'x-my-header': 'hello world!!',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/has-match-1')
expect(res2.status).toBe(404)
})
it('should match has query', async () => {
const res = await fetchViaHTTP(next.url, '/has-match-2', {
'my-query': 'hellooo',
})
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/has-match-2')
expect(res2.status).toBe(404)
})
it('should match has cookie', async () => {
const res = await fetchViaHTTP(next.url, '/has-match-3', undefined, {
headers: {
cookie: 'loggedIn=true',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/has-match-3', undefined, {
headers: {
cookie: 'loggedIn=false',
},
})
expect(res2.status).toBe(404)
})
// Cannot modify host when testing with real deployment
itif(!isModeDeploy)('should match has host', async () => {
const res1 = await fetchViaHTTP(next.url, '/has-match-4')
expect(res1.status).toBe(404)
const res = await fetchViaHTTP(next.url, '/has-match-4', undefined, {
headers: {
host: 'example.com',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/has-match-4', undefined, {
headers: {
host: 'example.org',
},
})
expect(res2.status).toBe(404)
})
it('should match has header value', async () => {
const res = await fetchViaHTTP(next.url, '/has-match-5', undefined, {
headers: {
hasParam: 'with-params',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/has-match-5', undefined, {
headers: {
hasParam: 'without-params',
},
})
expect(res2.status).toBe(404)
})
// FIXME: Test fails on Vercel deployment for now.
// See https://linear.app/vercel/issue/EC-160/header-value-set-on-middleware-is-not-propagated-on-client-request-of
itif(!isModeDeploy)(
'should match has query on client routing',
async () => {
const browser = await webdriver(next.url, '/routes')
await browser.eval('window.__TEST_NO_RELOAD = true')
await browser.elementById('has-match-2').click()
const fromMiddleware = await browser
.elementById('from-middleware')
.text()
expect(fromMiddleware).toBe('true')
const noReload = await browser.eval('window.__TEST_NO_RELOAD')
expect(noReload).toBe(true)
}
)
itif(!isModeDeploy)(
'should match has cookie on client routing',
async () => {
const browser = await webdriver(next.url, '/routes')
await browser.addCookie({ name: 'loggedIn', value: 'true' })
await browser.refresh()
await browser.eval('window.__TEST_NO_RELOAD = true')
await browser.elementById('has-match-3').click()
const fromMiddleware = await browser
.elementById('from-middleware')
.text()
expect(fromMiddleware).toBe('true')
const noReload = await browser.eval('window.__TEST_NO_RELOAD')
expect(noReload).toBe(true)
}
)
}
runTests()
})