Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrx committed Nov 13, 2024
1 parent 48c5f0e commit e747993
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/tsurlfilter/test/modifiers/advanced-modifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('NetworkRule - removeparam rules', () => {
});

it('works if query parameters are correctly filtered with regexp', () => {
const rule = createNetworkRule('$removeparam=/p1|p2|p3|p4_.*/i', 0);
const rule = createNetworkRule('$removeparam=/p1|p2|p3|p4_|\\$p5.*/i', 0);
const modifier = rule.getAdvancedModifier() as RemoveParamModifier;

const comPage = 'http://example.com/page';
Expand All @@ -366,6 +366,7 @@ describe('NetworkRule - removeparam rules', () => {
expect(modifier.removeParameters(`${comPage}?p0=0&p4_=4`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p4=4`)).toBe(`${comPage}?p0=0&p4=4`);
expect(modifier.removeParameters(`${comPage}?p0=0&p4_1=4`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&%24p5=0&$p5=0&p5=0`)).toBe(`${comPage}?p0=0&p5=0`);
});

it('works if query parameters are removed by naked rule', () => {
Expand All @@ -390,14 +391,15 @@ describe('NetworkRule - removeparam rules', () => {
});

it('works if inverted regex removeparam is applied correctly', () => {
const rule = createNetworkRule('$removeparam=~/p0.*/', 0);
const rule = createNetworkRule('$removeparam=~/p0|\\$p4.*/', 0);
const modifier = rule.getAdvancedModifier() as RemoveParamModifier;

const comPage = 'http://example.com/page';
expect(modifier.removeParameters(`${comPage}`)).toBe(`${comPage}`);
expect(modifier.removeParameters(`${comPage}?p0=0`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p1=1`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p01=0&p1=1&p2=2&p3=3`)).toBe(`${comPage}?p01=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p1=2&p2=2&p3=3&%24p4=0&$p4=0&p4=0`)).toBe(`${comPage}?p0=0&%24p4=0&$p4=0`);
});

it('does not remove unmatched parameters', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/tsurlfilter/test/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ describe('Query parameters', () => {
expect(utils.cleanUrlParamByRegExp('http://example.com?test=1&test=2', /test.*/, true)).toEqual('http://example.com?test=1&test=2');
expect(utils.cleanUrlParamByRegExp('http://example.com?test=1&test=2', /test=1/, true)).toEqual('http://example.com?test=1');
});

it('handles properly encoded params', () => {
expect(utils.cleanUrlParamByRegExp('http://example.com?$test', /\$test.*/)).toEqual('http://example.com');
expect(utils.cleanUrlParamByRegExp('http://example.com?%24test', /\$test.*/)).toEqual('http://example.com');
expect(utils.cleanUrlParamByRegExp('http://example.com?$test', /\$test.*/, true)).toEqual('http://example.com?$test');
expect(utils.cleanUrlParamByRegExp('http://example.com?%24test', /\$test.*/, true)).toEqual('http://example.com?%24test');
});
});

0 comments on commit e747993

Please sign in to comment.