Skip to content

Commit 0650cc6

Browse files
test: add getHostDomain and getCacheControlRules test cases
1 parent 528134e commit 0650cc6

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/extension/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function getRandomId() {
3030
* @param {string} host
3131
* @returns {string}
3232
*/
33-
function getHostDomain(host) {
33+
export function getHostDomain(host) {
3434
if (!host || typeof host !== 'string') return '';
3535
return host.startsWith('http') ? new URL(host).host : host;
3636
}
@@ -41,7 +41,7 @@ function getHostDomain(host) {
4141
* @param {Object[]} projectConfigs Configs with host, previewHost, liveHost, reviewHost
4242
* @returns {Object[]} Rules to add
4343
*/
44-
function getCacheControlRules(projectConfigs) {
44+
export function getCacheControlRules(projectConfigs) {
4545
const seen = new Set();
4646
const rules = [];
4747
const hosts = projectConfigs.flatMap((p) => [

test/auth.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import { setUserAgent } from '@web/test-runner-commands';
1818
import sinon from 'sinon';
1919

2020
import {
21+
CACHE_MAX_AGE_SECONDS,
2122
configureAuthAndCorsHeaders,
23+
getCacheControlRules,
24+
getHostDomain,
2225
setAuthToken,
2326
updateUserAgent,
2427
} from '../src/extension/auth.js';
@@ -54,6 +57,59 @@ describe('Test auth', () => {
5457
await configureAuthAndCorsHeaders();
5558
});
5659

60+
describe('getHostDomain', () => {
61+
it('returns hostname for full URL', () => {
62+
expect(getHostDomain('https://example.com/path')).to.equal('example.com');
63+
expect(getHostDomain('https://sub.example.com:443/')).to.equal('sub.example.com');
64+
});
65+
it('returns input for plain domain', () => {
66+
expect(getHostDomain('example.com')).to.equal('example.com');
67+
expect(getHostDomain('main--repo--owner.aem.live')).to.equal('main--repo--owner.aem.live');
68+
});
69+
it('returns empty string for invalid or empty input', () => {
70+
expect(getHostDomain('')).to.equal('');
71+
expect(getHostDomain(null)).to.equal('');
72+
expect(getHostDomain(undefined)).to.equal('');
73+
expect(getHostDomain(123)).to.equal('');
74+
});
75+
});
76+
77+
describe('getCacheControlRules', () => {
78+
it('returns one rule per unique host', () => {
79+
const configs = [
80+
{ host: 'prod.example.com', previewHost: 'preview.example.com' },
81+
{ liveHost: 'live.example.com' },
82+
];
83+
const rules = getCacheControlRules(configs);
84+
expect(rules).to.have.lengthOf(3);
85+
expect(rules.every((r) => r.action?.responseHeaders?.[0]?.header === 'Cache-Control')).to.be.true;
86+
expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}`)).to.be.true;
87+
const filters = rules.map((r) => r.condition.regexFilter);
88+
expect(filters).to.include('^https://prod\\.example\\.com/.*');
89+
expect(filters).to.include('^https://preview\\.example\\.com/.*');
90+
expect(filters).to.include('^https://live\\.example\\.com/.*');
91+
});
92+
it('deduplicates same host across configs', () => {
93+
const configs = [
94+
{ host: 'same.com', previewHost: 'same.com' },
95+
{ liveHost: 'same.com' },
96+
];
97+
const rules = getCacheControlRules(configs);
98+
expect(rules).to.have.lengthOf(1);
99+
expect(rules[0].condition.regexFilter).to.equal('^https://same\\.com/.*');
100+
});
101+
it('extracts host from full URL in config', () => {
102+
const configs = [{ host: 'https://url-host.com/path' }];
103+
const rules = getCacheControlRules(configs);
104+
expect(rules).to.have.lengthOf(1);
105+
expect(rules[0].condition.regexFilter).to.equal('^https://url-host\\.com/.*');
106+
});
107+
it('returns empty array for empty or no valid hosts', () => {
108+
expect(getCacheControlRules([])).to.deep.equal([]);
109+
expect(getCacheControlRules([{ host: '' }, { previewHost: null }])).to.deep.equal([]);
110+
});
111+
});
112+
57113
it('setAuthToken', async () => {
58114
const updateSessionRules = sandbox.spy(chrome.declarativeNetRequest, 'updateSessionRules');
59115
const getConfig = sandbox.spy(chrome.storage.session, 'get');

0 commit comments

Comments
 (0)