Skip to content

Commit

Permalink
Merge pull request #1178 from dottedmag/feature-xhr-set-status
Browse files Browse the repository at this point in the history
Expose XHR.setStatus to simplify asynchronous answers
  • Loading branch information
mroderick committed Nov 11, 2016
2 parents 9622a73 + 8f067e8 commit c458047
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
10 changes: 7 additions & 3 deletions docs/current/fake-xhr-and-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ The filter will be called when `xhr.open` is called, with the exact same argumen

### Simulating server responses

#### `request.setStatus(status);``

Sets response status (`status` and `statusText` properties).

Status should be a number, the status text is looked up from `sinon.FakeXMLHttpRequest.statusCodes`.

#### `request.setResponseHeaders(object);``

Sets response headers (e.g. `{ "Content-Type": "text/html", /* ... */ }`, updates the `readyState` property and fires `onreadystatechange`.
Expand All @@ -170,9 +176,7 @@ Additionally, populates `responseXML` with a parsed document if [response header

#### `request.respond(status, headers, body);``

Calls the above two methods and sets the `status` and `statusText` properties.

Status should be a number, the status text is looked up from `sinon.FakeXMLHttpRequest.statusCodes`.
Calls the above three methods.

#### `request.error();`

Expand Down
11 changes: 9 additions & 2 deletions lib/sinon/util/fake_xml_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ extend(FakeXMLHttpRequest.prototype, sinonEvent.EventTarget, {
}
},

setStatus: function setStatus(status) {
var sanitizedStatus = typeof status === "number" ? status : 200;

verifyRequestOpened(this);
this.status = sanitizedStatus;
this.statusText = FakeXMLHttpRequest.statusCodes[sanitizedStatus];
},

// Helps testing
setResponseHeaders: function setResponseHeaders(headers) {
verifyRequestOpened(this);
Expand Down Expand Up @@ -629,8 +637,7 @@ extend(FakeXMLHttpRequest.prototype, sinonEvent.EventTarget, {
},

respond: function respond(status, headers, body) {
this.status = typeof status === "number" ? status : 200;
this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
this.setStatus(status);
this.setResponseHeaders(headers || {});
this.setResponseBody(body || "");
},
Expand Down
52 changes: 52 additions & 0 deletions test/util/fake-xml-http-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,58 @@ if (typeof window !== "undefined") {
});
});

describe(".setStatus", function () {
beforeEach(function () {
this.xhr = new FakeXMLHttpRequest();
});

it("cannot be set on closed request", function () {
assert.exception(function () {
this.xhr.setStatus();
});
});

it("cannot be set on unsent request", function () {
this.xhr.open("GET", "/");

assert.exception(function () {
this.xhr.setStatus();
});
});

it("by default sets status to 200", function () {
this.xhr.open("GET", "/");
this.xhr.send();
this.xhr.setStatus();

assert.equals(this.xhr.status, 200);
assert.equals(this.xhr.statusText, "OK");
});

it("sets status", function () {
var expectedStatus = 206;
var xhr = this.xhr;

xhr.open("GET", "/");
xhr.send();
xhr.setStatus(expectedStatus);

assert.equals(xhr.status, expectedStatus);
});

it("sets status text to the value from FakeXMLHttpRequest.statusCodes", function () {
var status = 206;
var expectedStatusText = FakeXMLHttpRequest.statusCodes[status];
var xhr = this.xhr;

xhr.open("GET", "/");
xhr.send();
xhr.setStatus(status);

assert.equals(xhr.statusText, expectedStatusText);
});
});

describe(".setResponseBodyAsync", function () {
beforeEach(function () {
this.xhr = new FakeXMLHttpRequest();
Expand Down

0 comments on commit c458047

Please sign in to comment.