Skip to content

Commit

Permalink
handle basic blocking rules with document modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Feb 22, 2024
1 parent 8b7caa1 commit fb27db5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,21 @@ export class RequestBlockingApi {
}

// Basic rules for blocking requests are applied only to sub-requests
// so `||example.com^` will not block the main page
// https://adguard.com/kb/general/ad-filtering/create-own-filters/#basic-rules
if (requestType === RequestType.Document) {
// but if the blocking rule has $document modifier, blocking page should be shown
// e.g. `||example.com^$document`
if ((rule.getPermittedRequestTypes()) === RequestType.Document) {
return documentBlockingService.getDocumentBlockingResponse({
eventId,
requestUrl,
referrerUrl,
rule,
tabId,
});
}

return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ describe('Request Blocking Api - shouldCollapseElement', () => {
});

describe('Request Blocking Api - getBlockingResponse', () => {
const BLOCKING_PAGE_RESPONSE_MARKER = 'BLOCKING_PAGE';
const mockedBlockingPageResponse = {
cancel: true,
redirectUrl: BLOCKING_PAGE_RESPONSE_MARKER,
};

beforeEach(() => {
jest.spyOn(documentBlockingService, 'getDocumentBlockingResponse')
.mockReturnValue(mockedBlockingPageResponse);
});

afterEach(() => {
jest.resetAllMocks();
});

describe('tab is new', () => {
beforeEach(() => {
jest.spyOn(tabsApi, 'isNewPopupTab').mockReturnValue(true);
Expand Down Expand Up @@ -137,6 +152,17 @@ describe('Request Blocking Api - getBlockingResponse', () => {
expect(response).toEqual({ cancel: true });
});

it('blocking rule, document modifier, document request - blocking page', () => {
const data = getGetBlockingResponseParamsData(
'||example.com^$document',
'http://example.com',
RequestType.Document,
ContentType.Document,
);
const response = RequestBlockingApi.getBlockingResponse(data);
expect(response).toEqual(mockedBlockingPageResponse);
});

it('the popup modifier, image request - close tab', () => {
const data = getGetBlockingResponseParamsData(
'||example.com^$popup',
Expand Down Expand Up @@ -208,16 +234,8 @@ describe('Request Blocking Api - getBlockingResponse', () => {
});

describe('tab is not new', () => {
const BLOCKING_PAGE_RESPONSE_MARKER = 'BLOCKING_PAGE';
const mockedBlockingPageResponse = {
cancel: true,
redirectUrl: BLOCKING_PAGE_RESPONSE_MARKER,
};

beforeEach(() => {
jest.spyOn(tabsApi, 'isNewPopupTab').mockReturnValue(false);
jest.spyOn(documentBlockingService, 'getDocumentBlockingResponse')
.mockReturnValue(mockedBlockingPageResponse);
});

afterEach(() => {
Expand Down Expand Up @@ -257,6 +275,17 @@ describe('Request Blocking Api - getBlockingResponse', () => {
expect(response).toEqual(mockedBlockingPageResponse);
});

it('blocking rule, document modifier, document request - blocking page', () => {
const data = getGetBlockingResponseParamsData(
'||example.com^$document',
'http://example.com',
RequestType.Document,
ContentType.Document,
);
const response = RequestBlockingApi.getBlockingResponse(data);
expect(response).toEqual(mockedBlockingPageResponse);
});

it('the popup modifier, image request - do nothing', () => {
const data = getGetBlockingResponseParamsData(
'||example.com^$popup',
Expand Down

0 comments on commit fb27db5

Please sign in to comment.