Skip to content

Commit

Permalink
fix-missing-request-url in response (#183)
Browse files Browse the repository at this point in the history
* codejedi365-fix-missing-url > add unit tests

\### Rationale
Define unit tests to highlight that there is an issue with the current
code definition.  1 Test is just to mirror all other tests of setting
the value of the responseURL and the other is to show that the url
is being set to late to be usable.

* codejedi365-fix-missing-url > fix bug in fake-xhr

\### Rationale
Switched the order of when the responseURL is defined to be at a
minimum before `setResponseBody()`. `setResponseBody()` initiates the
`readyStateChange()` which follows on to call the onload function and
return the data to the xhr request handler all before returning to
the respond function to set the url. At this point, it became too late.

On the other side, when using `fetch()` to send the xhr request that
`nise` then intercepts, the `fetch()` command returns the data to the
response handler/resolves the promise during the `onload` event.
Therefore, sinon js did not imitate real browser response which would
contain the url.  This fix resolves that mismatch.

* codejedi365-fix-missing-url > prettier applied
  • Loading branch information
codejedi365 committed May 20, 2021
1 parent 4aef758 commit b1beaee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/fake-xhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,11 @@ function fakeXMLHttpRequestFor(globalScope) {
},

respond: function respond(status, headers, body) {
this.responseURL = this.url;

this.setStatus(status);
this.setResponseHeaders(headers || {});
this.setResponseBody(body || "");

this.responseURL = this.url;
},

uploadProgress: function uploadProgress(progressEventRaw) {
Expand Down
26 changes: 26 additions & 0 deletions lib/fake-xhr/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,32 @@ describe("FakeXMLHttpRequest", function() {
);
});

it("sets response url", function() {
var xhr = new FakeXMLHttpRequest();
var uri = "/";

xhr.open("GET", uri);
xhr.send();
xhr.respond(200, {}, "");

assert.equals(xhr.responseURL, uri);
});

it("url is provided before onload event of the XHR object", function(done) {
var xhr = new FakeXMLHttpRequest();
var uri = "/";
xhr.open("GET", uri);

xhr.onload = function() {
assert.same(this.responseURL, uri);

done();
};

xhr.send();
xhr.respond(200, {}, "");
});

it("sets response text", function() {
this.xhr.respond(200, {}, "'tis some body text");

Expand Down

0 comments on commit b1beaee

Please sign in to comment.