2010-09-29 02:41:50 +08:00
|
|
|
describe("FakeXMLHttpRequest", function() {
|
|
|
|
var xhr;
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
var realXMLHttpRequest = jasmine.createSpy('realRequest'),
|
|
|
|
fakeGlobal = {XMLHttpRequest: realXMLHttpRequest},
|
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
mockAjax.install();
|
|
|
|
xhr = new fakeGlobal.XMLHttpRequest();
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
2013-10-12 13:16:22 +08:00
|
|
|
|
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
|
|
|
|
2010-09-29 02:41:50 +08:00
|
|
|
describe("when opened", function() {
|
|
|
|
beforeEach(function() {
|
2012-12-20 15:02:08 +08:00
|
|
|
xhr.open("GET", "http://example.com");
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|
|
|
|
it("should have a readyState of 1 (open)", function() {
|
|
|
|
expect(xhr.readyState).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when sent", function() {
|
|
|
|
it("should have a readyState of 2 (sent)", function() {
|
|
|
|
xhr.send(null);
|
|
|
|
expect(xhr.readyState).toEqual(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a response comes in", function() {
|
|
|
|
it("should have a readyState of 4 (loaded)", function() {
|
|
|
|
xhr.response({status: 200});
|
|
|
|
expect(xhr.readyState).toEqual(4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when aborted", function() {
|
|
|
|
it("should have a readyState of 0 (uninitialized)", function() {
|
|
|
|
xhr.abort();
|
|
|
|
expect(xhr.readyState).toEqual(0);
|
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");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-08-08 04:20:37 +08:00
|
|
|
it("can be extended", function(){
|
2013-10-12 13:16:22 +08:00
|
|
|
pending("why do we want to do this?");
|
2011-08-08 04:20:37 +08:00
|
|
|
FakeXMLHttpRequest.prototype.foo = function(){
|
|
|
|
return "foo";
|
2012-12-20 15:02:08 +08:00
|
|
|
};
|
2011-08-08 04:20:37 +08:00
|
|
|
expect(new FakeXMLHttpRequest().foo()).toEqual("foo");
|
|
|
|
});
|
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");
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it("should return request params as a hash of arrays with values sorted alphabetically", function() {
|
|
|
|
var data = xhr.data();
|
2012-05-17 03:09:37 +08:00
|
|
|
expect(data['3 stooges'].length).toEqual(2);
|
|
|
|
expect(data['3 stooges'][0]).toEqual('larry & moe & curly');
|
|
|
|
expect(data['3 stooges'][1]).toEqual('shemp');
|
|
|
|
expect(data['some=thing']).toEqual(['else entirely']);
|
2012-05-17 02:40:24 +08:00
|
|
|
});
|
|
|
|
});
|
2010-09-29 02:41:50 +08:00
|
|
|
});
|