Skip to content

Commit

Permalink
add response type mock for noopPromiseResolve
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 19, 2022
1 parent 2f886f0 commit d9c0c53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/helpers/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,26 @@ export const noopObject = () => ({});
export const noopPromiseReject = () => Promise.reject(); // eslint-disable-line compat/compat

/**
* Returns Promise object that is resolved with a response
* @param {string} [responseBody='{}'] value of response body
* Returns Promise object that is reso value of response body
* @param {string} [url=''] value of response url to set on response object
* @param {string} [response='basic'] value of response type to set on response object
*/
export const noopPromiseResolve = (responseBody = '{}', url = '') => {
export const noopPromiseResolve = (responseBody = '{}', responseUrl = '', responseType = 'default') => {
if (typeof Response === 'undefined') {
return;
}
// eslint-disable-next-line compat/compat
const response = new Response(responseBody, {
status: 200,
statusText: 'OK',
// Mock response' url to avoid adb checks
// https://github.com/AdguardTeam/Scriptlets/issues/216
url,
type: responseType,
});

// Mock response' url & type to avoid adb checks
// https://github.com/AdguardTeam/Scriptlets/issues/216
Object.defineProperties(response, {
url: { value: responseUrl },
type: { value: responseType },
});

// eslint-disable-next-line compat/compat, consistent-return
Expand Down
10 changes: 6 additions & 4 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
*
* **Syntax**
* ```
* example.org#%#//scriptlet('prevent-fetch'[, propsToMatch[, responseBody]])
* example.org#%#//scriptlet('prevent-fetch'[, propsToMatch[, responseBody[, responseType]]])
* ```
*
* - `propsToMatch` - optional, string of space-separated properties to match; possible props:
Expand All @@ -41,8 +41,10 @@ import {
* - responseBody - optional, string for defining response body value, defaults to `emptyObj`. Possible values:
* - `emptyObj` - empty object
* - `emptyArr` - empty array
* - responseType - optional, string for defining response type, defaults to `default`.
*
* > Usage with no arguments will log fetch calls to browser console;
* which is useful for debugging but permitted for production filter lists.
* which is useful for debugging but not permitted for production filter lists.
*
* **Examples**
* 1. Log all fetch calls
Expand Down Expand Up @@ -82,7 +84,7 @@ import {
* ```
*/
/* eslint-enable max-len */
export function preventFetch(source, propsToMatch, responseBody = 'emptyObj') {
export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', responseType = 'default') {
// do nothing if browser does not support fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Expand Down Expand Up @@ -131,7 +133,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj') {

if (shouldPrevent) {
hit(source);
return noopPromiseResolve(strResponseBody, fetchData.url);
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
}

return Reflect.apply(target, thisArg, args);
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,20 @@ test('Test getNumberFromString for all data types inputs', (assert) => {

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);
});

test('Test matchStackTrace for working with getNativeRegexpTest helper', async (assert) => {
Expand Down

0 comments on commit d9c0c53

Please sign in to comment.