Beef up XHR code in sh.wget(), still failing 2 in PhantomJS due to not having web server

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-26 19:34:32 -04:00
parent 2aa46493fa
commit 60a9dc2470
2 changed files with 26 additions and 4 deletions

View File

@ -444,8 +444,16 @@ define(function(require) {
var path = options.filename || ('file-' + Date.now());
path = Path.resolve(fs.cwd, path);
function onerror() {
callback(new Error('unable to get resource'));
}
var request = new XMLHttpRequest();
request.addEventListener("load", function() {
request.onload = function() {
if(request.status !== 200) {
return onerror();
}
var data = new Uint8Array(request.response);
fs.writeFile(path, data, function(err) {
if(err) {
@ -454,9 +462,12 @@ define(function(require) {
callback(null, path);
}
});
}, false);
request.addEventListener("error", function(err) { callback(err); }, false);
request.open("GET", url);
};
request.onerror = onerror;
request.open("GET", url, true);
if("withCredentials" in request) {
request.withCredentials = true;
}
request.responseType = "arraybuffer";
request.send();
};

View File

@ -20,6 +20,17 @@ define(["Filer", "util"], function(Filer, util) {
});
});
it('should fail when the url does not exist (404)', function(done) {
var fs = util.fs();
var shell = fs.Shell();
shell.wget("no-such-url", function(err, data) {
expect(err).to.exist;
expect(data).not.to.exist;
done();
});
});
it('should download the contents of a file from a url to default filename', function(done) {
var fs = util.fs();
var shell = fs.Shell();