url-protocol.test.js 2.0 KB

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