From b5f133739eee8aee816179af0bb32ea1335236c5 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Wed, 19 Jul 2023 20:55:43 +0300 Subject: [PATCH] chore(backend): Add tests for buildRequestUrl and buildOrigin --- packages/backend/src/utils.test.ts | 118 +++++++++++++++++++++++++++++ packages/backend/tests/suites.ts | 2 + 2 files changed, 120 insertions(+) create mode 100644 packages/backend/src/utils.test.ts diff --git a/packages/backend/src/utils.test.ts b/packages/backend/src/utils.test.ts new file mode 100644 index 00000000000..9114d8cb815 --- /dev/null +++ b/packages/backend/src/utils.test.ts @@ -0,0 +1,118 @@ +import type QUnit from 'qunit'; + +import { buildOrigin, buildRequestUrl } from './utils'; + +export default (QUnit: QUnit) => { + const { module, test } = QUnit; + + module('buildOrigin({ protocol, forwardedProto, forwardedHost, host })', () => { + test('without any param', assert => { + assert.equal(buildOrigin({}), ''); + }); + + test('with protocol', assert => { + assert.equal(buildOrigin({ protocol: 'http' }), ''); + }); + + test('with host', assert => { + assert.equal(buildOrigin({ host: 'localhost:3000' }), ''); + }); + + test('with protocol and host', assert => { + assert.equal(buildOrigin({ protocol: 'http', host: 'localhost:3000' }), 'http://localhost:3000'); + }); + + test('with forwarded proto', assert => { + assert.equal( + buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedProto: 'https' }), + 'https://localhost:3000', + ); + }); + + test('with forwarded proto - with multiple values', assert => { + assert.equal( + buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedProto: 'https,http' }), + 'https://localhost:3000', + ); + }); + + test('with forwarded host', assert => { + assert.equal( + buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedHost: 'example.com' }), + 'http://example.com', + ); + }); + + test('with forwarded host - with multiple values', assert => { + assert.equal( + buildOrigin({ protocol: 'http', host: 'localhost:3000', forwardedHost: 'example.com,example-2.com' }), + 'http://example.com', + ); + }); + + test('with forwarded proto and host', assert => { + assert.equal( + buildOrigin({ + protocol: 'http', + host: 'localhost:3000', + forwardedProto: 'https', + forwardedHost: 'example.com', + }), + 'https://example.com', + ); + }); + + test('with forwarded proto and host - without protocol', assert => { + assert.equal( + buildOrigin({ host: 'localhost:3000', forwardedProto: 'https', forwardedHost: 'example.com' }), + 'https://example.com', + ); + }); + + test('with forwarded proto and host - without host', assert => { + assert.equal( + buildOrigin({ protocol: 'http', forwardedProto: 'https', forwardedHost: 'example.com' }), + 'https://example.com', + ); + }); + + test('with forwarded proto and host - without host and protocol', assert => { + assert.equal(buildOrigin({ forwardedProto: 'https', forwardedHost: 'example.com' }), 'https://example.com'); + }); + }); + + module('buildRequestUrl({ request, path })', () => { + test('without headers', assert => { + const req = new Request('http://localhost:3000/path'); + assert.equal(buildRequestUrl(req), 'http://localhost:3000/path'); + }); + + test('with forwarded proto / host headers', assert => { + const req = new Request('http://localhost:3000/path', { + headers: { 'x-forwarded-host': 'example.com', 'x-forwarded-proto': 'https,http' }, + }); + assert.equal(buildRequestUrl(req), 'https://example.com/path'); + }); + + test('with forwarded proto / host and host headers', assert => { + const req = new Request('http://localhost:3000/path', { + headers: { + 'x-forwarded-host': 'example.com', + 'x-forwarded-proto': 'https,http', + host: 'example-host.com', + }, + }); + assert.equal(buildRequestUrl(req), 'https://example.com/path'); + }); + + test('with path', assert => { + const req = new Request('http://localhost:3000/path'); + assert.equal(buildRequestUrl(req, '/other-path'), 'http://localhost:3000/other-path'); + }); + + test('with query params in request', assert => { + const req = new Request('http://localhost:3000/path'); + assert.equal(buildRequestUrl(req), 'http://localhost:3000/path'); + }); + }); +}; diff --git a/packages/backend/tests/suites.ts b/packages/backend/tests/suites.ts index db4cf04f7b3..3273d44f9a3 100644 --- a/packages/backend/tests/suites.ts +++ b/packages/backend/tests/suites.ts @@ -14,6 +14,7 @@ import factoryTest from './dist/api/factory.test.js'; import exportsTest from './dist/exports.test.js'; import redirectTest from './dist/redirections.test.js'; +import utilsTest from './dist/utils.test.js'; // Add them to the suite array const suites = [ @@ -28,6 +29,7 @@ const suites = [ verifyJwtTest, factoryTest, redirectTest, + utilsTest, ]; export default suites;