2024-01-25 11:17:31 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
|
|
|
import { before, describe, it } from 'node:test';
|
2023-07-17 19:17:59 -05:00
|
|
|
import { TLSSocket } from 'node:tls';
|
2024-04-03 08:30:23 -05:00
|
|
|
import * as cheerio from 'cheerio';
|
2023-07-17 19:17:59 -05:00
|
|
|
import nodejs from '../dist/index.js';
|
|
|
|
import { createRequestAndResponse, loadFixture } from './test-utils.js';
|
2023-01-26 12:39:57 -05:00
|
|
|
|
2024-04-03 08:27:23 -05:00
|
|
|
describe('URL', () => {
|
|
|
|
/** @type {import('./test-utils.js').Fixture} */
|
2023-01-26 12:39:57 -05:00
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
2024-04-03 08:27:23 -05:00
|
|
|
root: './fixtures/url/',
|
2023-01-26 12:39:57 -05:00
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return http when non-secure', async () => {
|
2024-04-03 08:27:23 -05:00
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
2023-01-26 12:39:57 -05:00
|
|
|
let { req, res, text } = createRequestAndResponse({
|
|
|
|
url: '/',
|
|
|
|
});
|
|
|
|
|
|
|
|
handler(req, res);
|
|
|
|
req.send();
|
|
|
|
|
|
|
|
const html = await text();
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html.includes('http:'), true);
|
2023-01-26 12:39:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('return https when secure', async () => {
|
2024-04-03 08:27:23 -05:00
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
2023-01-26 12:39:57 -05:00
|
|
|
let { req, res, text } = createRequestAndResponse({
|
|
|
|
socket: new TLSSocket(),
|
|
|
|
url: '/',
|
|
|
|
});
|
|
|
|
|
|
|
|
handler(req, res);
|
|
|
|
req.send();
|
|
|
|
|
|
|
|
const html = await text();
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html.includes('https:'), true);
|
2023-01-26 12:39:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('return http when the X-Forwarded-Proto header is set to http', async () => {
|
2024-04-03 08:27:23 -05:00
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
2023-01-26 12:39:57 -05:00
|
|
|
let { req, res, text } = createRequestAndResponse({
|
|
|
|
headers: { 'X-Forwarded-Proto': 'http' },
|
|
|
|
url: '/',
|
|
|
|
});
|
|
|
|
|
|
|
|
handler(req, res);
|
|
|
|
req.send();
|
|
|
|
|
|
|
|
const html = await text();
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html.includes('http:'), true);
|
2023-01-26 12:39:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('return https when the X-Forwarded-Proto header is set to https', async () => {
|
2024-04-03 08:27:23 -05:00
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
2023-01-26 12:39:57 -05:00
|
|
|
let { req, res, text } = createRequestAndResponse({
|
|
|
|
headers: { 'X-Forwarded-Proto': 'https' },
|
|
|
|
url: '/',
|
|
|
|
});
|
|
|
|
|
|
|
|
handler(req, res);
|
|
|
|
req.send();
|
|
|
|
|
|
|
|
const html = await text();
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html.includes('https:'), true);
|
2023-01-26 12:39:57 -05:00
|
|
|
});
|
2024-04-03 08:27:23 -05:00
|
|
|
|
|
|
|
it('includes forwarded host and port in the url', async () => {
|
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
|
|
|
let { req, res, text } = createRequestAndResponse({
|
|
|
|
headers: {
|
|
|
|
'X-Forwarded-Proto': 'https',
|
2024-04-03 08:30:23 -05:00
|
|
|
'X-Forwarded-Host': 'abc.xyz',
|
|
|
|
'X-Forwarded-Port': '444',
|
2024-04-03 08:27:23 -05:00
|
|
|
},
|
|
|
|
url: '/',
|
|
|
|
});
|
|
|
|
|
|
|
|
handler(req, res);
|
|
|
|
req.send();
|
|
|
|
|
|
|
|
const html = await text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-04-03 08:30:23 -05:00
|
|
|
assert.equal($('body').text(), 'https://abc.xyz:444/');
|
2024-04-03 08:27:23 -05:00
|
|
|
});
|
2024-05-03 14:01:25 -05:00
|
|
|
|
|
|
|
it('accepts port in forwarded host and forwarded port', async () => {
|
|
|
|
const { handler } = await import('./fixtures/url/dist/server/entry.mjs');
|
|
|
|
let { 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/');
|
|
|
|
});
|
2023-01-26 12:39:57 -05:00
|
|
|
});
|