|
1 | 1 | import assert from 'node:assert/strict';
|
2 |
| -import { before, describe, it, beforeEach } from 'node:test'; |
| 2 | +import { before, beforeEach, describe, it } from 'node:test'; |
3 | 3 | import * as cheerio from 'cheerio';
|
4 | 4 | import testAdapter from './test-adapter.js';
|
5 | 5 | import { loadFixture } from './test-utils.js';
|
6 | 6 |
|
7 | 7 | describe('Custom Fetch for Error Pages', () => {
|
8 |
| - /** @type {import('./test-utils.js').Fixture} */ |
9 |
| - let fixture; |
10 |
| - |
11 |
| - before(async () => { |
12 |
| - fixture = await loadFixture({ |
13 |
| - root: './fixtures/custom-fetch-error-pages/', |
14 |
| - output: 'server', |
15 |
| - adapter: testAdapter(), |
16 |
| - build: { inlineStylesheets: 'never' }, |
17 |
| - }); |
18 |
| - }); |
19 |
| - |
20 |
| - describe('Production', () => { |
21 |
| - /** @type {import('./test-utils.js').App} */ |
22 |
| - let app; |
23 |
| - |
24 |
| - // Mock fetch calls for tracking |
25 |
| - let fetchCalls = []; |
26 |
| - const customFetch = async (url) => { |
27 |
| - fetchCalls.push(url); |
28 |
| - // Return a custom response to verify our fetch was used |
29 |
| - return new Response('<html><body><h1>Custom Fetch Response</h1></body></html>', { |
30 |
| - headers: { |
31 |
| - 'content-type': 'text/html', |
32 |
| - }, |
33 |
| - }); |
34 |
| - }; |
35 |
| - |
36 |
| - before(async () => { |
37 |
| - await fixture.build({}); |
38 |
| - app = await fixture.loadTestAdapterApp(); |
39 |
| - }); |
40 |
| - |
41 |
| - beforeEach(() => { |
42 |
| - // Reset fetch calls before each test |
43 |
| - fetchCalls = []; |
44 |
| - }); |
45 |
| - |
46 |
| - it('uses custom fetch implementation in case the server needs to get pre-rendered error 404 page when provided via preRenderedFetch', async () => { |
47 |
| - const request = new Request('http://example.com/not-found'); |
48 |
| - const response = await app.render(request, { prerenderedErrorPageFetch: customFetch }); |
49 |
| - |
50 |
| - // Verify the response comes from our custom fetch |
51 |
| - assert.equal(response.status, 404); |
52 |
| - |
53 |
| - // Verify our custom fetch was called with the right URL |
54 |
| - assert.equal(fetchCalls.length, 1); |
55 |
| - assert.ok(fetchCalls[0].includes('/404')); |
56 |
| - |
57 |
| - const html = await response.text(); |
58 |
| - const $ = cheerio.load(html); |
59 |
| - assert.equal($('h1').text(), 'Custom Fetch Response'); |
60 |
| - }); |
61 |
| - |
62 |
| - it('uses custom fetch implementation for 500 errors', async () => { |
63 |
| - const request = new Request('http://example.com/causes-error'); |
64 |
| - const response = await app.render(request, { prerenderedErrorPageFetch: customFetch }); |
65 |
| - |
66 |
| - // Verify the response comes from our custom fetch |
67 |
| - assert.equal(response.status, 500); |
68 |
| - |
69 |
| - // Verify our custom fetch was called with the right URL |
70 |
| - assert.equal(fetchCalls.length, 1); |
71 |
| - assert.ok(fetchCalls[0].includes('/500')); |
72 |
| - |
73 |
| - const html = await response.text(); |
74 |
| - const $ = cheerio.load(html); |
75 |
| - assert.equal($('h1').text(), 'Custom Fetch Response'); |
76 |
| - }); |
77 |
| - |
78 |
| - it('falls back to global fetch when preRenderedFetch is not provided', async () => { |
79 |
| - const request = new Request('http://example.com/not-found'); |
80 |
| - const response = await app.render(request); |
81 |
| - |
82 |
| - // Verify our custom fetch was NOT called |
83 |
| - assert.equal(fetchCalls.length, 0); |
84 |
| - |
85 |
| - // Response should be the default 404 page |
86 |
| - assert.equal(response.status, 404); |
87 |
| - const html = await response.text(); |
88 |
| - const $ = cheerio.load(html); |
89 |
| - assert.equal($('h1').text(), 'Example Domain'); // actual fetch requesting example.com and gets that. |
90 |
| - }); |
91 |
| - }); |
92 |
| -}); |
| 8 | + /** @type {import('./test-utils.js').Fixture} */ |
| 9 | + let fixture; |
| 10 | + |
| 11 | + before(async () => { |
| 12 | + fixture = await loadFixture({ |
| 13 | + root: './fixtures/custom-fetch-error-pages/', |
| 14 | + output: 'server', |
| 15 | + adapter: testAdapter(), |
| 16 | + build: { inlineStylesheets: 'never' }, |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('Production', () => { |
| 21 | + /** @type {import('./test-utils.js').App} */ |
| 22 | + let app; |
| 23 | + |
| 24 | + // Mock fetch calls for tracking |
| 25 | + let fetchCalls = []; |
| 26 | + const customFetch = async (url) => { |
| 27 | + fetchCalls.push(url); |
| 28 | + // Return a custom response to verify our fetch was used |
| 29 | + return new Response('<html><body><h1>Custom Fetch Response</h1></body></html>', { |
| 30 | + headers: { |
| 31 | + 'content-type': 'text/html', |
| 32 | + }, |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + before(async () => { |
| 37 | + await fixture.build({}); |
| 38 | + app = await fixture.loadTestAdapterApp(); |
| 39 | + }); |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + // Reset fetch calls before each test |
| 43 | + fetchCalls = []; |
| 44 | + }); |
| 45 | + |
| 46 | + it('uses custom fetch implementation in case the server needs to get pre-rendered error 404 page when provided via preRenderedFetch', async () => { |
| 47 | + const request = new Request('http://example.com/not-found'); |
| 48 | + const response = await app.render(request, { prerenderedErrorPageFetch: customFetch }); |
| 49 | + |
| 50 | + // Verify the response comes from our custom fetch |
| 51 | + assert.equal(response.status, 404); |
| 52 | + |
| 53 | + // Verify our custom fetch was called with the right URL |
| 54 | + assert.equal(fetchCalls.length, 1); |
| 55 | + assert.ok(fetchCalls[0].includes('/404')); |
| 56 | + |
| 57 | + const html = await response.text(); |
| 58 | + const $ = cheerio.load(html); |
| 59 | + assert.equal($('h1').text(), 'Custom Fetch Response'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('uses custom fetch implementation for 500 errors', async () => { |
| 63 | + const request = new Request('http://example.com/causes-error'); |
| 64 | + const response = await app.render(request, { prerenderedErrorPageFetch: customFetch }); |
| 65 | + |
| 66 | + // Verify the response comes from our custom fetch |
| 67 | + assert.equal(response.status, 500); |
| 68 | + |
| 69 | + // Verify our custom fetch was called with the right URL |
| 70 | + assert.equal(fetchCalls.length, 1); |
| 71 | + assert.ok(fetchCalls[0].includes('/500')); |
| 72 | + |
| 73 | + const html = await response.text(); |
| 74 | + const $ = cheerio.load(html); |
| 75 | + assert.equal($('h1').text(), 'Custom Fetch Response'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('falls back to global fetch when preRenderedFetch is not provided', async () => { |
| 79 | + const request = new Request('http://example.com/not-found'); |
| 80 | + const response = await app.render(request); |
| 81 | + |
| 82 | + // Verify our custom fetch was NOT called |
| 83 | + assert.equal(fetchCalls.length, 0); |
| 84 | + |
| 85 | + // Response should be the default 404 page |
| 86 | + assert.equal(response.status, 404); |
| 87 | + const html = await response.text(); |
| 88 | + const $ = cheerio.load(html); |
| 89 | + assert.equal($('h1').text(), 'Example Domain'); // actual fetch requesting example.com and gets that. |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments