-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Cherry pick the `FakeXMLHttpRequest` fix from #761 onto the `b3.10` branch. This ugly PR patches `FakeXMLHttpRequest` based on the discoveries from @VictorSoroka1 in pretenderjs/FakeXMLHttpRequest#54 which were introduced by: https://github.com/pretenderjs/FakeXMLHttpRequest/pull/53/files The changes introduced here together with the PR from @VictorSoroka1 in: folio-org/stripes-smart-components#677 should hopefully resolve the test issues. This is a temporary fix until we can get the Mirage dependency updated.
- Loading branch information
Showing
3 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* eslint-disable */ | ||
import FakeXMLHttpRequest from 'fake-xml-http-request'; | ||
|
||
/** | ||
* Monkey patch FakeXMLHttpRequest and remove `response` based on the work in: | ||
* https://github.com/pretenderjs/FakeXMLHttpRequest/pull/53/files | ||
* | ||
* which caused the issue described under: | ||
* | ||
* https://github.com/pretenderjs/FakeXMLHttpRequest/issues/54 | ||
* | ||
*/ | ||
|
||
FakeXMLHttpRequest.prototype.open = function(method, url, async, username, password) { | ||
this.method = method; | ||
this.url = url; | ||
this.async = typeof async == "boolean" ? async : true; | ||
this.username = username; | ||
this.password = password; | ||
this.responseText = null; | ||
this.responseXML = null; | ||
this.responseURL = url; | ||
this.requestHeaders = {}; | ||
this.sendFlag = false; | ||
delete this.response; | ||
this._readyStateChange(FakeXMLHttpRequest.OPENED); | ||
} | ||
|
||
FakeXMLHttpRequest.prototype.abort = function() { | ||
this.aborted = true; | ||
this.responseText = null; | ||
delete this.response; | ||
this.errorFlag = true; | ||
this.requestHeaders = {}; | ||
|
||
this.dispatchEvent(new _Event("abort", false, false, this)); | ||
|
||
if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) { | ||
this._readyStateChange(FakeXMLHttpRequest.UNSENT); | ||
this.sendFlag = false; | ||
} | ||
|
||
if (typeof this.onerror === "function") { | ||
this.onerror(); | ||
} | ||
} | ||
|
||
FakeXMLHttpRequest.prototype._setResponseBody = function (body) { | ||
verifyRequestSent(this); | ||
verifyHeadersReceived(this); | ||
verifyResponseBodyType(body); | ||
|
||
var chunkSize = this.chunkSize || 10; | ||
var index = 0; | ||
this.responseText = ""; | ||
delete this.response; | ||
|
||
do { | ||
if (this.async) { | ||
this._readyStateChange(FakeXMLHttpRequest.LOADING); | ||
} | ||
|
||
this.responseText += body.substring(index, index + chunkSize); | ||
index += chunkSize; | ||
} while (index < body.length); | ||
|
||
var type = this.getResponseHeader("Content-Type"); | ||
|
||
if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) { | ||
try { | ||
this.responseXML = parseXML(this.responseText); | ||
} catch (e) { | ||
// Unable to parse XML - no biggie | ||
} | ||
} | ||
|
||
if (this.async) { | ||
this._readyStateChange(FakeXMLHttpRequest.DONE); | ||
} else { | ||
this.readyState = FakeXMLHttpRequest.DONE; | ||
} | ||
}; | ||
|
||
function verifyRequestSent(xhr) { | ||
if (xhr.readyState == FakeXMLHttpRequest.DONE) { | ||
throw new Error("Request done"); | ||
} | ||
} | ||
|
||
function verifyHeadersReceived(xhr) { | ||
if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) { | ||
throw new Error("No headers received"); | ||
} | ||
} | ||
|
||
function verifyResponseBodyType(body) { | ||
if (typeof body != "string") { | ||
var error = new Error("Attempted to respond to fake XMLHttpRequest with " + | ||
body + ", which is not a string."); | ||
error.name = "InvalidBodyException"; | ||
throw error; | ||
} | ||
} | ||
|
||
/* | ||
Cross-browser XML parsing. Used to turn | ||
XML responses into Document objects | ||
Borrowed from JSpec | ||
*/ | ||
function parseXML(text) { | ||
var xmlDoc; | ||
|
||
if (typeof DOMParser != "undefined") { | ||
var parser = new DOMParser(); | ||
xmlDoc = parser.parseFromString(text, "text/xml"); | ||
} else { | ||
xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); | ||
xmlDoc.async = "false"; | ||
xmlDoc.loadXML(text); | ||
} | ||
|
||
return xmlDoc; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters