0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/node/test/url.test.js
Jakob Hellermann 3412535be4
fix: don't include port twice from x-forwarded-host and x-forwarded-port headers (#10917)
* fix: don't include port twice from x-forwarded-host and x-forwarded-port headers

* add changeset

* add test for port both in forwarded host and forwarded port

* don't include port if undefined

* Update .changeset/forty-wolves-turn.md

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
2024-05-03 15:01:25 -04:00

115 lines
2.9 KiB
JavaScript

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');
let { 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');
let { 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');
let { 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');
let { 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');
let { 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');
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/');
});
});