Skip to content

XMLHttpRequest's responseType json is ignored by passthrough #325

@jvalatkevicius

Description

@jvalatkevicius

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions