From e50b102566488c61d8de6751e5d3c2e7ae34a163 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Mon, 31 Oct 2022 18:44:08 +0300 Subject: [PATCH] use matchRequestProps helper in prevent-xhr & prevent-fetch --- src/scriptlets/prevent-fetch.js | 30 +++++++++--------------------- src/scriptlets/prevent-xhr.js | 30 +++++++++--------------------- 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/src/scriptlets/prevent-fetch.js b/src/scriptlets/prevent-fetch.js index ae8d9d7a..1ffb77e4 100644 --- a/src/scriptlets/prevent-fetch.js +++ b/src/scriptlets/prevent-fetch.js @@ -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, @@ -16,6 +14,9 @@ import { getRequestData, getObjectEntries, getObjectFromEntries, + parseMatchProps, + validateParsedData, + getMatchPropsData, } from '../helpers/index'; /* eslint-disable max-len */ @@ -123,21 +124,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re // 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]); - }); - } + shouldPrevent = matchRequestProps(propsToMatch); } if (shouldPrevent) { @@ -167,11 +154,9 @@ preventFetch.injections = [ hit, getFetchData, objectToString, - parseMatchProps, - validateParsedData, - getMatchPropsData, noopPromiseResolve, getWildcardSymbol, + matchRequestProps, toRegExp, isValidStrPattern, escapeRegExp, @@ -179,4 +164,7 @@ preventFetch.injections = [ getRequestData, getObjectEntries, getObjectFromEntries, + parseMatchProps, + validateParsedData, + getMatchPropsData, ]; diff --git a/src/scriptlets/prevent-xhr.js b/src/scriptlets/prevent-xhr.js index f6ec99d4..275d63ae 100644 --- a/src/scriptlets/prevent-xhr.js +++ b/src/scriptlets/prevent-xhr.js @@ -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, @@ -18,6 +16,9 @@ import { getNumberFromString, nativeIsFinite, nativeIsNaN, + parseMatchProps, + validateParsedData, + getMatchPropsData, } from '../helpers/index'; /* eslint-disable max-len */ @@ -113,21 +114,7 @@ export function preventXHR(source, propsToMatch, customResponseText) { // 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(xhrData, matchKey) - && matchValue.test(xhrData[matchKey]); - }); - } + shouldPrevent = matchRequestProps(propsToMatch); } return Reflect.apply(target, thisArg, args); @@ -205,9 +192,7 @@ preventXHR.injections = [ hit, objectToString, getWildcardSymbol, - parseMatchProps, - validateParsedData, - getMatchPropsData, + matchRequestProps, getRandomIntInclusive, getRandomStrByLength, generateRandomResponse, @@ -219,4 +204,7 @@ preventXHR.injections = [ getNumberFromString, nativeIsFinite, nativeIsNaN, + parseMatchProps, + validateParsedData, + getMatchPropsData, ];