forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite.test.js
621 lines (495 loc) · 16.9 KB
/
rewrite.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
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { load as cheerioLoad } from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Dev reroute', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/reroute/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should render the index page when navigating /reroute ', async () => {
const html = await fixture.fetch('/reroute').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating /blog/hello ', async () => {
const html = await fixture.fetch('/blog/hello').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating /blog/salut ', async () => {
const html = await fixture.fetch('/blog/salut').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating dynamic route /dynamic/[id] ', async () => {
const html = await fixture.fetch('/dynamic/hello').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating spread route /spread/[...spread] ', async () => {
const html = await fixture.fetch('/spread/hello').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should return a 404', async () => {
const response = await fixture.fetch('/blog/oops');
assert.equal(response.status, 404);
});
});
describe('Dev rewrite, trailing slash -> never', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-trailing-slash-never/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should rewrite to the homepage', async () => {
const html = await fixture.fetch('/foo').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
});
describe('Dev rewrite, trailing slash -> never, with base', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-trailing-slash-never/',
base: 'base',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should rewrite to the homepage', async () => {
const html = await fixture.fetch('/base/foo').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
});
describe('Dev rewrite, dynamic routing', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-dynamic-routing/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should decode the escaped characters in the URL', async () => {
const html = await fixture.fetch('/foo').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should decode the escaped characters in the params', async () => {
const html = await fixture.fetch('/bar').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
});
describe('Dev rewrite, hybrid/server', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-server/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should rewrite the [slug]/title ', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
assert.match($('h1').text(), /Title/);
assert.match($('p').text(), /some-slug/);
});
it('should display an error if a rewrite is attempted after the body has been consumed', async () => {
const formData = new FormData();
formData.append('email', '[email protected]');
const request = new Request('http://example.com/post/post-body-used', {
method: 'POST',
body: formData,
});
const response = await fixture.fetch('/post/post-body-used', request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('title').text(), 'RewriteWithBodyUsed');
});
it('should error when rewriting from a SSR route to a SSG route', async () => {
const html = await fixture.fetch('/forbidden/dynamic').then((res) => res.text());
const $ = cheerioLoad(html);
assert.match($('title').text(), /ForbiddenRewrite/);
});
});
describe('Build reroute', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/reroute/',
});
await fixture.build();
});
it('should create the index page when navigating /reroute ', async () => {
const html = await fixture.readFile('/reroute/index.html');
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should create the index page when navigating /blog/hello ', async () => {
const html = await fixture.readFile('/blog/hello/index.html');
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should create the index page when navigating /blog/salut ', async () => {
const html = await fixture.readFile('/blog/salut/index.html');
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should create the index page when navigating dynamic route /dynamic/[id] ', async () => {
const html = await fixture.readFile('/dynamic/hello/index.html');
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should create the index page when navigating spread route /spread/[...spread] ', async () => {
const html = await fixture.readFile('/spread/hello/index.html');
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should create the 404 built-in page', async () => {
try {
await fixture.readFile('/spread/oops/index.html');
assert.fail('Not found');
} catch {
assert.ok;
}
});
});
describe('SSR route', () => {
it("should not build if a user tries to use rewrite('/404') in static pages", async () => {
try {
const fixture = await loadFixture({
root: './fixtures/rewrite-404-invalid/',
});
await fixture.build();
assert.fail('It should fail.');
} catch {
// it passes
assert.equal(true, true);
}
});
});
describe('SSR reroute', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/reroute/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('should render the index page when navigating /reroute ', async () => {
const request = new Request('http://example.com/reroute');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating /blog/hello ', async () => {
const request = new Request('http://example.com/blog/hello');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating /blog/salut ', async () => {
const request = new Request('http://example.com/blog/salut');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating dynamic route /dynamic/[id] ', async () => {
const request = new Request('http://example.com/dynamic/hello');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the index page when navigating spread route /spread/[...spread] ', async () => {
const request = new Request('http://example.com/spread/hello');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
});
it('should render the 404 built-in page', async () => {
const request = new Request('http://example.com/blog/oops');
const response = await app.render(request);
assert.equal(response.status, 404);
});
it('should pass the POST data from one page to another', async () => {
const request = new Request('http://example.com/post/post-a', {
method: 'POST',
body: JSON.stringify({
email: '[email protected]',
}),
headers: {
'content-type': 'application/json',
},
});
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Post B');
assert.match($('h2').text(), /[email protected]/);
});
});
describe('SSR rewrite, hybrid/server', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-server/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('should rewrite the [slug]/title ', async () => {
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.match($('h1').text(), /Title/);
assert.match($('p').text(), /some-slug/);
});
it('should return a 500 if a rewrite is attempted after the body has been read', async () => {
const formData = new FormData();
formData.append('email', '[email protected]');
const request = new Request('http://example.com/post/post-body-used', {
method: 'POST',
body: formData,
});
const response = await app.render(request);
assert.equal(response.status, 500);
});
});
describe('Middleware', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/reroute/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should render a locals populated in the third middleware function, because we use next("/")', async () => {
const html = await fixture.fetch('/auth/base').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
assert.equal($('p').text(), 'Called auth');
});
it('should NOT render locals populated in the third middleware function, because we use ctx.reroute("/")', async () => {
const html = await fixture.fetch('/auth/dashboard').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Index');
assert.equal($('p').text(), '');
});
it('should render the index when rewriting with params', async () => {
const html = await fixture.fetch('/auth/params').then((res) => res.text());
const $ = cheerioLoad(html);
assert.match($('h1').text(), /Index/);
});
it('should render correctly compute the new params next("/auth/1234")', async () => {
const html = await fixture.fetch('/auth/astro-params').then((res) => res.text());
const $ = cheerioLoad(html);
assert.match($('h1').text(), /Index with params/);
assert.match($('#params').text(), /Param: 1234/);
assert.match($('#locals').text(), /Locals: Params changed/);
});
});
describe('Middleware with custom 404.astro and 500.astro', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-custom-404/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('The `next()` function should return a Response with status code 404', async () => {
const html = await fixture.fetch('/about').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Custom error');
assert.equal($('p').text(), 'Interjected');
});
it('The `next()` function should return a Response with status code 500', async () => {
const html = await fixture.fetch('/about-2').then((res) => res.text());
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Custom error');
assert.equal($('p').text(), 'Interjected');
});
});
describe('Runtime error, default 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-runtime-error/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should return a 500 status code, but not render the custom 500', async () => {
const response = await fixture.fetch('/errors/from');
assert.equal(response.status, 500);
const text = await response.text();
assert.match(text, /@vite\/client/);
});
});
describe('Runtime error in SSR, default 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-runtime-error/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('should return a 500 status code, but not render the custom 500', async () => {
const request = new Request('http://example.com/errors/from');
const response = await app.render(request);
const text = await response.text();
assert.equal(text, '');
});
});
describe('Runtime error in dev, custom 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-runtime-error-custom500/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should render the custom 500 when rewriting a page that throws an error', async () => {
const response = await fixture.fetch('/errors/start');
assert.equal(response.status, 500);
const html = await response.text();
assert.match(html, /I am the custom 500/);
});
});
describe('Runtime error in SSR, custom 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-runtime-error-custom500/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('should render the custom 500 when rewriting a page that throws an error', async () => {
const request = new Request('http://example.com/errors/start');
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'I am the custom 500');
});
});
describe('Runtime error in dev, custom 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-i18n-manual-routing/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should return a status 200 when rewriting from the middleware to the homepage', async () => {
const response = await fixture.fetch('/reroute');
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Expected http status of index page is 200');
});
});
describe('Runtime error in SSR, custom 500', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/rewrite-i18n-manual-routing/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('should return a status 200 when rewriting from the middleware to the homepage', async () => {
const request = new Request('http://example.com/foo');
const response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerioLoad(html);
assert.equal($('h1').text(), 'Expected http status of index page is 200');
});
});