Skip to content

Commit

Permalink
Add test for beforesend listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 10, 2013
1 parent 9f59f94 commit 8e8fcbe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions test/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ global.XMLHttpRequest = function XMLHttpRequest() {

// TODO handle file system errors?

self.readyState = 0;

self.open = function(m, u, a) {
info.url = u;
info.async = a;
self.readyState = 1;
self.send = a ? read : readSync;
};

Expand All @@ -19,6 +22,7 @@ global.XMLHttpRequest = function XMLHttpRequest() {
};

function read() {
self.readyState = 2;
fs.readFile(info.url, "binary", function(e, d) {
if (e) {
self.status = 404; // assumed
Expand All @@ -35,6 +39,7 @@ global.XMLHttpRequest = function XMLHttpRequest() {
}

function readSync() {
self.readyState = 2;
try {
var d = fs.readFileSync(info.url, "binary");
self.status = 200;
Expand Down
37 changes: 31 additions & 6 deletions test/xhr/xhr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ suite.addBatch({
topic: load("xhr/xhr").expression("d3.xhr").document(),

"on a sample text file": {
topic: function(xhr) {
xhr("test/data/sample.txt", this.callback);
topic: function(d3_xhr) {
d3_xhr("test/data/sample.txt", this.callback);
},
"makes an asynchronous HTTP request": function(req) {
assert.equal(req._info.url, "test/data/sample.txt");
Expand All @@ -29,18 +29,43 @@ suite.addBatch({
},

"when a custom mime type is specified": {
topic: function(xhr) {
xhr("test/data/sample.txt", "text/plain", this.callback);
topic: function(d3_xhr) {
d3_xhr("test/data/sample.txt", "text/plain", this.callback);
},
"observes the optional mime type": function(req) {
assert.equal(req._info.mimeType, "text/plain");
}
},

"when a beforesend listener is specified": {
topic: function(d3_xhr) {
var callback = this.callback;
var xhr = d3_xhr("test/data/sample.txt", "text/plain").on("beforesend", function(request) {
callback(null, {
that: this,
xhr: xhr,
readyState: request.readyState,
request: request
});
});
xhr.get();
},
"invokes the beforesend listener with the xhr object as the context": function(result) {
assert.equal(result.that, result.xhr);
assert.ok(result.xhr.get);
},
"invokes the beforesend listener with the underlying XMLHttpRequest as an argument": function(result) {
assert.instanceOf(result.request, XMLHttpRequest);
},
"invokes the beforesend listener after open and before send": function(result) {
assert.equal(result.readyState, 1);
}
},

"on a file that does not exist": {
topic: function(xhr) {
topic: function(d3_xhr) {
var callback = this.callback;
xhr("//does/not/exist.txt", function(error, req) {
d3_xhr("//does/not/exist.txt", function(error, req) {
callback(null, req);
});
},
Expand Down

0 comments on commit 8e8fcbe

Please sign in to comment.