diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b999032b..d6d38c347d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix an issue with unhandled JavaScript crashes being reported as native Android crashes ([#980](https://github.com/Instabug/Instabug-React-Native/pull/980)). - Fix an issue with the Android sourcemaps upload script, causing the build to fail on older versions of Gradle ([#970](https://github.com/Instabug/Instabug-React-Native/pull/970)), closes [#969](https://github.com/Instabug/Instabug-React-Native/issues/969). - Fix an issue with the Android sourcemaps upload script, causing the build to fail when using product flavors ([#975](https://github.com/Instabug/Instabug-React-Native/pull/975)), closes [#974](https://github.com/Instabug/Instabug-React-Native/issues/974). +- Fix an issue with the network interceptor reverting the user's changes to `XMLHttpRequest` after disabling network logging ([#984](https://github.com/Instabug/Instabug-React-Native/pull/984)), closes [#981](https://github.com/Instabug/Instabug-React-Native/issues/981). ## [11.10.0](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.1...11.10.0) (April 20, 2023) diff --git a/src/utils/XhrNetworkInterceptor.ts b/src/utils/XhrNetworkInterceptor.ts index 86e19e9b16..ad79f2d4fa 100644 --- a/src/utils/XhrNetworkInterceptor.ts +++ b/src/utils/XhrNetworkInterceptor.ts @@ -24,9 +24,9 @@ export interface NetworkData { } const XMLHttpRequest = global.XMLHttpRequest; -const originalXHROpen = XMLHttpRequest.prototype.open; -const originalXHRSend = XMLHttpRequest.prototype.send; -const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; +let originalXHROpen = XMLHttpRequest.prototype.open; +let originalXHRSend = XMLHttpRequest.prototype.send; +let originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; let onProgressCallback: ProgressCallback | null; let onDoneCallback: NetworkDataCallback | null; @@ -63,6 +63,15 @@ export default { onProgressCallback = callback; }, enableInterception() { + // Prevents infinite calls to XMLHttpRequest.open when enabling interception multiple times + if (isInterceptorEnabled) { + return; + } + + originalXHROpen = XMLHttpRequest.prototype.open; + originalXHRSend = XMLHttpRequest.prototype.send; + originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; + XMLHttpRequest.prototype.open = function (method, url, ...args) { _reset(); network.url = url; diff --git a/test/utils/XhrNetworkInterceptor.spec.ts b/test/utils/XhrNetworkInterceptor.spec.ts index 5258d533bb..b014bc9d9c 100644 --- a/test/utils/XhrNetworkInterceptor.spec.ts +++ b/test/utils/XhrNetworkInterceptor.spec.ts @@ -29,6 +29,29 @@ describe('Network Interceptor', () => { FakeRequest.send(); }); + it('should keep patched XMLHttpRequest methods', () => { + Interceptor.disableInterception(); + + // Patch XMLHttpRequest.open + const originalXHROpen = XMLHttpRequest.prototype.open; + const patchedCode = jest.fn(); + XMLHttpRequest.prototype.open = function (...args: Parameters) { + patchedCode(); + originalXHROpen.apply(this, args); + }; + + // Enable and disable network interception to see if disabling network interception + // keeps the patched XMLHttpRequest methods + Interceptor.enableInterception(); + Interceptor.disableInterception(); + + FakeRequest.open(method, url); + + expect(patchedCode).toHaveBeenCalledTimes(1); + + XMLHttpRequest.prototype.open = originalXHROpen; + }); + it('should set network object on calling setRequestHeader', (done) => { const requestHeaders = { 'content-type': 'application/json', token: '9u4hiudhi3bf' };