-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
257 lines (220 loc) · 9.21 KB
/
Copy pathmiddleware.test.ts
File metadata and controls
257 lines (220 loc) · 9.21 KB
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
import {NextRequest} from 'next/server';
import {afterEach, describe, expect, it, vi} from 'vitest';
// Helper to import the middleware module fresh with given env vars.
// isDeveloperDocs and redirectMap are evaluated at module load time,
// so we need a fresh import per env var scenario.
const ENV_KEYS = ['DEVELOPER_DOCS', 'NEXT_PUBLIC_DEVELOPER_DOCS'] as const;
async function importMiddleware(env: Record<string, string> = {}) {
vi.resetModules();
vi.unstubAllEnvs();
// Save originals so we can restore after import
const saved = new Map<string, string>();
for (const key of ENV_KEYS) {
if (key in process.env) {
saved.set(key, process.env[key]!);
}
delete process.env[key];
}
for (const [key, value] of Object.entries(env)) {
vi.stubEnv(key, value);
}
const mod = await import('./middleware');
vi.unstubAllEnvs();
// Restore any originals that were deleted
for (const [key, value] of saved) {
process.env[key] = value;
}
return mod;
}
function makeRequest(path: string): NextRequest {
return new NextRequest(new URL(path, 'http://localhost:3000'));
}
function isRedirect(res: Response): boolean {
return res.status === 301 || res.status === 302;
}
describe('middleware redirect set selection', () => {
afterEach(() => {
vi.unstubAllEnvs();
});
// "/" only exists in DEVELOPER_DOCS_REDIRECTS, so it's a good signal
// for which redirect set is active without hardcoding destinations.
describe('when DEVELOPER_DOCS is set (build-time inline via next.config.ts)', () => {
it('activates developer docs redirect set', async () => {
const {middleware} = await importMiddleware({DEVELOPER_DOCS: '1'});
expect(isRedirect(middleware(makeRequest('/')))).toBe(true);
});
});
describe('when NEXT_PUBLIC_DEVELOPER_DOCS is set (runtime fallback)', () => {
it('activates developer docs redirect set', async () => {
const {middleware} = await importMiddleware({NEXT_PUBLIC_DEVELOPER_DOCS: '1'});
expect(isRedirect(middleware(makeRequest('/')))).toBe(true);
});
it('does not use user docs redirects', async () => {
const {middleware} = await importMiddleware({NEXT_PUBLIC_DEVELOPER_DOCS: '1'});
// user-docs-only path should not redirect in developer docs mode
expect(isRedirect(middleware(makeRequest('/platforms/python/http_errors/')))).toBe(
false
);
});
});
describe('when NEXT_PUBLIC_DEVELOPER_DOCS is not set (docs.sentry.io)', () => {
it('activates user docs redirect set', async () => {
const {middleware} = await importMiddleware({});
// "/" should NOT redirect in user docs mode
expect(isRedirect(middleware(makeRequest('/')))).toBe(false);
});
it('does not use developer docs redirects', async () => {
const {middleware} = await importMiddleware({});
// developer-docs-only path "/docs-components/" should not redirect
expect(isRedirect(middleware(makeRequest('/docs-components/')))).toBe(false);
});
});
describe('pass-through', () => {
it('does not redirect unknown paths', async () => {
const {middleware} = await importMiddleware({});
expect(isRedirect(middleware(makeRequest('/not/a/real/path/')))).toBe(false);
});
it('does not redirect unknown paths in developer docs mode', async () => {
const {middleware} = await importMiddleware({NEXT_PUBLIC_DEVELOPER_DOCS: '1'});
expect(isRedirect(middleware(makeRequest('/not/a/real/path/')))).toBe(false);
});
});
});
describe('production build-url redirect to canonical (Deployment Protection off)', () => {
afterEach(() => {
vi.unstubAllEnvs();
});
function makeHostRequest(url: string, method = 'GET'): NextRequest {
return new NextRequest(new URL(url), {method});
}
it('redirects a non-canonical production host to the canonical host', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(
makeHostRequest('https://sentry-docs-abc123.vercel.app/platforms/javascript/')
);
expect(res.status).toBe(308);
expect(res.headers.get('location')).toBe(
'https://docs.sentry.io/platforms/javascript/'
);
});
it('preserves query strings when redirecting', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(
makeHostRequest('https://sentry-docs-git-master-getsentry.vercel.app/search/?q=x')
);
expect(res.status).toBe(308);
expect(res.headers.get('location')).toBe('https://docs.sentry.io/search/?q=x');
});
it('redirects to develop.sentry.dev in developer docs mode', async () => {
const {middleware} = await importMiddleware({NEXT_PUBLIC_DEVELOPER_DOCS: '1'});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(
makeHostRequest('https://develop-docs-abc123.vercel.app/getting-started/')
);
expect(res.status).toBe(308);
expect(res.headers.get('location')).toBe(
'https://develop.sentry.dev/getting-started/'
);
});
it('does not redirect requests already on the canonical host', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(makeHostRequest('https://docs.sentry.io/platforms/'));
expect(res.status).not.toBe(308);
});
it('leaves preview deployments accessible (no redirect)', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'preview');
const res = middleware(
makeHostRequest('https://sentry-docs-git-my-branch.sentry.dev/getting-started/')
);
expect(res.status).not.toBe(308);
});
it('does nothing locally (VERCEL_ENV unset)', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', '');
const res = middleware(makeHostRequest('https://sentry-docs-abc123.vercel.app/foo/'));
expect(res.status).not.toBe(308);
});
it('redirects HEAD requests', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(
makeHostRequest('https://sentry-docs-abc123.vercel.app/foo/', 'HEAD')
);
expect(res.status).toBe(308);
expect(res.headers.get('location')).toBe('https://docs.sentry.io/foo/');
});
it('does not redirect non-page requests', async () => {
const {middleware} = await importMiddleware({});
vi.stubEnv('VERCEL_ENV', 'production');
const res = middleware(
makeHostRequest('https://sentry-docs-abc123.vercel.app/foo/', 'POST')
);
expect(res.status).not.toBe(308);
});
});
describe('canonical Link header on .md responses', () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it('adds Link rel=canonical pointing to the HTML page for a deep path', async () => {
const {middleware} = await importMiddleware({});
const res = middleware(makeRequest('/platforms/apple/cocoa.md'));
expect(res.headers.get('Link')).toBe(
'<https://docs.sentry.io/platforms/apple/cocoa/>; rel="canonical"'
);
});
it('maps /index.md to the root canonical URL', async () => {
const {middleware} = await importMiddleware({});
const res = middleware(makeRequest('/index.md'));
expect(res.headers.get('Link')).toBe('<https://docs.sentry.io/>; rel="canonical"');
});
it('handles top-level .md paths', async () => {
const {middleware} = await importMiddleware({});
const res = middleware(makeRequest('/platforms.md'));
expect(res.headers.get('Link')).toBe(
'<https://docs.sentry.io/platforms/>; rel="canonical"'
);
});
it('uses develop.sentry.dev origin in developer docs mode', async () => {
const {middleware} = await importMiddleware({NEXT_PUBLIC_DEVELOPER_DOCS: '1'});
const res = middleware(makeRequest('/platforms/apple/cocoa.md'));
expect(res.headers.get('Link')).toBe(
'<https://develop.sentry.dev/platforms/apple/cocoa/>; rel="canonical"'
);
});
it('does not add Link header for non-.md paths', async () => {
const {middleware} = await importMiddleware({});
const res = middleware(makeRequest('/platforms/apple/cocoa/'));
expect(res.headers.get('Link')).toBeNull();
});
});
describe('wantsMarkdownViaAccept: text/plain no longer triggers markdown', () => {
it('does not serve markdown for Accept: application/json, text/plain, */*', async () => {
const {middleware} = await importMiddleware({});
const req = new NextRequest(
new URL('/platforms/apple/cocoa/', 'http://localhost:3000'),
{
headers: {Accept: 'application/json, text/plain, */*'},
}
);
const res = middleware(req);
// Should not rewrite to .md — Next rewrite changes the URL; a plain next() keeps it
expect(res.headers.get('x-middleware-rewrite')).toBeNull();
});
it('still serves markdown for explicit text/markdown Accept header', async () => {
const {middleware} = await importMiddleware({});
const req = new NextRequest(
new URL('/platforms/apple/cocoa/', 'http://localhost:3000'),
{
headers: {Accept: 'text/markdown'},
}
);
const res = middleware(req);
// A rewrite to .md will set x-middleware-rewrite
expect(res.headers.get('x-middleware-rewrite')).toContain('.md');
});
});