@@ -18,7 +18,10 @@ import { setUserAgent } from '@web/test-runner-commands';
1818import sinon from 'sinon' ;
1919
2020import {
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