|
| 1 | +/* |
| 2 | + * Copyright 2026 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +/* eslint-disable no-unused-expressions */ |
| 13 | + |
| 14 | +import { expect } from '@open-wc/testing'; |
| 15 | +import sinon from 'sinon'; |
| 16 | + |
| 17 | +import { addCacheBusterRule, getHostDomain } from '../src/extension/cache-buster.js'; |
| 18 | +import chromeMock from './mocks/chrome.js'; |
| 19 | +import { log } from '../src/extension/log.js'; |
| 20 | + |
| 21 | +// @ts-ignore |
| 22 | +window.chrome = chromeMock; |
| 23 | + |
| 24 | +describe('cache-buster', () => { |
| 25 | + const sandbox = sinon.createSandbox(); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + sandbox.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('getHostDomain', () => { |
| 32 | + it('returns empty string for null or undefined', () => { |
| 33 | + expect(getHostDomain(null)).to.equal(''); |
| 34 | + expect(getHostDomain(undefined)).to.equal(''); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns empty string for empty or whitespace-only string', () => { |
| 38 | + expect(getHostDomain('')).to.equal(''); |
| 39 | + expect(getHostDomain(' ')).to.equal(''); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns hostname for full URL', () => { |
| 43 | + expect(getHostDomain('https://example.com/path')).to.equal('example.com'); |
| 44 | + expect(getHostDomain('https://sub.example.com:443/')).to.equal('sub.example.com'); |
| 45 | + expect(getHostDomain('http://foo.com')).to.equal('foo.com'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns input for plain domain', () => { |
| 49 | + expect(getHostDomain('example.com')).to.equal('example.com'); |
| 50 | + expect(getHostDomain('main--repo--owner.aem.live')).to.equal('main--repo--owner.aem.live'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('trims whitespace', () => { |
| 54 | + expect(getHostDomain(' example.com ')).to.equal('example.com'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('addCacheBusterRule', () => { |
| 59 | + it('returns false and warns when domain is empty after getHostDomain', async () => { |
| 60 | + const logWarn = sandbox.stub(log, 'warn'); |
| 61 | + expect(await addCacheBusterRule('')).to.equal(false); |
| 62 | + expect(await addCacheBusterRule(' ')).to.equal(false); |
| 63 | + expect(logWarn.calledWith('addCacheBusterRule: no domain')).to.be.true; |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns false and warns when domain is null or undefined', async () => { |
| 67 | + const logWarn = sandbox.stub(log, 'warn'); |
| 68 | + expect(await addCacheBusterRule(null)).to.equal(false); |
| 69 | + expect(await addCacheBusterRule(undefined)).to.equal(false); |
| 70 | + expect(logWarn.calledWith('addCacheBusterRule: no domain')).to.be.true; |
| 71 | + }); |
| 72 | + |
| 73 | + it('adds rule and schedules removal after 10s', async () => { |
| 74 | + const clock = sandbox.useFakeTimers(); |
| 75 | + const updateSessionRules = sandbox.stub(chrome.declarativeNetRequest, 'updateSessionRules').resolves(); |
| 76 | + |
| 77 | + const result = await addCacheBusterRule('example.com'); |
| 78 | + |
| 79 | + expect(result).to.equal(true); |
| 80 | + expect(updateSessionRules.calledTwice).to.be.false; |
| 81 | + expect(updateSessionRules.calledOnce).to.be.true; |
| 82 | + const [firstCall] = updateSessionRules.args; |
| 83 | + expect(firstCall[0]).to.have.property('addRules'); |
| 84 | + expect(firstCall[0].addRules).to.have.lengthOf(1); |
| 85 | + const rule = firstCall[0].addRules[0]; |
| 86 | + expect(rule).to.have.property('id'); |
| 87 | + expect(rule.priority).to.equal(1); |
| 88 | + expect(rule.action.type).to.equal('modifyHeaders'); |
| 89 | + expect(rule.action.requestHeaders).to.deep.include( |
| 90 | + { operation: 'set', header: 'Cache-Control', value: 'no-cache' }, |
| 91 | + ); |
| 92 | + expect(rule.action.requestHeaders).to.deep.include( |
| 93 | + { operation: 'set', header: 'Pragma', value: 'no-cache' }, |
| 94 | + ); |
| 95 | + expect(rule.condition.regexFilter).to.equal('^https://example\\.com/.*'); |
| 96 | + expect(rule.condition.requestMethods).to.deep.equal(['get']); |
| 97 | + |
| 98 | + clock.tick(10 * 1000); |
| 99 | + expect(updateSessionRules.calledTwice).to.be.true; |
| 100 | + const removeCall = updateSessionRules.getCall(1).args[0]; |
| 101 | + expect(removeCall).to.have.property('removeRuleIds'); |
| 102 | + expect(removeCall.removeRuleIds).to.deep.equal([rule.id]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('accepts full URL and uses hostname', async () => { |
| 106 | + const updateSessionRules = sandbox.stub(chrome.declarativeNetRequest, 'updateSessionRules').resolves(); |
| 107 | + |
| 108 | + const result = await addCacheBusterRule('https://my.site.com/path'); |
| 109 | + |
| 110 | + expect(result).to.equal(true); |
| 111 | + const rule = updateSessionRules.firstCall.args[0].addRules[0]; |
| 112 | + expect(rule.condition.regexFilter).to.equal('^https://my\\.site\\.com/.*'); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments