aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/test/functions/redirects.test.js
blob: 01bddc4c9a2869ab03ce90e156c921dd9ff8a290 (plain) (blame)
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
import * as assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';

describe(
	'SSR - Redirects',
	() => {
		let fixture;

		before(async () => {
			fixture = await loadFixture({ root: new URL('./fixtures/redirects/', import.meta.url) });
			await fixture.build();
		});

		it('Creates a redirects file', async () => {
			const redirects = await fixture.readFile('./_redirects');
			const parts = redirects.split(/\s+/);
			assert.deepEqual(parts, ['', '/other', '/', '301', '']);
			// Snapshots are not supported in Node.js test yet (https://github.com/nodejs/node/issues/48260)
			assert.equal(redirects, '\n/other    /       301\n');
		});

		it('Does not create .html files', async () => {
			let hasErrored = false;
			try {
				await fixture.readFile('/other/index.html');
			} catch {
				hasErrored = true;
			}
			assert.equal(hasErrored, true, 'this file should not exist');
		});

		it('renders static 404 page', async () => {
			const entryURL = new URL(
				'./fixtures/redirects/.netlify/v1/functions/ssr/ssr.mjs',
				import.meta.url,
			);
			const { default: handler } = await import(entryURL);
			const resp = await handler(new Request('http://example.com/nonexistant-page'), {});
			assert.equal(resp.status, 404);
			assert.equal(resp.headers.get('content-type'), 'text/html; charset=utf-8');
			const text = await resp.text();
			assert.equal(text.includes('This is my static 404 page'), true);
		});

		it('does not pass through 404 request', async () => {
			let testServerCalls = 0;
			const testServer = createServer((_req, res) => {
				testServerCalls++;
				res.writeHead(200);
				res.end();
			});
			testServer.listen(5678);
			const entryURL = new URL(
				'./fixtures/redirects/.netlify/v1/functions/ssr/ssr.mjs',
				import.meta.url,
			);
			const { default: handler } = await import(entryURL);
			const resp = await handler(new Request('http://localhost:5678/nonexistant-page'), {});
			assert.equal(resp.status, 404);
			assert.equal(testServerCalls, 0);
			testServer.close();
		});
	},
	{
		timeout: 120000,
	},
);