Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MOB-12425] Properly Disable Network Interception with Patched XHR #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 12 additions & 3 deletions src/utils/XhrNetworkInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions test/utils/XhrNetworkInterceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<XMLHttpRequest['open']>) {
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' };

Expand Down