summaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/headers.test.js
blob: f2753517e9754fcccbd1ae062f891c125d9c8b22 (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
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
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import nodejs from '../dist/index.js';
import { createRequestAndResponse, loadFixture } from './test-utils.js';

describe('Node Adapter Headers', () => {
	/** @type {import('./test-utils').Fixture} */
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: './fixtures/headers/',
			output: 'server',
			adapter: nodejs({ mode: 'middleware' }),
		});
		await fixture.build();
	});

	it('Endpoint Simple Headers', async () => {
		await runTest('/endpoints/simple', {
			'content-type': 'text/plain;charset=utf-8',
			'x-hello': 'world',
		});
	});

	it('Endpoint Astro Single Cookie Header', async () => {
		await runTest('/endpoints/astro-cookies-single', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': 'from1=astro1',
		});
	});

	it('Endpoint Astro Multi Cookie Header', async () => {
		await runTest('/endpoints/astro-cookies-multi', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': ['from1=astro1', 'from2=astro2'],
		});
	});

	it('Endpoint Response Single Cookie Header', async () => {
		await runTest('/endpoints/response-cookies-single', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': 'hello1=world1',
		});
	});

	it('Endpoint Response Multi Cookie Header', async () => {
		await runTest('/endpoints/response-cookies-multi', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': ['hello1=world1', 'hello2=world2'],
		});
	});

	it('Endpoint Complex Headers Kitchen Sink', async () => {
		await runTest('/endpoints/kitchen-sink', {
			'content-type': 'text/plain;charset=utf-8',
			'x-single': 'single',
			'x-triple': 'one, two, three',
			'set-cookie': ['hello1=world1', 'hello2=world2'],
		});
	});

	it('Endpoint Astro and Response Single Cookie Header', async () => {
		await runTest('/endpoints/astro-response-cookie-single', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': ['from1=response1', 'from1=astro1'],
		});
	});

	it('Endpoint Astro and Response Multi Cookie Header', async () => {
		await runTest('/endpoints/astro-response-cookie-multi', {
			'content-type': 'text/plain;charset=utf-8',
			'set-cookie': ['from1=response1', 'from2=response2', 'from3=astro1', 'from4=astro2'],
		});
	});

	it('Endpoint Response Empty Headers Object', async () => {
		await runTest('/endpoints/response-empty-headers-object', {
			'content-type': 'text/plain;charset=UTF-8',
		});
	});

	it('Endpoint Response undefined Headers Object', async () => {
		await runTest('/endpoints/response-undefined-headers-object', {
			'content-type': 'text/plain;charset=UTF-8',
		});
	});

	it('Component Astro Single Cookie Header', async () => {
		await runTest('/astro/component-astro-cookies-single', {
			'content-type': 'text/html',
			'set-cookie': 'from1=astro1',
		});
	});

	it('Component Astro Multi Cookie Header', async () => {
		await runTest('/astro/component-astro-cookies-multi', {
			'content-type': 'text/html',
			'set-cookie': ['from1=astro1', 'from2=astro2'],
		});
	});

	it('Component Response Single Cookie Header', async () => {
		await runTest('/astro/component-response-cookies-single', {
			'content-type': 'text/html',
			'set-cookie': 'from1=value1',
		});
	});

	it('Component Response Multi Cookie Header', async () => {
		await runTest('/astro/component-response-cookies-multi', {
			'content-type': 'text/html',
			'set-cookie': ['from1=value1', 'from2=value2'],
		});
	});

	it('Component Astro and Response Single Cookie Header', async () => {
		await runTest('/astro/component-astro-response-cookie-single', {
			'content-type': 'text/html',
			'set-cookie': ['from1=response1', 'from1=astro1'],
		});
	});

	it('Component Astro and Response Multi Cookie Header', async () => {
		await runTest('/astro/component-astro-response-cookie-multi', {
			'content-type': 'text/html',
			'set-cookie': ['from1=response1', 'from2=response2', 'from3=astro1', 'from4=astro2'],
		});
	});
});

async function runTest(url, expectedHeaders) {
	const { handler } = await import('./fixtures/headers/dist/server/entry.mjs');

	const { req, res, done } = createRequestAndResponse({
		method: 'GET',
		url,
	});

	handler(req, res);

	req.send();

	await done;
	const headers = res.getHeaders();

	assert.deepEqual(headers, expectedHeaders);
}