Skip to content

Commit 83f0dca

Browse files
Add test cases for getTabHostname
1 parent 43ff857 commit 83f0dca

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

injected/src/utils.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,9 @@ export function getTabUrl() {
134134
// @ts-expect-error - globalThis.top is possibly 'null' here
135135
framingURLString = globalThis.top.location.href;
136136
} catch {
137-
framingURLString = globalThis.document.referrer;
138-
}
139-
140-
if (!framingURLString) {
141-
// This is suboptimal, but we need to get the top level origin in an about:blank frame
142-
const topLevelOriginFromFrameAncestors = getTopLevelOriginFromFrameAncestors();
143-
if (topLevelOriginFromFrameAncestors) {
144-
framingURLString = topLevelOriginFromFrameAncestors;
145-
}
137+
// If there's no URL then let's fall back to using the frame ancestors origin which won't have path
138+
// Fall back to the referrer if we can't get the top level origin
139+
framingURLString = getTopLevelOriginFromFrameAncestors() ?? globalThis.document.referrer;
146140
}
147141

148142
let framingURL;

injected/unit-test/helpers/polyfill-process-globals.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
1-
export function polyfillProcessGlobals() {
1+
/**
2+
* Creates a mock location object for testing purposes.
3+
* @returns {Location} A mock location object.
4+
*/
5+
export function createLocationObject(href, frameAncestorsList = []) {
6+
return {
7+
href,
8+
// @ts-expect-error - ancestorOrigins is not defined in the type definition
9+
ancestorOrigins: createDomStringList(frameAncestorsList)
10+
};
11+
}
12+
13+
export function createDomStringList(list) {
14+
const domStringList = {
15+
length: list.length,
16+
item(index) {
17+
if (index < 0 || index >= list.length) {
18+
return null;
19+
}
20+
return list[index];
21+
},
22+
contains(item) {
23+
return list.includes(item);
24+
},
25+
};
26+
27+
// Add index access support
28+
for (let i = 0; i < list.length; i++) {
29+
Object.defineProperty(domStringList, i, {
30+
get() {
31+
return list[i];
32+
},
33+
enumerable: true,
34+
});
35+
}
36+
37+
return domStringList;
38+
}
39+
40+
export function polyfillProcessGlobals(defaultLocation = 'http://localhost:8080', frameAncestorsList = [], topisNull = false) {
241
// Store original values to restore later
342
const originalDocument = globalThis.document;
443
const originalLocation = globalThis.location;
544
const originalTop = globalThis.top;
645

746
// Apply the patch
47+
// @ts-expect-error - document is not defined in the type definition
848
globalThis.document = {
9-
referrer: 'http://localhost:8080',
10-
location: {
11-
href: 'http://localhost:8080',
12-
// @ts-expect-error - ancestorOrigins is not defined in the type definition
13-
ancestorOrigins: {
14-
length: 0,
15-
},
16-
},
49+
referrer: defaultLocation,
50+
location: createLocationObject(defaultLocation, frameAncestorsList)
1751
};
1852

19-
globalThis.location = globalThis.document.location;
53+
globalThis.location = createLocationObject(defaultLocation, frameAncestorsList);
2054

2155
globalThis.top = Object.assign({}, originalTop, {
22-
location: globalThis.location,
56+
location: createLocationObject(defaultLocation, frameAncestorsList)
2357
});
58+
if (topisNull) {
59+
globalThis.top = null;
60+
}
2461

2562
// Return a cleanup function
2663
return function cleanup() {

injected/unit-test/utils.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { matchHostname, postDebugMessage, initStringExemptionLists, processConfig, satisfiesMinVersion } from '../src/utils.js';
1+
import { matchHostname, postDebugMessage, initStringExemptionLists, processConfig, satisfiesMinVersion, getTabHostname } from '../src/utils.js';
22
import { polyfillProcessGlobals } from './helpers/polyfill-process-globals.js';
33

44
polyfillProcessGlobals();
@@ -243,4 +243,25 @@ describe('Helpers checks', () => {
243243
expect(counters.get('testf')).toEqual(5000);
244244
});
245245
});
246+
247+
describe('utils.getTabHostname', () => {
248+
it('returns the hostname of the URL', () => {
249+
const hostname = getTabHostname();
250+
expect(hostname).toEqual('localhost');
251+
252+
const reset = polyfillProcessGlobals('http://example.com');
253+
const hostname2 = getTabHostname();
254+
expect(hostname2).toEqual('example.com');
255+
reset();
256+
257+
const hostname3 = getTabHostname();
258+
expect(hostname3).toEqual('localhost');
259+
260+
// Validates when we're in a frame thats sandboxed so top is null
261+
const reset2 = polyfillProcessGlobals('https://bloop.com', ['http://example.com'], true);
262+
const hostname5 = getTabHostname();
263+
expect(hostname5).toEqual('example.com');
264+
reset2();
265+
});
266+
});
246267
});

0 commit comments

Comments
 (0)