diff --git a/src/create-passthrough.ts b/src/create-passthrough.ts index 91c54bc..25e3706 100644 --- a/src/create-passthrough.ts +++ b/src/create-passthrough.ts @@ -90,6 +90,15 @@ 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 + } for (var h in fakeXHR.requestHeaders) { xhr.setRequestHeader(h, fakeXHR.requestHeaders[h]); } 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'); });