Skip to content

Commit 55218bf

Browse files
a7medevHeshamMegid
authored andcommitted
[MOB-12425] Properly Disable Network Interception with Patched XHR (#984)
Modify the XHR interceptor to get the original XMLHttpRequest methods when enabling it and not globally on import, allowing users to patch XHR when network interception is disabled.
1 parent c7f00e5 commit 55218bf

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Fix an issue with unhandled JavaScript crashes being reported as native Android crashes ([#980](https://github.com/Instabug/Instabug-React-Native/pull/980)).
1212
- 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).
1313
- 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).
14+
- 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).
1415

1516
## [11.10.0](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.1...11.10.0) (April 20, 2023)
1617

src/utils/XhrNetworkInterceptor.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export interface NetworkData {
2424
}
2525

2626
const XMLHttpRequest = global.XMLHttpRequest;
27-
const originalXHROpen = XMLHttpRequest.prototype.open;
28-
const originalXHRSend = XMLHttpRequest.prototype.send;
29-
const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
27+
let originalXHROpen = XMLHttpRequest.prototype.open;
28+
let originalXHRSend = XMLHttpRequest.prototype.send;
29+
let originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
3030

3131
let onProgressCallback: ProgressCallback | null;
3232
let onDoneCallback: NetworkDataCallback | null;
@@ -63,6 +63,15 @@ export default {
6363
onProgressCallback = callback;
6464
},
6565
enableInterception() {
66+
// Prevents infinite calls to XMLHttpRequest.open when enabling interception multiple times
67+
if (isInterceptorEnabled) {
68+
return;
69+
}
70+
71+
originalXHROpen = XMLHttpRequest.prototype.open;
72+
originalXHRSend = XMLHttpRequest.prototype.send;
73+
originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
74+
6675
XMLHttpRequest.prototype.open = function (method, url, ...args) {
6776
_reset();
6877
network.url = url;

test/utils/XhrNetworkInterceptor.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ describe('Network Interceptor', () => {
2929
FakeRequest.send();
3030
});
3131

32+
it('should keep patched XMLHttpRequest methods', () => {
33+
Interceptor.disableInterception();
34+
35+
// Patch XMLHttpRequest.open
36+
const originalXHROpen = XMLHttpRequest.prototype.open;
37+
const patchedCode = jest.fn();
38+
XMLHttpRequest.prototype.open = function (...args: Parameters<XMLHttpRequest['open']>) {
39+
patchedCode();
40+
originalXHROpen.apply(this, args);
41+
};
42+
43+
// Enable and disable network interception to see if disabling network interception
44+
// keeps the patched XMLHttpRequest methods
45+
Interceptor.enableInterception();
46+
Interceptor.disableInterception();
47+
48+
FakeRequest.open(method, url);
49+
50+
expect(patchedCode).toHaveBeenCalledTimes(1);
51+
52+
XMLHttpRequest.prototype.open = originalXHROpen;
53+
});
54+
3255
it('should set network object on calling setRequestHeader', (done) => {
3356
const requestHeaders = { 'content-type': 'application/json', token: '9u4hiudhi3bf' };
3457

0 commit comments

Comments
 (0)