diff options
author | 2025-02-05 16:05:26 +0000 | |
---|---|---|
committer | 2025-02-05 16:05:26 +0000 | |
commit | 43a37c1956d415c54bb5847e3b29927e16bef1e3 (patch) | |
tree | 67c7b4b086c1d41e5a09c1c64dab3aa01e541310 /packages/integrations/node/test/url.test.js | |
parent | 817fe553899d0a8a0e4ff27c8d062bf1e24ca566 (diff) | |
parent | 0f3e23b50afe3f6f82caaf3e964c451280aa0688 (diff) | |
download | astro-43a37c1956d415c54bb5847e3b29927e16bef1e3.tar.gz astro-43a37c1956d415c54bb5847e3b29927e16bef1e3.tar.zst astro-43a37c1956d415c54bb5847e3b29927e16bef1e3.zip |
Merge branch 'main' of ../../temp/adapters into move-node
Diffstat (limited to 'packages/integrations/node/test/url.test.js')
-rw-r--r-- | packages/integrations/node/test/url.test.js | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/packages/integrations/node/test/url.test.js b/packages/integrations/node/test/url.test.js new file mode 100644 index 000000000..81b357b71 --- /dev/null +++ b/packages/integrations/node/test/url.test.js @@ -0,0 +1,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/'); + }); +}); |