Skip to content

Commit

Permalink
Merge branch 'release/v1.7' into fix/AG-14398
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Nov 3, 2022
2 parents 4a4d308 + 788f73a commit 162089f
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 72 deletions.
4 changes: 4 additions & 0 deletions src/helpers/match-request-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
* @returns {boolean}
*/
export const matchRequestProps = (propsToMatch, requestData) => {
if (propsToMatch === '' || propsToMatch === '*') {
return true;
}

let isMatched;

const parsedData = parseMatchProps(propsToMatch);
Expand Down
45 changes: 17 additions & 28 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import {
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -16,6 +14,9 @@ import {
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -96,6 +97,9 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);

let strResponseBody;
if (responseBody === 'emptyObj') {
strResponseBody = '{}';
Expand All @@ -108,7 +112,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
// Skip disallowed response types
if (!(responseType === 'default' || responseType === 'opaque')) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${responseType}`);
log(`Invalid parameter: ${responseType}`);
return;
}

Expand All @@ -117,29 +121,13 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
const fetchData = getFetchData(args);
if (typeof propsToMatch === 'undefined') {
// log if no propsToMatch given
const logMessage = `log: fetch( ${objectToString(fetchData)} )`;
hit(source, logMessage);
} else if (propsToMatch === '' || propsToMatch === getWildcardSymbol()) {
// prevent all fetch calls
shouldPrevent = true;
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(fetchData, matchKey)
&& matchValue.test(fetchData[matchKey]);
});
}
log(`fetch( ${objectToString(fetchData)} )`);
hit(source);
return Reflect.apply(target, thisArg, args);
}

shouldPrevent = matchRequestProps(propsToMatch);

if (shouldPrevent) {
hit(source);
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
Expand Down Expand Up @@ -167,16 +155,17 @@ preventFetch.injections = [
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];
43 changes: 15 additions & 28 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import {
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -18,6 +16,9 @@ import {
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -94,6 +95,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);

let shouldPrevent = false;
let response = '';
let responseText = '';
Expand All @@ -107,27 +111,10 @@ export function preventXHR(source, propsToMatch, customResponseText) {
responseUrl = xhrData.url;
if (typeof propsToMatch === 'undefined') {
// Log if no propsToMatch given
const logMessage = `log: xhr( ${objectToString(xhrData)} )`;
hit(source, logMessage);
} else if (propsToMatch === '' || propsToMatch === getWildcardSymbol()) {
// Prevent all fetch calls
shouldPrevent = true;
log(`log: xhr( ${objectToString(xhrData)} )`);
hit(source);
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(xhrData, matchKey)
&& matchValue.test(xhrData[matchKey]);
});
}
shouldPrevent = matchRequestProps(propsToMatch);
}

return Reflect.apply(target, thisArg, args);
Expand All @@ -151,8 +138,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
if (randomText) {
responseText = randomText;
} else {
// eslint-disable-next-line no-console
console.log(`Invalid range: ${customResponseText}`);
log(`Invalid range: ${customResponseText}`);
}
}
// Mock response object
Expand Down Expand Up @@ -205,9 +191,7 @@ preventXHR.injections = [
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
matchRequestProps,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
Expand All @@ -219,4 +203,7 @@ preventXHR.injections = [
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];
1 change: 1 addition & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export * from './prevent-element-src-loading';
export * from './no-topics';
export * from './trusted-replace-xhr-response';
export * from './xml-prune';
export * from './trusted-replace-fetch-response';
Loading

0 comments on commit 162089f

Please sign in to comment.