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

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

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

	it('return http when non-secure', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		assert.equal(html.includes('http:'), true);
	});

	it('return https when secure', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			socket: new TLSSocket(),
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		assert.equal(html.includes('https:'), true);
	});

	it('return http when the X-Forwarded-Proto header is set to http', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			headers: { 'X-Forwarded-Proto': 'http' },
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		assert.equal(html.includes('http:'), true);
	});

	it('return https when the X-Forwarded-Proto header is set to https', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			headers: { 'X-Forwarded-Proto': 'https' },
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		assert.equal(html.includes('https:'), true);
	});

	it('includes forwarded host and port in the url', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			headers: {
				'X-Forwarded-Proto': 'https',
				'X-Forwarded-Host': 'abc.xyz',
				'X-Forwarded-Port': '444',
			},
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		const $ = cheerio.load(html);

		assert.equal($('body').text(), 'https://abc.xyz:444/');
	});

	it('accepts port in forwarded host and forwarded port', async () => {
		const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
		const { req, res, text } = createRequestAndResponse({
			headers: {
				'X-Forwarded-Proto': 'https',
				'X-Forwarded-Host': 'abc.xyz:444',
				'X-Forwarded-Port': '444',
			},
			url: '/',
		});

		handler(req, res);
		req.send();

		const html = await text();
		const $ = cheerio.load(html);

		assert.equal($('body').text(), 'https://abc.xyz:444/');
	});
});