2010-09-29 02:41:50 +08:00
|
|
|
describe("FakeXMLHttpRequest", function() {
|
|
|
|
var xhr;
|
2013-12-03 09:51:21 +08:00
|
|
|
var xhr2;
|
2014-06-13 03:54:56 +08:00
|
|
|
var mockAjax;
|
2010-09-29 02:41:50 +08:00
|
|
|
beforeEach(function() {
|
2014-04-04 22:29:49 +08:00
|
|
|
var realXMLHttpRequest = {someOtherProperty: 'someValue'},
|
|
|
|
realXMLHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(realXMLHttpRequest),
|
2014-06-13 03:54:56 +08:00
|
|
|
fakeGlobal = {XMLHttpRequest: realXMLHttpRequestCtor};
|
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.install();
|
|
|
|
xhr = new fakeGlobal.XMLHttpRequest();
|
2013-12-03 09:51:21 +08:00
|
|
|
xhr2 = new fakeGlobal.XMLHttpRequest();
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
2013-10-12 13:16:22 +08:00
|
|
|
|
2014-06-02 06:37:19 +08:00
|
|
|
function objectKeys(obj) {
|
|
|
|
keys = [];
|
|
|
|
for (key in obj) {
|
|
|
|
if (obj.hasOwnProperty(key)) {
|
|
|
|
keys.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2010-09-29 02:41:50 +08:00
|
|
|
it("should have an initial readyState of 0 (uninitialized)", function() {
|
|
|
|
expect(xhr.readyState).toEqual(0);
|
|
|
|
});
|
2013-01-09 16:03:52 +08:00
|
|
|
|
2013-12-03 09:51:21 +08:00
|
|
|
describe("when setting request headers", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
xhr.setRequestHeader('X-Header-1', 'one');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should make the request headers available", function() {
|
2014-06-02 06:37:19 +08:00
|
|
|
expect(objectKeys(xhr.requestHeaders).length).toEqual(1);
|
2013-12-03 09:51:21 +08:00
|
|
|
expect(xhr.requestHeaders['X-Header-1']).toEqual('one');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when setting headers on another xhr object", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
xhr2.setRequestHeader('X-Header-2', 'two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should make the only its request headers available", function() {
|
2014-06-02 06:37:19 +08:00
|
|
|
expect(objectKeys(xhr2.requestHeaders).length).toEqual(1);
|
2013-12-03 09:51:21 +08:00
|
|
|
expect(xhr2.requestHeaders['X-Header-2']).toEqual('two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not modify any other xhr objects", function() {
|
2014-06-02 06:37:19 +08:00
|
|
|
expect(objectKeys(xhr.requestHeaders).length).toEqual(1);
|
2013-12-03 09:51:21 +08:00
|
|
|
expect(xhr.requestHeaders['X-Header-1']).toEqual('one');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-09-29 02:41:50 +08:00
|
|
|
describe("when opened", function() {
|
|
|
|
beforeEach(function() {
|
2014-01-18 02:41:23 +08:00
|
|
|
spyOn(xhr, 'onreadystatechange');
|
2012-12-20 15:02:08 +08:00
|
|
|
xhr.open("GET", "http://example.com");
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
2014-01-18 02:41:23 +08:00
|
|
|
|
2010-09-29 02:41:50 +08:00
|
|
|
it("should have a readyState of 1 (open)", function() {
|
|
|
|
expect(xhr.readyState).toEqual(1);
|
2014-01-18 02:41:23 +08:00
|
|
|
expect(xhr.onreadystatechange).toHaveBeenCalled();
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when sent", function() {
|
|
|
|
it("should have a readyState of 2 (sent)", function() {
|
2014-01-18 02:41:23 +08:00
|
|
|
xhr.onreadystatechange.calls.reset();
|
2010-09-29 02:41:50 +08:00
|
|
|
xhr.send(null);
|
|
|
|
expect(xhr.readyState).toEqual(2);
|
2014-01-18 02:41:23 +08:00
|
|
|
expect(xhr.onreadystatechange).toHaveBeenCalled();
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a response comes in", function() {
|
|
|
|
it("should have a readyState of 4 (loaded)", function() {
|
2014-01-18 02:41:23 +08:00
|
|
|
xhr.onreadystatechange.calls.reset();
|
2010-09-29 02:41:50 +08:00
|
|
|
xhr.response({status: 200});
|
|
|
|
expect(xhr.readyState).toEqual(4);
|
2014-01-18 02:41:23 +08:00
|
|
|
expect(xhr.onreadystatechange).toHaveBeenCalled();
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when aborted", function() {
|
|
|
|
it("should have a readyState of 0 (uninitialized)", function() {
|
2014-01-18 02:41:23 +08:00
|
|
|
xhr.onreadystatechange.calls.reset();
|
2010-09-29 02:41:50 +08:00
|
|
|
xhr.abort();
|
|
|
|
expect(xhr.readyState).toEqual(0);
|
2014-01-18 02:41:23 +08:00
|
|
|
expect(xhr.onreadystatechange).toHaveBeenCalled();
|
2013-11-02 03:09:55 +08:00
|
|
|
expect(xhr.status).toEqual(0);
|
|
|
|
expect(xhr.statusText).toEqual("abort");
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2011-06-08 11:43:20 +08:00
|
|
|
|
2013-01-09 16:03:52 +08:00
|
|
|
describe("when opened with a username/password", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
xhr.open("GET", "http://example.com", true, "username", "password");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store the username", function() {
|
|
|
|
expect(xhr.username).toEqual("username");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store the password", function() {
|
|
|
|
expect(xhr.password).toEqual("password");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-17 02:40:24 +08:00
|
|
|
describe("data", function() {
|
|
|
|
beforeEach(function() {
|
2012-12-20 15:02:08 +08:00
|
|
|
xhr.open("POST", "http://example.com?this=that");
|
2014-06-11 23:03:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be an empty object if no params were sent", function() {
|
|
|
|
xhr.send();
|
|
|
|
expect(xhr.data()).toEqual({});
|
2012-05-17 02:40:24 +08:00
|
|
|
});
|
|
|
|
|
2014-04-19 01:49:18 +08:00
|
|
|
it("should return request params as a hash of arrays", function() {
|
2014-06-11 23:03:34 +08:00
|
|
|
xhr.send('3+stooges=shemp&3+stooges=larry%20%26%20moe%20%26%20curly&some%3Dthing=else+entirely');
|
2012-05-17 02:40:24 +08:00
|
|
|
var data = xhr.data();
|
2012-05-17 03:09:37 +08:00
|
|
|
expect(data['3 stooges'].length).toEqual(2);
|
2014-04-19 01:49:18 +08:00
|
|
|
expect(data['3 stooges'][0]).toEqual('shemp');
|
|
|
|
expect(data['3 stooges'][1]).toEqual('larry & moe & curly');
|
2012-05-17 03:09:37 +08:00
|
|
|
expect(data['some=thing']).toEqual(['else entirely']);
|
2012-05-17 02:40:24 +08:00
|
|
|
});
|
2014-06-11 23:03:34 +08:00
|
|
|
|
|
|
|
it("should parse json when the content type is appropriate", function() {
|
|
|
|
var data = {
|
|
|
|
foo: 'bar',
|
|
|
|
baz: ['q', 'u', 'u', 'x'],
|
|
|
|
nested: {
|
|
|
|
object: {
|
|
|
|
with: 'stuff'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.send(JSON.stringify(data));
|
|
|
|
|
|
|
|
expect(xhr.data()).toEqual(data);
|
|
|
|
});
|
2014-06-13 03:54:56 +08:00
|
|
|
|
|
|
|
it("should be able to use a custom parser", function() {
|
|
|
|
xhr.send('custom_format');
|
|
|
|
var custom = {
|
|
|
|
test: jasmine.createSpy('test').and.returnValue(true),
|
|
|
|
parse: jasmine.createSpy('parse').and.returnValue('parsedFormat')
|
|
|
|
};
|
|
|
|
mockAjax.addCustomParamParser(custom);
|
|
|
|
expect(xhr.data()).toBe('parsedFormat');
|
|
|
|
expect(custom.test).toHaveBeenCalled();
|
|
|
|
expect(custom.parse).toHaveBeenCalledWith('custom_format');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear custom parsers when uninstalled", function() {
|
|
|
|
var custom = {
|
|
|
|
test: jasmine.createSpy('test').and.returnValue(true),
|
|
|
|
parse: jasmine.createSpy('parse').and.returnValue('parsedFormat')
|
|
|
|
};
|
|
|
|
mockAjax.addCustomParamParser(custom);
|
|
|
|
xhr.send('custom_format');
|
|
|
|
expect(xhr.data()).toBe('parsedFormat');
|
|
|
|
|
|
|
|
mockAjax.uninstall();
|
|
|
|
mockAjax.install();
|
|
|
|
|
|
|
|
xhr.send('custom_format');
|
|
|
|
expect(xhr.data()).toEqual({custom_format: [ 'undefined' ]});
|
|
|
|
});
|
2014-06-11 23:03:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("contentType", function() {
|
|
|
|
it("gets the Content-Type", function() {
|
|
|
|
xhr.setRequestHeader('Content-Type', 'something');
|
|
|
|
expect(xhr.contentType()).toEqual('something');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("gets the content-type even when the casing is not to spec", function() {
|
|
|
|
xhr.setRequestHeader('content-Type', 'some other thing');
|
|
|
|
expect(xhr.contentType()).toEqual('some other thing');
|
|
|
|
});
|
2012-05-17 02:40:24 +08:00
|
|
|
});
|
2014-04-04 22:29:49 +08:00
|
|
|
|
|
|
|
describe("when a fake XMLHttpRequest is created", function() {
|
|
|
|
it("inherits the properties of the real XMLHttpRequest object", function() {
|
|
|
|
expect(xhr.someOtherProperty).toBe('someValue');
|
|
|
|
})
|
|
|
|
})
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|