Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Add basic response parsing tests for xml, json, and text.
Browse files Browse the repository at this point in the history
  • Loading branch information
garlicnation committed Nov 5, 2014
1 parent 14cd00c commit 0ffc55b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
2 changes: 2 additions & 0 deletions test/core-ajax-progress.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@
ajax.url="http://example.org/downloadLargeFile"
cb();
},
flush,
animationFrameFlush,
function(cb) {
requests[0].dispatchEvent(progressEvent(true, 10, 100));
cb();
},
flush,
animationFrameFlush,
function(cb) {
assert(ajax.loading === true,
Expand Down
5 changes: 2 additions & 3 deletions test/core-ajax-race.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,25 @@
ajax.url="http://example.org/request1"
cb();
},
flush,
animationFrameFlush,
function(cb) {
ajax.url="http://example.org/request2"
cb();
},
flush,
animationFrameFlush,
function(cb) {
debugger;
requests[0].respond(200, headers, body("http://example.org/request2"));
cb();
},
flush,
function(cb) {
debugger;
requests[1].respond(200, headers, body("http://example.org/request1"));
cb();
},
flush,
function(cb) {
debugger
assert(ajax.response.url.match('request1'),
"Race condition detected. An earlier request's delayed response " +
"caused the more recent request's response to be overwritten.");
Expand Down
100 changes: 96 additions & 4 deletions test/core-ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,101 @@
ajax.body = '';
});
suite('handleAs', function() {
suite('text', function(){});
suite('xml', function(){});
suite('json', function(){});
suite('text', function(){
var headers = {
"Content-Type": "text/plain"
};
setup(function(done){
async.series([
function(cb){
ajax.handleAs = 'text';
ajax.url = "http://example.com/text"
ajax.auto = true;
cb();
},
flush,
function(cb) {
requestAnimationFrame(function(){
cb();
});
},
function(cb){
requests[0].respond(200, headers, "test text");
cb();
}
], done);
});
test('Raw text should pass through', function(){
assert.equal(ajax.response, "test text")
})
});
suite('xml', function(){
var headers = {
"Content-Type": "text/xml"
};
setup(function(done){
async.series([
function(cb){
ajax.handleAs = 'xml';
ajax.url = "http://example.com/xml"
ajax.auto = true;
cb();
},
flush,
function(cb) {
requestAnimationFrame(function(){
cb();
});
},
function(cb){
requests[0].respond(200, headers,
"<note>" +
"<to>AJ</to>" +
"<from>Dog</from>" +
"<subject>Reminder</subject>" +
"<body><q>Feed me!</q></body>" +
"</note>");
cb();
}
], done);
});
test('XML should be returned with queryable structure', function(){
var q = ajax.response.querySelector("note body q");
assert.equal(q.innerHTML, "Feed me!");
var to = ajax.response.querySelector("to");
assert.equal(to.innerHTML, "AJ");
})});
suite('json', function(){
var headers = {
"Content-Type": "text/json"
};
setup(function(done){
async.series([
function(cb){
ajax.handleAs = 'json';
ajax.url = "http://example.com/json"
ajax.auto = true;
cb();
},
flush,
function(cb) {
requestAnimationFrame(function(){
cb();
});
},
function(cb){
requests[0].respond(200, headers,
'{"object" : {"list" : [2, 3, {"key": "value"}]}}');
cb();
}
], done);
});
test('JSON should be returned as an Object', function(){
var r = ajax.response;
assert.equal(r.object.list[1], 3);
assert.equal(r.object.list[2].key, "value");
});
});
suite('arraybuffer', function(){});
suite('blob', function(){});
suite('document', function(){});
Expand All @@ -57,7 +149,7 @@
suite('core-error', function(){});
suite('core-complete', function(){});
});
}
});
</script>

</body>
Expand Down

0 comments on commit 0ffc55b

Please sign in to comment.