-
Notifications
You must be signed in to change notification settings - Fork 158
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
Fix - Add default timeout to XMLHttpRequest #326
Conversation
Not sure who I should mention, but based on the contributor list and organization: |
This looks fine. I think if you can fix the failing test to say That said tho, I'm confused by the fact
Do you know in your case is there code that deletes the Also sync xhr are not allowed to set timeout in a document environment per MDN
The following code will throw (https://codepen.io/xg-wang/pen/BaRmXOR)
|
No, I'm not changing the request directly nor indirectly. The package being used by According to the source, this is the package Pretender is utilizing: xmlhttprequest-ssl |
@bombillazo ah that ponyfill is not spec compatible, looks like repo does not have issue section, not sure if we can submit a bug report to We can add a compatibility check here but I'd want to add more context to the change, something like diff --git a/src/create-passthrough.ts b/src/create-passthrough.ts
index f2b3c1d..25e3706 100644
--- a/src/create-passthrough.ts
+++ b/src/create-passthrough.ts
@@ -90,6 +90,12 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {
xhr.timeout = fakeXHR.timeout;
xhr.withCredentials = fakeXHR.withCredentials;
}
+ // XMLHttpRequest.timeout default initializes to 0, and is not allowed to be used for
+ // synchronous XMLHttpRequests requests in a document environment. However, when a XHR
+ // polyfill does not sets the timeout value, it will throw in React Native environment.
+ // TODO:
+ // synchronous XHR is deprecated, make async the default as XMLHttpRequest.open(),
+ // and throw error if sync XHR has timeout not 0
if (!xhr.timeout) {
xhr.timeout = 0; // default XMLHttpRequest timeout
}
diff --git a/test/passthrough_test.js b/test/passthrough_test.js
index 5734ee5..c26685b 100644
--- a/test/passthrough_test.js
+++ b/test/passthrough_test.js
@@ -112,7 +112,7 @@ describe('passthrough requests', function(config) {
}
);
- it('synchronous request does not have timeout, withCredentials and onprogress event', function(
+ it('synchronous request has timeout=0, withCredentials and onprogress event', function(
assert
) {
var pretender = this.pretender;
@@ -125,7 +125,7 @@ describe('passthrough requests', function(config) {
this.send = {
pretender: pretender,
apply: function(xhr/*, argument*/) {
- assert.ok(!('timeout' in xhr));
+ assert.equal(xhr.timeout, 0);
assert.ok(!('withCredentials' in xhr));
assert.ok(!('onprogress' in xhr));
this.pretender.resolve(xhr);
@@ -139,7 +139,6 @@ describe('passthrough requests', function(config) {
var xhr = new window.XMLHttpRequest();
xhr.open('POST', '/some/path', false);
- xhr.timeout = 1000;
xhr.withCredentials = true;
xhr.send('some data');
});
|
This PR appears to have created a new bug:
We may need to revisit this bugfix and find a way to solve the original problem without introducing a new regression for scenarios where pretender was previously working. |
This proposed change is to fix a bug when using Pretender with React Native in Android. If the Pretender request is passthrough and synchronous, Pretender does not assign a timeout to the xhr request variable. When the request is dispatched and reaches native code, it errors out:
This is due to the Android native library expecting a numeric
timeout
value.NativeNetworkingAndroid.js Code
NetworkingModule.java Code
To Dos:
passthrough_test.js
since now we are always adding a timeout, and this test will fail.