refactor(response-modifier): extract XHR-specific code from the class
The ResponseModifier class had code specific for XMLHttpRequest. Since
in a follow-up commit we will add support for proxying fetch as well,
this commit extracts the XMLHttpRequest-specific code to the XHRProxy
class.
Bug: twpowertools:229
Change-Id: I1b3653ee20f09ec762aafbac2583a96790a27f45
diff --git a/src/xhrInterceptor/XHRProxy.js b/src/xhrInterceptor/XHRProxy.js
index 95529d8..14d2aae 100644
--- a/src/xhrInterceptor/XHRProxy.js
+++ b/src/xhrInterceptor/XHRProxy.js
@@ -1,7 +1,7 @@
import {waitFor} from 'poll-until-promise';
import {correctArrayKeys} from '../common/protojs';
-import ResponseModifier from '../xhrInterceptor/responseModifiers/index.js';
+import ResponseModifier from '../xhrInterceptor/ResponseModifier';
import * as utils from '../xhrInterceptor/utils.js';
const kSpecialEvents = ['load', 'loadend'];
@@ -144,26 +144,39 @@
window.XMLHttpRequest.prototype.$proxySpecialEvents = function() {
const proxyInstance = this;
kSpecialEvents.forEach(eventName => {
- this.xhr.addEventListener(eventName, function() {
- let interceptedPromise;
+ this.xhr.addEventListener(eventName, async function() {
if (eventName === 'load') {
- interceptedPromise =
- XHRProxyInstance.responseModifier.intercept(proxyInstance)
- .then(() => {
- proxyInstance.$responseIntercepted = true;
- });
+ const initialResponse = utils.getResponseJSON({
+ responseType: proxyInstance.xhr.responseType,
+ response: proxyInstance.xhr.response,
+ $TWPTRequestURL: proxyInstance.$TWPTRequestURL,
+ $isArrayProto: proxyInstance.$isArrayProto,
+ });
+
+ const result = await XHRProxyInstance.responseModifier.intercept({
+ url: proxyInstance.$TWPTRequestURL,
+ originalResponse: initialResponse,
+ });
+
+ if (result.wasModified) {
+ proxyInstance.$newResponse = utils.convertJSONToResponse(
+ proxyInstance, result.modifiedResponse);
+ proxyInstance.$newResponseText = utils.convertJSONToResponseText(
+ proxyInstance, result.modifiedResponse);
+ }
+
+ proxyInstance.$responseModified = result.wasModified;
+ proxyInstance.$responseIntercepted = true;
} else {
- interceptedPromise = waitFor(() => {
+ await waitFor(() => {
if (proxyInstance.$responseIntercepted) return Promise.resolve();
return Promise.reject();
}, kCheckInterceptionOptions);
}
- interceptedPromise.then(() => {
- for (const e of proxyInstance.specialHandlers[eventName]) {
- e[1](arguments);
- }
- });
+ for (const e of proxyInstance.specialHandlers[eventName]) {
+ e[1](arguments);
+ }
});
});
};