Commit b84771ef b84771ef9e18c062cf5d7905c0f114a84d89bec9 by Nicolas Perriault

better implementation of Casper.download()

1 parent 1932b913
......@@ -293,10 +293,20 @@
return this.exit(Number(status) > 0 ? Number(status) : 1);
},
/**
* Downloads a resource and saves it on the filesystem.
*
* @param String url The url of the resource to download
* @param String targetPath The destination file path
* @return Casper
*/
download: function(url, targetPath) {
var fs = require('fs');
var cu = new phantom.Casper.ClientUtils();
fs.write(targetPath, cu.decode(this.base64encode(url)), 'w');
try {
require('fs').write(targetPath, cu.decode(this.base64encode(url)), 'w');
} catch (e) {
this.log("Error while downloading " + url + " to " + targetPath + ": " + e, "error");
}
return this;
},
......
......@@ -74,44 +74,44 @@
* @return string
*/
this.decode = function(str) {
// var c1, c2, c3, c4, i = 0, len = str.length, out = "";
// while (i < len) {
// do {
// c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
// } while (i < len && c1 == -1);
// if (c1 == -1) {
// break;
// }
// do {
// c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
// } while (i < len && c2 == -1);
// if (c2 == -1) {
// break;
// }
// out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
// do {
// c3 = str.charCodeAt(i++) & 0xff;
// if (c3 == 61)
// return out;
// c3 = BASE64_DECODE_CHARS[c3];
// } while (i < len && c3 == -1);
// if (c3 == -1) {
// break;
// }
// out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
// do {
// c4 = str.charCodeAt(i++) & 0xff;
// if (c4 == 61) {
// return out;
// }
// c4 = BASE64_DECODE_CHARS[c4];
// } while (i < len && c4 == -1);
// if (c4 == -1) {
// break;
// }
// out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
// }
// return out;
var c1, c2, c3, c4, i = 0, len = str.length, out = "";
while (i < len) {
do {
c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
} while (i < len && c1 == -1);
if (c1 == -1) {
break;
}
do {
c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
} while (i < len && c2 == -1);
if (c2 == -1) {
break;
}
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
do {
c3 = str.charCodeAt(i++) & 0xff;
if (c3 == 61)
return out;
c3 = BASE64_DECODE_CHARS[c3];
} while (i < len && c3 == -1);
if (c3 == -1) {
break;
}
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
do {
c4 = str.charCodeAt(i++) & 0xff;
if (c4 == 61) {
return out;
}
c4 = BASE64_DECODE_CHARS[c4];
} while (i < len && c4 == -1);
if (c4 == -1) {
break;
}
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
};
/**
......
......@@ -11,7 +11,7 @@
"Les nuages filent et le ciel s'éclaircit\n" +
"Et dans ma tête qui bourdonnent?\n" +
"Les abeilles!"),
'a file contents': fs.read(phantom.casperPath + '/tests/site/images/phantom.png')
'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
};
for (var what in testCases) {
......
(function(t) {
t.comment('Casper.base64encode()');
casper.start('tests/site/index.html', function(self) {
var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
var image = self.base64encode(imageUrl);
var fs = require('fs');
t.comment('Casper.base64encode()');
t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
t.comment('Casper.download()');
self.download(imageUrl, 'logo.png');
var fs = require('fs');
t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file');
if (fs.exists('logo.png')) {
fs.remove('logo.png');
}
});
casper.run(function(self) {
......