Allow query string and hashes in wget filenames and add tests.

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-05-14 13:24:24 -04:00
parent 670b84eba9
commit 1b12f44a02
2 changed files with 45 additions and 10 deletions

View File

@ -441,15 +441,10 @@ define(function(require) {
return;
}
var path = options.filename || (function() {
// Strip any query string or hash portions of the url first, then
// Grab whatever is after the last / (assuming there is one) and
// remove any non-filename type chars.
var filename = url.replace(/\?.*$/, '');
filename = filename.replace(/\#.*$/, '');
filename = filename.split('/').pop();
return filename.replace(/[^a-zA-Z0-9-_\.\~]/, '');
}());
// Grab whatever is after the last / (assuming there is one) and
// remove any non-filename type chars(i.e., : and /). Like the real
// wget, we leave query string or hash portions in tact.
var path = options.filename || url.replace(/[:/]/g, '').split('/').pop();
path = Path.resolve(fs.cwd, path);
function onerror() {

View File

@ -60,7 +60,6 @@ define(["Filer", "util"], function(Filer, util) {
shell.wget(url, { filename: 'test-file.txt' }, function(err, path) {
if(err) throw err;
// The filename should be something like /file-13424512341
expect(path).to.equal('/test-file.txt');
fs.readFile(path, 'utf8', function(err, data) {
@ -72,5 +71,46 @@ define(["Filer", "util"], function(Filer, util) {
});
});
it('should download the contents of a file from a url with query string', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = "test-file.txt?foo";
var contents = "This is a test file used in some of the tests.\n";
shell.wget(url, function(err, path) {
if(err) throw err;
expect(path).to.equal('/test-file.txt?foo');
fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
it('should download the contents of a file from a url to specified filename, stripping : and /', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = "test-file.txt?foo=:/";
var contents = "This is a test file used in some of the tests.\n";
shell.wget(url, function(err, path) {
if(err) throw err;
expect(path).to.equal('/test-file.txt?foo=');
fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
});
});