Skip to content

Commit

Permalink
fix match prop parser and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 19, 2022
1 parent d9c0c53 commit a9f9cdf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/helpers/fetch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,40 @@ export const getFetchData = (args) => {
export const parseMatchProps = (propsToMatchStr) => {
const PROPS_DIVIDER = ' ';
const PAIRS_MARKER = ':';
const PROTOCOL_REGEX = /(http|https):/;
const LEGAL_MATCH_PROPS = [
'method',
'url',
'headers',
'body',
'mode',
'credentials',
'cache',
'redirect',
'referrer',
'referrerPolicy',
'integrity',
'keepalive',
'signal',
'async',
];

const propsObj = {};
const props = propsToMatchStr.split(PROPS_DIVIDER);

props.forEach((prop) => {
const hasProtocol = PROTOCOL_REGEX.test(prop);

const dividerInd = prop.indexOf(PAIRS_MARKER);
const delimCount = prop.split(':').length - 1;

if ((hasProtocol && delimCount === 1) || dividerInd === -1) {
// parse url when it is specified with protocol and no 'url:'
propsObj.url = prop;
} else {
const key = prop.slice(0, dividerInd);
const key = prop.slice(0, dividerInd);
const hasLegalMatchProp = LEGAL_MATCH_PROPS.indexOf(key) !== -1;

if (hasLegalMatchProp) {
const value = prop.slice(dividerInd + 1);
propsObj[key] = value;
} else {
// Escape multiple colons in prop
// i.e regex value and/or url with protocol specified, with or without 'url:' match prop
// https://github.com/AdguardTeam/Scriptlets/issues/216#issuecomment-1178591463
propsObj.url = prop;
}
});

Expand Down
20 changes: 20 additions & 0 deletions tests/helpers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getNumberFromString,
noopPromiseResolve,
matchStackTrace,
parseMatchProps,
} from '../../src/helpers';

const { test, module } = QUnit;
Expand Down Expand Up @@ -115,3 +116,22 @@ test('Test matchStackTrace for working with getNativeRegexpTest helper', async (

assert.ok(!match);
});

test('Test parseMatchProps for working with different url inputs', (assert) => {
const URL_INPUT_1 = 'example.com';
const URL_INPUT_2 = 'http://example.com';
const URL_INPUT_3 = '/^https?://example.org/';
const URL_INPUT_4 = '/^https?://example.org/section#user:45/comments/';

assert.ok(parseMatchProps(URL_INPUT_1).url, URL_INPUT_1, 'No url match prop, no protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_1}`).url, URL_INPUT_1, 'url match prop, no protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_2).url, URL_INPUT_2, 'No url match prop, has protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_2}`).url, URL_INPUT_2, 'url match prop, has protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_3).url, URL_INPUT_3, 'No url match prop, has protocol, regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_3}`).url, URL_INPUT_3, 'url match prop, has protocol, regexp');

assert.ok(parseMatchProps(URL_INPUT_4).url, URL_INPUT_4, 'No url match prop, has protocol, regexp, extra colon in url');
assert.ok(parseMatchProps(`url: ${URL_INPUT_4}`).url, URL_INPUT_4, 'url match prop, has protocol, extra colon in url');
});

0 comments on commit a9f9cdf

Please sign in to comment.