Issue
Seems like pretender's passthrough does not take into account the responseType value that was set in original XMLHttpRequest object.
The issue can be reproduced by the following snippet:
import Pretender from "pretender";
const endpoint = "https://jsonplaceholder.typicode.com/todos/1";
new Pretender(function () {
this.get(endpoint, this.passthrough);
});
const xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.onload = () => {
const response = xhr.response;
// expected to be 'object'
console.log(typeof response);
};
xhr.open("GET", endpoint);
xhr.send();
What it does is creates pretender instance with passthrough get endpoint and XMLHttpRequest with responseType = 'json'. After the xhr request is sent, the type of result is logged into console.
Expected result
Since xhr request's responseType is set to json the type of result should be object. You can observe this behaviour by deleting code, that creates pretender instance so that xhr requests are not routed through passthrough middleware but instead are handled directly by the browser.
Actual result
Type of response is string
Issue
Seems like pretender's
passthroughdoes not take into account theresponseTypevalue that was set in originalXMLHttpRequestobject.The issue can be reproduced by the following snippet:
What it does is creates pretender instance with passthrough
getendpoint and XMLHttpRequest withresponseType = 'json'. After thexhrrequest is sent, the type of result is logged into console.Expected result
Since
xhrrequest'sresponseTypeis set tojsonthe type of result should beobject. You can observe this behaviour by deleting code, that creates pretender instance so that xhr requests are not routed throughpassthroughmiddleware but instead are handled directly by the browser.Actual result
Type of response is
string