2014-07-29 09:38:06 +08:00
|
|
|
/*global sharedAjaxResponseBehaviorForZepto_Failure: true, sharedAjaxResponseBehaviorForZepto_Success: true */
|
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
describe("Jasmine Mock Ajax (for toplevel)", function() {
|
2013-01-09 14:40:59 +08:00
|
|
|
var request, anotherRequest, response;
|
|
|
|
var success, error, complete;
|
2013-01-09 15:47:56 +08:00
|
|
|
var client, onreadystatechange;
|
2013-01-09 14:40:59 +08:00
|
|
|
var sharedContext = {};
|
2013-10-12 13:16:22 +08:00
|
|
|
var fakeGlobal, mockAjax;
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
var fakeXMLHttpRequest = jasmine.createSpy('realFakeXMLHttpRequest');
|
2014-11-02 06:13:55 +08:00
|
|
|
fakeGlobal = {
|
|
|
|
XMLHttpRequest: fakeXMLHttpRequest,
|
|
|
|
DOMParser: window.DOMParser,
|
|
|
|
ActiveXObject: window.ActiveXObject
|
|
|
|
};
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.install();
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
success = jasmine.createSpy("onSuccess");
|
|
|
|
error = jasmine.createSpy("onFailure");
|
|
|
|
complete = jasmine.createSpy("onComplete");
|
2013-01-09 15:47:56 +08:00
|
|
|
|
|
|
|
onreadystatechange = function() {
|
2014-11-27 13:25:49 +08:00
|
|
|
if (this.readyState === (this.DONE || 4)) { // IE 8 doesn't support DONE
|
|
|
|
if (this.status === 200) {
|
2014-05-04 10:38:34 +08:00
|
|
|
success(this.responseText, this.textStatus, this);
|
2013-01-09 15:47:56 +08:00
|
|
|
} else {
|
|
|
|
error(this, this.textStatus, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
complete(this, this.textStatus);
|
|
|
|
}
|
|
|
|
};
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when making a request", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
|
|
|
client.send();
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should store URL and transport", function() {
|
|
|
|
expect(request.url).toEqual("example.com/someApi");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should queue the request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.count()).toEqual(1);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow access to the queued request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.first()).toEqual(request);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
2013-10-19 04:05:47 +08:00
|
|
|
it("should allow access to the queued request via index", function() {
|
|
|
|
expect(mockAjax.requests.at(0)).toEqual(request);
|
|
|
|
});
|
|
|
|
|
2013-01-09 14:40:59 +08:00
|
|
|
describe("and then another request", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
anotherRequest = mockAjax.requests.mostRecent();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should queue the next request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.count()).toEqual(2);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow access to the other queued request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.first()).toEqual(request);
|
|
|
|
expect(mockAjax.requests.mostRecent()).toEqual(anotherRequest);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
describe("mockAjax.requests.mostRecent()", function () {
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
describe("when there is one request queued", function () {
|
|
|
|
it("should return the request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.mostRecent()).toEqual(request);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is more than one request", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
|
|
|
client.send();
|
2013-10-12 13:16:22 +08:00
|
|
|
anotherRequest = mockAjax.requests.mostRecent();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the most recent request", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.mostRecent()).toEqual(anotherRequest);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there are no requests", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.requests.reset();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.mostRecent()).toBeUndefined();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("clearAjaxRequests()", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.requests.reset();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove all requests", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.count()).toEqual(0);
|
|
|
|
expect(mockAjax.requests.mostRecent()).toBeUndefined();
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when simulating a response with request.response", function () {
|
|
|
|
describe("and the response is Success", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "text/plain");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2013-11-02 06:51:21 +08:00
|
|
|
response = {status: 200, statusText: "OK", contentType: "text/html", responseText: "OK!"};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(response);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = success;
|
|
|
|
sharedContext.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = response.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = response.contentType;
|
|
|
|
sharedContext.responseText = response.responseText;
|
2014-06-13 21:04:18 +08:00
|
|
|
sharedContext.responseType = response.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = response.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the success handler", function() {
|
|
|
|
expect(success).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call the failure handler", function() {
|
|
|
|
expect(error).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Success(sharedContext);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the response is Success, but with JSON", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "application/json");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2014-06-12 21:35:21 +08:00
|
|
|
var responseObject = {status: 200, statusText: "OK", contentType: "application/json", responseText: '{"foo":"bar"}', responseType: "json"};
|
2013-01-09 14:40:59 +08:00
|
|
|
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(responseObject);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = success;
|
|
|
|
sharedContext.status = responseObject.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = responseObject.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = responseObject.contentType;
|
|
|
|
sharedContext.responseText = responseObject.responseText;
|
2014-06-12 21:35:21 +08:00
|
|
|
sharedContext.responseType = responseObject.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = responseObject.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
|
2013-10-11 04:27:26 +08:00
|
|
|
response = success.calls.mostRecent().args[2];
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the success handler", function() {
|
|
|
|
expect(success).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call the failure handler", function() {
|
|
|
|
expect(error).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2014-06-12 21:35:21 +08:00
|
|
|
it("should return a JavaScript object for XHR2 response", function() {
|
|
|
|
var responseText = sharedContext.responseText;
|
|
|
|
expect(success.calls.mostRecent().args[0]).toEqual(responseText);
|
|
|
|
|
|
|
|
expect(response.responseText).toEqual(responseText);
|
|
|
|
expect(response.response).toEqual({foo: "bar"});
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Success(sharedContext);
|
|
|
|
});
|
|
|
|
|
2014-06-12 21:35:21 +08:00
|
|
|
describe("and the response is Success, and response is overriden", function () {
|
|
|
|
beforeEach(function() {
|
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-11-27 13:25:49 +08:00
|
|
|
client.setRequestHeader("Content-Type", "application/json");
|
2014-06-12 21:35:21 +08:00
|
|
|
client.send();
|
|
|
|
|
|
|
|
request = mockAjax.requests.mostRecent();
|
2014-11-20 05:25:54 +08:00
|
|
|
var responseObject = {status: 200, statusText: "OK", contentType: "application/json", responseText: '{"foo":"bar"}', responseType: 'json'};
|
2014-06-12 21:35:21 +08:00
|
|
|
|
2014-11-20 05:25:54 +08:00
|
|
|
request.respondWith(responseObject);
|
2014-06-12 21:35:21 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = success;
|
|
|
|
sharedContext.status = responseObject.status;
|
|
|
|
sharedContext.statusText = responseObject.statusText;
|
|
|
|
sharedContext.contentType = responseObject.contentType;
|
|
|
|
sharedContext.responseText = responseObject.responseText;
|
|
|
|
sharedContext.responseType = responseObject.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = responseObject.responseURL;
|
2014-06-12 21:35:21 +08:00
|
|
|
|
|
|
|
response = success.calls.mostRecent().args[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the provided override for the XHR2 response", function() {
|
|
|
|
var responseText = sharedContext.responseText;
|
|
|
|
|
|
|
|
expect(response.responseText).toEqual(responseText);
|
|
|
|
expect(response.response).toEqual({foo: "bar"});
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Success(sharedContext);
|
|
|
|
});
|
|
|
|
|
2016-01-13 04:24:19 +08:00
|
|
|
describe("and the response is Success, and responseURL is set", function () {
|
|
|
|
beforeEach(function() {
|
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
|
|
|
client.setRequestHeader("Content-Type", "application/json");
|
|
|
|
client.send();
|
|
|
|
|
|
|
|
request = mockAjax.requests.mostRecent();
|
|
|
|
var responseObject = {status: 200, statusText: "OK", contentType: "application/json", responseText: '{"foo":"bar"}', responseType: 'json', responseURL: 'example.com/redirected'};
|
|
|
|
|
|
|
|
request.respondWith(responseObject);
|
|
|
|
|
|
|
|
sharedContext.responseCallback = success;
|
|
|
|
sharedContext.status = responseObject.status;
|
|
|
|
sharedContext.statusText = responseObject.statusText;
|
|
|
|
sharedContext.contentType = responseObject.contentType;
|
|
|
|
sharedContext.responseText = responseObject.responseText;
|
|
|
|
sharedContext.responseType = responseObject.responseType;
|
|
|
|
sharedContext.responseURL = responseObject.responseURL;
|
|
|
|
|
|
|
|
response = success.calls.mostRecent().args[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Success(sharedContext);
|
|
|
|
});
|
|
|
|
|
2014-07-13 09:06:24 +08:00
|
|
|
describe("response with unique header names using an object", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com");
|
|
|
|
client.send();
|
|
|
|
|
|
|
|
request = mockAjax.requests.mostRecent();
|
|
|
|
var responseObject = {status: 200, statusText: "OK", responseText: '["foo"]', responseHeaders: {
|
2014-07-31 04:16:46 +08:00
|
|
|
'X-Header1': 'header 1 value',
|
|
|
|
'X-Header2': 'header 2 value',
|
|
|
|
'X-Header3': 'header 3 value'
|
2014-07-13 09:06:24 +08:00
|
|
|
}};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(responseObject);
|
2014-07-13 09:06:24 +08:00
|
|
|
response = success.calls.mostRecent().args[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getResponseHeader should return the each value", function () {
|
2014-07-31 04:16:46 +08:00
|
|
|
expect(response.getResponseHeader('X-Header1')).toBe('header 1 value');
|
|
|
|
expect(response.getResponseHeader('X-Header2')).toBe('header 2 value');
|
|
|
|
expect(response.getResponseHeader('X-Header3')).toBe('header 3 value');
|
2014-07-13 09:06:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getAllResponseHeaders should return all values", function () {
|
|
|
|
expect(response.getAllResponseHeaders()).toBe([
|
2014-07-31 04:16:46 +08:00
|
|
|
"X-Header1: header 1 value",
|
|
|
|
"X-Header2: header 2 value",
|
|
|
|
"X-Header3: header 3 value"
|
2015-01-24 05:11:42 +08:00
|
|
|
].join("\r\n") + "\r\n");
|
2014-07-13 09:06:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("response with multiple headers of the same name using an array of objects", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com");
|
|
|
|
client.send();
|
|
|
|
|
|
|
|
request = mockAjax.requests.mostRecent();
|
|
|
|
var responseObject = {status: 200, statusText: "OK", responseText: '["foo"]', responseHeaders: [
|
2014-07-31 04:16:46 +08:00
|
|
|
{ name: 'X-Header', value: 'header value 1' },
|
|
|
|
{ name: 'X-Header', value: 'header value 2' }
|
2014-07-13 09:06:24 +08:00
|
|
|
]};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(responseObject);
|
2014-07-13 09:06:24 +08:00
|
|
|
response = success.calls.mostRecent().args[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getResponseHeader should return all values comma separated", function () {
|
2014-07-31 04:16:46 +08:00
|
|
|
expect(response.getResponseHeader('X-Header')).toBe('header value 1, header value 2');
|
2014-07-13 09:06:24 +08:00
|
|
|
});
|
|
|
|
|
2016-09-30 21:48:24 +08:00
|
|
|
it("returns null, if header does not exist", function() {
|
|
|
|
expect(response.getResponseHeader('X-Does-Not-Exist')).toBe(null);
|
|
|
|
});
|
|
|
|
|
2014-07-13 09:06:24 +08:00
|
|
|
it("getAllResponseHeaders should return all values", function () {
|
|
|
|
expect(response.getAllResponseHeaders()).toBe([
|
2014-07-31 04:16:46 +08:00
|
|
|
"X-Header: header value 1",
|
|
|
|
"X-Header: header value 2"
|
2015-01-24 05:11:42 +08:00
|
|
|
].join("\r\n") + "\r\n");
|
2014-07-13 09:06:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-01-09 14:40:59 +08:00
|
|
|
describe("the content type defaults to application/json", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "application/json");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2014-06-12 21:35:21 +08:00
|
|
|
response = {status: 200, statusText: "OK", responseText: '{"foo": "valid JSON, dammit."}', responseType: 'json'};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(response);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = success;
|
|
|
|
sharedContext.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = response.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = "application/json";
|
2014-06-13 21:04:18 +08:00
|
|
|
sharedContext.responseType = response.responseType;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.responseText = response.responseText;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = response.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the success handler", function() {
|
|
|
|
expect(success).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call the failure handler", function() {
|
|
|
|
expect(error).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Success(sharedContext);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the status/response code is 0", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "text/plain");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2013-11-02 06:51:21 +08:00
|
|
|
response = {status: 0, statusText: "ABORT", responseText: '{"foo": "whoops!"}'};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(response);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = error;
|
|
|
|
sharedContext.status = 0;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = response.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = 'application/json';
|
|
|
|
sharedContext.responseText = response.responseText;
|
2014-06-13 21:04:18 +08:00
|
|
|
sharedContext.responseType = response.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = response.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the success handler", function() {
|
|
|
|
expect(success).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call the failure handler", function() {
|
|
|
|
expect(error).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Failure(sharedContext);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the response is error", function () {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "text/plain");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2013-11-02 06:51:21 +08:00
|
|
|
response = {status: 500, statusText: "SERVER ERROR", contentType: "text/html", responseText: "(._){"};
|
2014-11-18 05:33:19 +08:00
|
|
|
request.respondWith(response);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
|
|
|
sharedContext.responseCallback = error;
|
|
|
|
sharedContext.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = response.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = response.contentType;
|
|
|
|
sharedContext.responseText = response.responseText;
|
2014-06-13 21:04:18 +08:00
|
|
|
sharedContext.responseType = response.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = response.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call the success handler", function() {
|
|
|
|
expect(success).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the failure handler", function() {
|
|
|
|
expect(error).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedAjaxResponseBehaviorForZepto_Failure(sharedContext);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when simulating a response with request.responseTimeout', function() {
|
|
|
|
beforeEach(function() {
|
2013-11-01 00:54:47 +08:00
|
|
|
jasmine.clock().install();
|
2013-01-09 14:40:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
client = new fakeGlobal.XMLHttpRequest();
|
2013-01-09 15:47:56 +08:00
|
|
|
client.onreadystatechange = onreadystatechange;
|
|
|
|
client.open("GET", "example.com/someApi");
|
2014-07-29 09:38:06 +08:00
|
|
|
client.setRequestHeader("Content-Type", "text/plain");
|
2013-01-09 15:47:56 +08:00
|
|
|
client.send();
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
request = mockAjax.requests.mostRecent();
|
2014-06-12 21:35:21 +08:00
|
|
|
response = {contentType: "text/html", response: "(._){response", responseText: "(._){", responseType: "text"};
|
2013-01-09 14:40:59 +08:00
|
|
|
request.responseTimeout(response);
|
|
|
|
|
|
|
|
sharedContext.responseCallback = error;
|
|
|
|
sharedContext.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
sharedContext.statusText = response.statusText;
|
2013-01-09 14:40:59 +08:00
|
|
|
sharedContext.contentType = response.contentType;
|
|
|
|
sharedContext.responseText = response.responseText;
|
2014-06-13 21:04:18 +08:00
|
|
|
sharedContext.responseType = response.responseType;
|
2016-01-13 04:24:19 +08:00
|
|
|
sharedContext.responseURL = response.responseURL;
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
2013-10-11 04:27:26 +08:00
|
|
|
afterEach(function() {
|
2013-11-01 00:54:47 +08:00
|
|
|
jasmine.clock().uninstall();
|
2013-10-11 04:27:26 +08:00
|
|
|
});
|
|
|
|
|
2013-01-09 14:40:59 +08:00
|
|
|
it("should not call the success handler", function() {
|
|
|
|
expect(success).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the failure handler", function() {
|
|
|
|
expect(error).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call the complete handler", function() {
|
|
|
|
expect(complete).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function sharedAjaxResponseBehaviorForZepto_Success(context) {
|
|
|
|
describe("the success response", function () {
|
|
|
|
var xhr;
|
|
|
|
beforeEach(function() {
|
2013-10-11 04:27:26 +08:00
|
|
|
xhr = context.responseCallback.calls.mostRecent().args[2];
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should have the expected status code", function() {
|
|
|
|
expect(xhr.status).toEqual(context.status);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have the expected content type", function() {
|
2014-06-11 04:33:32 +08:00
|
|
|
expect(xhr.getResponseHeader('Content-Type')).toEqual(context.contentType);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
2014-05-04 10:38:34 +08:00
|
|
|
it("should have the expected xhr2 response", function() {
|
2014-11-27 13:25:49 +08:00
|
|
|
var expected = context.response || context.responseType === 'json' ? JSON.parse(context.responseText) : context.responseText;
|
2014-06-12 21:35:21 +08:00
|
|
|
expect(xhr.response).toEqual(expected);
|
2014-05-04 10:38:34 +08:00
|
|
|
});
|
|
|
|
|
2013-01-09 14:40:59 +08:00
|
|
|
it("should have the expected response text", function() {
|
|
|
|
expect(xhr.responseText).toEqual(context.responseText);
|
|
|
|
});
|
2014-07-29 09:38:06 +08:00
|
|
|
|
2013-11-02 06:51:21 +08:00
|
|
|
it("should have the expected status text", function() {
|
|
|
|
expect(xhr.statusText).toEqual(context.statusText);
|
|
|
|
});
|
2016-01-13 04:24:19 +08:00
|
|
|
|
|
|
|
it("should have the expected response URL", function() {
|
|
|
|
expect(xhr.responseURL).toEqual(context.responseURL || null);
|
|
|
|
});
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sharedAjaxResponseBehaviorForZepto_Failure(context) {
|
|
|
|
describe("the failure response", function () {
|
|
|
|
var xhr;
|
|
|
|
beforeEach(function() {
|
2013-10-11 04:27:26 +08:00
|
|
|
xhr = context.responseCallback.calls.mostRecent().args[0];
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should have the expected status code", function() {
|
|
|
|
expect(xhr.status).toEqual(context.status);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have the expected content type", function() {
|
2014-06-11 04:33:32 +08:00
|
|
|
expect(xhr.getResponseHeader('Content-Type')).toEqual(context.contentType);
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
|
2014-05-04 10:38:34 +08:00
|
|
|
it("should have the expected xhr2 response", function() {
|
2014-11-27 13:25:49 +08:00
|
|
|
var expected = context.response || xhr.responseType === 'json' ? JSON.parse(xhr.responseText) : xhr.responseText;
|
2014-06-13 21:04:18 +08:00
|
|
|
expect(xhr.response).toEqual(expected);
|
2014-05-04 10:38:34 +08:00
|
|
|
});
|
|
|
|
|
2013-01-09 14:40:59 +08:00
|
|
|
it("should have the expected response text", function() {
|
|
|
|
expect(xhr.responseText).toEqual(context.responseText);
|
|
|
|
});
|
2014-07-29 09:38:06 +08:00
|
|
|
|
2013-11-02 06:51:21 +08:00
|
|
|
it("should have the expected status text", function() {
|
|
|
|
expect(xhr.statusText).toEqual(context.statusText);
|
|
|
|
});
|
2016-01-13 04:24:19 +08:00
|
|
|
|
|
|
|
it("should have the expected response URL", function() {
|
|
|
|
expect(xhr.responseURL).toEqual(context.responseURL || null);
|
|
|
|
});
|
2013-01-09 14:40:59 +08:00
|
|
|
});
|
|
|
|
}
|