Skip to content

Commit

Permalink
chore(backend): Add tests for buildRequestUrl and buildOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkl committed Jul 19, 2023
1 parent 623194d commit b5f1337
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
118 changes: 118 additions & 0 deletions packages/backend/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
};
2 changes: 2 additions & 0 deletions packages/backend/tests/suites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -28,6 +29,7 @@ const suites = [
verifyJwtTest,
factoryTest,
redirectTest,
utilsTest,
];

export default suites;

0 comments on commit b5f1337

Please sign in to comment.