-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve prevent-fetch — return original url in response #216 AG-14514
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-14514 to release/v1.7 Squashed commit of the following: commit 53cfe6a Merge: 1a0d9c5 c01dd65 Author: Stanislav A <s.atroschenko@adguard.com> Date: Tue Oct 25 16:06:50 2022 +0300 Merge branch 'release/v1.7' into fix/AG-14514 commit 1a0d9c5 Author: Stanislav A <s.atroschenko@adguard.com> Date: Fri Oct 21 14:19:56 2022 +0300 fix tests commit 2959639 Author: Stanislav A <s.atroschenko@adguard.com> Date: Fri Oct 21 14:14:57 2022 +0300 remove redundant whitespaces in tests assertions commit b39f05c Author: Stanislav A <s.atroschenko@adguard.com> Date: Fri Oct 21 14:13:04 2022 +0300 update build-tests: move helpers to MULTIPLE_TEST_FILES_DIR commit c35d374 Author: Stanislav A <s.atroschenko@adguard.com> Date: Thu Oct 20 13:14:15 2022 +0300 redo helpers tests file structure commit 5a659c2 Author: Stanislav A <s.atroschenko@adguard.com> Date: Thu Oct 20 12:56:33 2022 +0300 fix imports commit 3e3b940 Author: Stanislav A <s.atroschenko@adguard.com> Date: Thu Oct 20 12:46:47 2022 +0300 improve tests commit b9f9c1e Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 20:09:20 2022 +0300 revert index.test typo commit bd76d2c Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 20:07:36 2022 +0300 split helpers tests to separate files and fix typo in noopPromiseResolve commit 1bc6e81 Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 19:40:22 2022 +0300 add arg parser test case commit 6509f76 Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 19:23:36 2022 +0300 fix response type restrictions and add tests commit 44ed1ad Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 17:01:43 2022 +0300 only allow opaque for responseType arg commit 562ad11 Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 14:24:11 2022 +0300 fix typo and helper param description commit a9f9cdf Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 14:05:28 2022 +0300 fix match prop parser and add tests commit d9c0c53 Author: Stanislav A <s.atroschenko@adguard.com> Date: Wed Oct 19 13:04:25 2022 +0300 add response type mock for noopPromiseResolve commit 2f886f0 Author: Stanislav A <s.atroschenko@adguard.com> Date: Fri Oct 14 15:09:04 2022 +0300 fix url parsing commit fac3e17 Author: Stanislav A <s.atroschenko@adguard.com> Date: Thu Oct 13 15:09:42 2022 +0300 remove defineProperty in favor of prop commit b9be4e5 Author: Stanislav A <s.atroschenko@adguard.com> Date: Tue Oct 11 20:49:11 2022 +0300 improve helper comments commit 9fa0ab1 Author: Stanislav A <s.atroschenko@adguard.com> Date: Mon Oct 10 17:34:00 2022 +0300 improve prevent-fetch
- Loading branch information
1 parent
c01dd65
commit d435aac
Showing
11 changed files
with
360 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { parseMatchProps } from '../../src/helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'scriptlets-redirects helpers'; | ||
|
||
module(name); | ||
|
||
const GET_METHOD = 'GET'; | ||
const METHOD_PROP = 'method'; | ||
const URL_PROP = 'url'; | ||
|
||
const URL1 = 'example.com'; | ||
const URL2 = 'http://example.com'; | ||
const URL3 = '/^https?://example.org/'; | ||
const URL4 = '/^https?://example.org/section#user:45/comments/'; | ||
|
||
test('Test parseMatchProps with different url props, simple input', (assert) => { | ||
assert.strictEqual(parseMatchProps(URL1).url, URL1, 'No url match prop, no protocol, not regexp'); | ||
assert.strictEqual(parseMatchProps(`url:${URL1}`).url, URL1, 'url match prop, no protocol, not regexp'); | ||
|
||
assert.strictEqual(parseMatchProps(URL2).url, URL2, 'No url match prop, has protocol, not regexp'); | ||
assert.strictEqual(parseMatchProps(`url:${URL2}`).url, URL2, 'url match prop, has protocol, not regexp'); | ||
|
||
assert.strictEqual(parseMatchProps(URL3).url, URL3, 'No url match prop, has protocol, regexp'); | ||
assert.strictEqual(parseMatchProps(`url:${URL3}`).url, URL3, 'url match prop, has protocol, regexp'); | ||
|
||
assert.strictEqual(parseMatchProps(URL4).url, URL4, 'No url match prop, has protocol, regexp, extra colon in url'); | ||
assert.strictEqual(parseMatchProps(`url:${URL4}`).url, URL4, 'url match prop, has protocol, extra colon in url'); | ||
}); | ||
|
||
test('Test parseMatchProps with different url props, mixed input', (assert) => { | ||
const INPUT1 = `${URL1} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expected1 = { | ||
url: URL1, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT1), expected1, 'No url match prop, no protocol, not regexp'); | ||
|
||
const INPUT1_PREFIXED = `${URL_PROP}:${URL1} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expectedPrefixed1 = { | ||
url: URL1, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT1_PREFIXED), expectedPrefixed1, 'Has url match prop, no protocol, not regexp'); | ||
|
||
const INPUT2 = `${URL2} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expected2 = { | ||
url: URL2, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT2), expected2, 'No url match prop, has protocol, not regexp'); | ||
|
||
const INPUT2_PREFIXED = `${URL_PROP}:${URL2} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expectedPrefixed2 = { | ||
url: URL2, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT2_PREFIXED), expectedPrefixed2, 'Has url match prop, has protocol, not regexp'); | ||
|
||
const INPUT3 = `${URL3} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expected3 = { | ||
url: URL3, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT3), expected3, 'No url match prop, has protocol, regexp'); | ||
|
||
const INPUT3_PREFIXED = `${URL_PROP}:${URL3} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expectedPrefixed3 = { | ||
url: URL3, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT3_PREFIXED), expectedPrefixed3, 'Has url match prop, has protocol, regexp'); | ||
|
||
const INPUT4 = `${URL4} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expected4 = { | ||
url: URL4, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT4), expected4, 'No url match prop, has protocol, regexp, extra colon in url'); | ||
|
||
const INPUT4_PREFIXED = `${URL_PROP}:${URL4} ${METHOD_PROP}:${GET_METHOD}`; | ||
const expectedPrefixed4 = { | ||
url: URL4, | ||
[METHOD_PROP]: GET_METHOD, | ||
}; | ||
assert.deepEqual(parseMatchProps(INPUT4_PREFIXED), expectedPrefixed4, 'Has url match prop, has protocol, regexp, extra colon in url'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,5 @@ | ||
import { | ||
toRegExp, | ||
getNumberFromString, | ||
noopPromiseResolve, | ||
matchStackTrace, | ||
} from '../../src/helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'scriptlets-redirects helpers'; | ||
|
||
module(name); | ||
|
||
test('Test toRegExp for valid inputs', (assert) => { | ||
const DEFAULT_VALUE = '.?'; | ||
const defaultRegexp = new RegExp(DEFAULT_VALUE); | ||
let inputStr; | ||
let expRegex; | ||
|
||
inputStr = '/abc/'; | ||
expRegex = /abc/; | ||
assert.deepEqual(toRegExp(inputStr), expRegex); | ||
|
||
inputStr = '/[a-z]{1,9}/'; | ||
expRegex = /[a-z]{1,9}/; | ||
assert.deepEqual(toRegExp(inputStr), expRegex); | ||
|
||
inputStr = ''; | ||
assert.deepEqual(toRegExp(inputStr), defaultRegexp); | ||
}); | ||
|
||
test('Test toRegExp for invalid inputs', (assert) => { | ||
let inputStr; | ||
|
||
assert.throws(() => { | ||
inputStr = '/\\/'; | ||
toRegExp(inputStr); | ||
}); | ||
|
||
assert.throws(() => { | ||
inputStr = '/[/'; | ||
toRegExp(inputStr); | ||
}); | ||
|
||
assert.throws(() => { | ||
inputStr = '/*/'; | ||
toRegExp(inputStr); | ||
}); | ||
|
||
assert.throws(() => { | ||
inputStr = '/[0-9]++/'; | ||
toRegExp(inputStr); | ||
}); | ||
}); | ||
|
||
test('Test getNumberFromString for all data types inputs', (assert) => { | ||
let inputValue; | ||
|
||
// Boolean | ||
inputValue = true; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// null | ||
inputValue = null; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// undefined | ||
inputValue = undefined; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// undefined | ||
inputValue = undefined; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// number | ||
inputValue = 123; | ||
assert.strictEqual(getNumberFromString(inputValue), 123); | ||
|
||
// valid string | ||
inputValue = '123parsable'; | ||
assert.strictEqual(getNumberFromString(inputValue), 123); | ||
|
||
// invalid string | ||
inputValue = 'not parsable 123'; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// object | ||
inputValue = { test: 'test' }; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
|
||
// array | ||
inputValue = ['test']; | ||
assert.strictEqual(getNumberFromString(inputValue), null); | ||
}); | ||
|
||
test('Test noopPromiseResolve for valid response.body values', async (assert) => { | ||
const objResponse = await noopPromiseResolve('{}'); | ||
const objBody = await objResponse.json(); | ||
|
||
const arrResponse = await noopPromiseResolve('[]'); | ||
const arrBody = await arrResponse.json(); | ||
|
||
assert.ok(typeof objBody === 'object' && !objBody.length); | ||
assert.ok(Array.isArray(arrBody) && !arrBody.length); | ||
}); | ||
|
||
test('Test matchStackTrace for working with getNativeRegexpTest helper', async (assert) => { | ||
const match = matchStackTrace('stack', new Error().stack); | ||
|
||
assert.ok(!match); | ||
}); | ||
import './get-number-from-string.test'; | ||
import './match-stack-trace.test'; | ||
import './noop-promise-resolve.test'; | ||
import './parse-match-props.test'; | ||
import './to-regexp.test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { matchStackTrace } from '../../src/helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'scriptlets-redirects helpers'; | ||
|
||
module(name); | ||
|
||
test('Test matchStackTrace for working with getNativeRegexpTest helper', async (assert) => { | ||
const match = matchStackTrace('stack', new Error().stack); | ||
|
||
assert.ok(!match); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { noopPromiseResolve } from '../../src/helpers'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'scriptlets-redirects helpers'; | ||
|
||
module(name); | ||
|
||
test('Test noopPromiseResolve for valid response props', async (assert) => { | ||
const TEST_URL = 'url'; | ||
const TEST_TYPE = 'opaque'; | ||
const objResponse = await noopPromiseResolve('{}'); | ||
const objBody = await objResponse.json(); | ||
|
||
const arrResponse = await noopPromiseResolve('[]'); | ||
const arrBody = await arrResponse.json(); | ||
|
||
const responseWithUrl = await noopPromiseResolve('{}', TEST_URL); | ||
const responseWithType = await noopPromiseResolve('{}', '', TEST_TYPE); | ||
|
||
assert.ok(responseWithUrl.url === TEST_URL); | ||
assert.ok(typeof objBody === 'object' && !objBody.length); | ||
assert.ok(Array.isArray(arrBody) && !arrBody.length); | ||
assert.strictEqual(responseWithType.type, TEST_TYPE); | ||
}); |
Oops, something went wrong.