|
| 1 | +import { TLSSocket } from 'tls'; |
| 2 | +import nodejs from '../dist/index.js'; |
| 3 | +import { loadFixture, createRequestAndResponse } from './test-utils.js'; |
| 4 | +import { expect } from 'chai'; |
| 5 | + |
| 6 | +describe('URL protocol', () => { |
| 7 | + /** @type {import('./test-utils').Fixture} */ |
| 8 | + let fixture; |
| 9 | + |
| 10 | + before(async () => { |
| 11 | + fixture = await loadFixture({ |
| 12 | + root: './fixtures/url-protocol/', |
| 13 | + output: 'server', |
| 14 | + adapter: nodejs({ mode: 'standalone' }), |
| 15 | + }); |
| 16 | + await fixture.build(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('return http when non-secure', async () => { |
| 20 | + const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs'); |
| 21 | + let { req, res, text } = createRequestAndResponse({ |
| 22 | + url: '/', |
| 23 | + }); |
| 24 | + |
| 25 | + handler(req, res); |
| 26 | + req.send(); |
| 27 | + |
| 28 | + const html = await text(); |
| 29 | + expect(html).to.include('http:'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('return https when secure', async () => { |
| 33 | + const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs'); |
| 34 | + let { req, res, text } = createRequestAndResponse({ |
| 35 | + socket: new TLSSocket(), |
| 36 | + url: '/', |
| 37 | + }); |
| 38 | + |
| 39 | + handler(req, res); |
| 40 | + req.send(); |
| 41 | + |
| 42 | + const html = await text(); |
| 43 | + expect(html).to.include('https:'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('return http when the X-Forwarded-Proto header is set to http', async () => { |
| 47 | + const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs'); |
| 48 | + let { req, res, text } = createRequestAndResponse({ |
| 49 | + headers: { 'X-Forwarded-Proto': 'http' }, |
| 50 | + url: '/', |
| 51 | + }); |
| 52 | + |
| 53 | + handler(req, res); |
| 54 | + req.send(); |
| 55 | + |
| 56 | + const html = await text(); |
| 57 | + expect(html).to.include('http:'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('return https when the X-Forwarded-Proto header is set to https', async () => { |
| 61 | + const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs'); |
| 62 | + let { req, res, text } = createRequestAndResponse({ |
| 63 | + headers: { 'X-Forwarded-Proto': 'https' }, |
| 64 | + url: '/', |
| 65 | + }); |
| 66 | + |
| 67 | + handler(req, res); |
| 68 | + req.send(); |
| 69 | + |
| 70 | + const html = await text(); |
| 71 | + expect(html).to.include('https:'); |
| 72 | + }); |
| 73 | +}); |
0 commit comments