164 lines
5.5 KiB
TypeScript
164 lines
5.5 KiB
TypeScript
|
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
||
|
|
||
|
import onBeforeRequest from '~background/utils/webRequest/onBeforeRequest';
|
||
|
import { DEFAULT_DOMAIN_CONFIG } from '~utils/constants';
|
||
|
import { formatDomainFromURL, validateSupport } from '~utils/domain';
|
||
|
import type { ExtensionData } from '~utils/types';
|
||
|
|
||
|
describe('background/utils/webRequest/onBeforeRequest.ts', () => {
|
||
|
beforeEach(() => {
|
||
|
jest.clearAllMocks();
|
||
|
(validateSupport as jest.Mock).mockReturnValue(true);
|
||
|
});
|
||
|
|
||
|
it('should update session rules if conditions are met', async () => {
|
||
|
const data: ExtensionData = {
|
||
|
actions: [
|
||
|
{
|
||
|
domain: 'jestjs.io',
|
||
|
name: 'click',
|
||
|
selector: '#jest',
|
||
|
},
|
||
|
],
|
||
|
exclusions: {
|
||
|
domains: ['*.jestjs.io'],
|
||
|
overflows: ['*.jestjs.io'],
|
||
|
tags: ['JEST'],
|
||
|
},
|
||
|
keywords: ['jest'],
|
||
|
rules: [
|
||
|
{
|
||
|
action: {
|
||
|
type: 'block' as chrome.declarativeNetRequest.RuleActionType,
|
||
|
},
|
||
|
condition: {
|
||
|
resourceTypes: [
|
||
|
'font' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'image' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'media' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'object' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'script' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'stylesheet' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'xmlhttprequest' as chrome.declarativeNetRequest.ResourceType,
|
||
|
],
|
||
|
urlFilter: '||jestjs.io^',
|
||
|
tabIds: [123],
|
||
|
},
|
||
|
id: 1,
|
||
|
priority: 1,
|
||
|
},
|
||
|
],
|
||
|
tokens: {
|
||
|
backdrops: ['#backdrop'],
|
||
|
classes: ['jest'],
|
||
|
containers: ['#container'],
|
||
|
selectors: ['#element'],
|
||
|
},
|
||
|
version: '0.0.0',
|
||
|
};
|
||
|
const details: chrome.webRequest.WebRequestBodyDetails = {
|
||
|
tabId: 123,
|
||
|
type: 'main_frame',
|
||
|
url: 'https://example.com/path',
|
||
|
} as chrome.webRequest.WebRequestBodyDetails;
|
||
|
|
||
|
await chrome.storage.local.set({ data });
|
||
|
await chrome.storage.local.set({ 'example.com': DEFAULT_DOMAIN_CONFIG });
|
||
|
await onBeforeRequest(details);
|
||
|
|
||
|
expect(chrome.storage.local.get).toHaveBeenNthCalledWith(1, 'data');
|
||
|
expect(chrome.storage.local.get).toHaveBeenNthCalledWith(2, 'example.com');
|
||
|
expect(formatDomainFromURL).toHaveBeenCalledWith(new URL(details.url));
|
||
|
expect(validateSupport).toHaveBeenCalledWith('example.com', ['*.jestjs.io']);
|
||
|
expect(chrome.declarativeNetRequest.updateSessionRules).toHaveBeenCalledWith({
|
||
|
addRules: [data.rules[0]],
|
||
|
removeRuleIds: [data.rules[0].id],
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should not update session rules if config.on is false', async () => {
|
||
|
const details: chrome.webRequest.WebRequestBodyDetails = {
|
||
|
tabId: 123,
|
||
|
type: 'main_frame',
|
||
|
url: 'https://example.com/path',
|
||
|
} as chrome.webRequest.WebRequestBodyDetails;
|
||
|
|
||
|
(validateSupport as jest.Mock).mockReturnValue(false);
|
||
|
|
||
|
await onBeforeRequest(details);
|
||
|
|
||
|
expect(chrome.declarativeNetRequest.updateSessionRules).not.toHaveBeenCalled();
|
||
|
});
|
||
|
|
||
|
it('should not update session rules if validateSupport returns false', async () => {
|
||
|
const data: ExtensionData = {
|
||
|
actions: [
|
||
|
{
|
||
|
domain: 'jestjs.io',
|
||
|
name: 'click',
|
||
|
selector: '#jest',
|
||
|
},
|
||
|
],
|
||
|
exclusions: {
|
||
|
domains: ['*.jestjs.io'],
|
||
|
overflows: ['*.jestjs.io'],
|
||
|
tags: ['JEST'],
|
||
|
},
|
||
|
keywords: ['jest'],
|
||
|
rules: [
|
||
|
{
|
||
|
action: {
|
||
|
type: 'block' as chrome.declarativeNetRequest.RuleActionType,
|
||
|
},
|
||
|
condition: {
|
||
|
resourceTypes: [
|
||
|
'font' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'image' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'media' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'object' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'script' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'stylesheet' as chrome.declarativeNetRequest.ResourceType,
|
||
|
'xmlhttprequest' as chrome.declarativeNetRequest.ResourceType,
|
||
|
],
|
||
|
urlFilter: '||jestjs.io^',
|
||
|
tabIds: [123],
|
||
|
},
|
||
|
id: 1,
|
||
|
priority: 1,
|
||
|
},
|
||
|
],
|
||
|
tokens: {
|
||
|
backdrops: ['#backdrop'],
|
||
|
classes: ['jest'],
|
||
|
containers: ['#container'],
|
||
|
selectors: ['#element'],
|
||
|
},
|
||
|
version: '0.0.0',
|
||
|
};
|
||
|
const details: chrome.webRequest.WebRequestBodyDetails = {
|
||
|
tabId: 123,
|
||
|
type: 'main_frame',
|
||
|
url: 'https://example.com/path',
|
||
|
} as chrome.webRequest.WebRequestBodyDetails;
|
||
|
|
||
|
await chrome.storage.local.set({ 'example.com': { ...DEFAULT_DOMAIN_CONFIG, on: false } });
|
||
|
await onBeforeRequest(details);
|
||
|
|
||
|
expect(chrome.declarativeNetRequest.updateSessionRules).toHaveBeenCalledWith({
|
||
|
addRules: undefined,
|
||
|
removeRuleIds: [data.rules[0].id],
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should not process if tabId is invalid or type is not "main_frame"', async () => {
|
||
|
let details = {} as chrome.webRequest.WebRequestBodyDetails;
|
||
|
|
||
|
details = { ...details, tabId: -1, type: 'main_frame', url: 'https://example.com' };
|
||
|
await onBeforeRequest(details);
|
||
|
details = { ...details, tabId: 123, type: 'sub_frame', url: 'https://example.com' };
|
||
|
await onBeforeRequest(details);
|
||
|
|
||
|
expect(chrome.declarativeNetRequest.updateSessionRules).not.toHaveBeenCalled();
|
||
|
});
|
||
|
});
|