2016-06-26 11:53:19 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
"use strict";
|
|
|
|
"use server";
|
|
|
|
|
|
|
|
|
|
|
|
require("c9/inline-mocha")(module);
|
2017-02-07 18:41:27 +00:00
|
|
|
require("amd-loader");
|
2016-06-26 11:53:19 +00:00
|
|
|
|
|
|
|
var assert = require("assert");
|
|
|
|
var fs = require("fs");
|
|
|
|
var http = require("http");
|
2017-02-07 18:41:27 +00:00
|
|
|
var mkdirp = require("mkdirp");
|
2016-06-26 11:53:19 +00:00
|
|
|
var localfs = require("vfs-local");
|
|
|
|
var download = require("./download");
|
|
|
|
var urlParse = require('url').parse;
|
|
|
|
var execFile = require('child_process').execFile;
|
|
|
|
|
2017-01-30 11:32:54 +00:00
|
|
|
describe(__filename, function() {
|
2016-06-26 11:53:19 +00:00
|
|
|
this.timeout(4000);
|
|
|
|
var base;
|
2017-02-07 18:41:27 +00:00
|
|
|
var id = 0;
|
2016-06-26 11:53:19 +00:00
|
|
|
|
2017-02-07 18:41:27 +00:00
|
|
|
before(function(next) {
|
2016-06-26 11:53:19 +00:00
|
|
|
var that = this;
|
2017-01-30 11:32:54 +00:00
|
|
|
var vfs = localfs({ root: "/" });
|
2016-06-26 11:53:19 +00:00
|
|
|
download({}, {
|
|
|
|
"Plugin": function() {
|
|
|
|
var that = this;
|
|
|
|
this.freezePublicAPI = function(api) {
|
|
|
|
for (var key in api)
|
|
|
|
that[key] = api[key];
|
|
|
|
};
|
|
|
|
},
|
|
|
|
"vfs.cache": {
|
2017-01-30 11:32:54 +00:00
|
|
|
registerExtension: function() { }
|
2016-06-26 11:53:19 +00:00
|
|
|
}
|
|
|
|
}, function(err, api) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
var download = api["vfs.download"].download;
|
|
|
|
that.server = http.createServer(function(req, res, next) {
|
|
|
|
req.uri = urlParse(req.url, true);
|
|
|
|
download(vfs, __dirname, req, res, function(err) {
|
|
|
|
console.log("download failed", err);
|
|
|
|
assert.fail(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function tryNext(retries, err) {
|
|
|
|
if (retries < 0) return next(err);
|
|
|
|
var port = 20000 + Math.round(Math.random() * 20000);
|
|
|
|
base = "http://localhost:" + port;
|
|
|
|
that.server.listen(port, "localhost", function() {
|
|
|
|
if (err) return tryNext(retries - 1, err);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tryNext(4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-07 18:41:27 +00:00
|
|
|
after(function(next) {
|
2016-06-26 11:53:19 +00:00
|
|
|
this.server.close(next);
|
|
|
|
});
|
2017-02-07 18:41:27 +00:00
|
|
|
|
|
|
|
before(cleanup);
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
function cleanup(next) {
|
|
|
|
execFile("rm", ["-rf", __dirname + "/_test"], function() {
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tmpDir(callback) {
|
|
|
|
var path = __dirname + "/_test/" + id++;
|
|
|
|
mkdirp(path, function(err) {
|
|
|
|
callback(err, path);
|
|
|
|
});
|
|
|
|
}
|
2016-06-26 11:53:19 +00:00
|
|
|
|
|
|
|
describe("download", function() {
|
|
|
|
|
|
|
|
it("should download as tar", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
var filename = "download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/?download=download.tar.gz", function(res) {
|
|
|
|
assert.equal(res.headers["content-type"], "application/x-gzip");
|
|
|
|
assert.equal(res.headers["content-disposition"], "attachment; filename*=utf-8''download.tar.gz");
|
|
|
|
|
|
|
|
res.pipe(file);
|
|
|
|
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("tar", ["-zxvf", filename, "c9.vfs.server/download.js"], { cwd: path }, function(err, stdout, stderr) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/download.js", "utf8"),
|
|
|
|
fs.readFileSync(path + "/c9.vfs.server/download.js", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download sub directory as tar", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test?download=download.tar.gz", function(res) {
|
|
|
|
res.pipe(file);
|
|
|
|
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("tar", ["-zxvf", filename, "test/dir1/testdata1.txt"], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir1/testdata1.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/test/dir1/testdata1.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download without specifying a name", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test?download", function(res) {
|
|
|
|
assert.equal(res.headers["content-type"], "application/x-gzip");
|
|
|
|
assert.equal(res.headers["content-disposition"], "attachment; filename*=utf-8''test.tar.gz");
|
|
|
|
|
|
|
|
res.pipe(file);
|
|
|
|
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("tar", ["-zxvf", filename, "test/dir1/testdata1.txt"], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir1/testdata1.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/test/dir1/testdata1.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download several files in same directory as tar", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test/dir2/testdata2a.txt,/test/dir2/testdata2b.txt?download=download.tar.gz", function(res) {
|
|
|
|
res.pipe(file);
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("tar", ["-zxvf", filename], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2a.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/testdata2a.txt", "utf8")
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2b.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/testdata2b.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download several files in different directories as tar", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test/dir1/testdata1.txt,/test/dir2/testdata2a.txt?download=download.tar.gz", function(res) {
|
|
|
|
res.pipe(file);
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("tar", ["-zxvf", filename], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir1/testdata1.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/dir1/testdata1.txt", "utf8")
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2a.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/dir2/testdata2a.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download as zip", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
var filename = "download.zip";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/?download=download.zip", function(res) {
|
|
|
|
assert.equal(res.headers["content-type"], "application/zip");
|
|
|
|
assert.equal(res.headers["content-disposition"], "attachment; filename*=utf-8''download.zip");
|
|
|
|
|
|
|
|
res.pipe(file);
|
|
|
|
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("unzip", [filename, "c9.vfs.server/download.js"], { cwd: path }, function(err, stdout, stderr) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/download.js", "utf8"),
|
|
|
|
fs.readFileSync(path + "/c9.vfs.server/download.js", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download sub directory as zip", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.zip";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test?download=download.zip", function(res) {
|
|
|
|
res.pipe(file);
|
|
|
|
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("unzip", [filename, "test/dir1/testdata1.txt"], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir1/testdata1.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/test/dir1/testdata1.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download several files in same directory as zip", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.zip";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test/dir2/testdata2a.txt,/test/dir2/testdata2b.txt?download=download.zip", function(res) {
|
|
|
|
res.pipe(file);
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("unzip", [filename], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2a.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/testdata2a.txt", "utf8")
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2b.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/testdata2b.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should download several files in different directories as zip", function(next) {
|
2017-02-07 18:41:27 +00:00
|
|
|
tmpDir(function(err, path) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
|
|
|
|
var filename = "download.zip";
|
|
|
|
var file = fs.createWriteStream(path + "/" + filename);
|
|
|
|
http.get(base + "/test/dir1/testdata1.txt,/test/dir2/testdata2a.txt?download=download.zip", function(res) {
|
|
|
|
res.pipe(file);
|
2017-02-27 13:37:29 +00:00
|
|
|
file.on("finish", function() {
|
2017-01-30 11:32:54 +00:00
|
|
|
execFile("unzip", [filename], { cwd: path }, function(err) {
|
2016-06-26 11:53:19 +00:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir1/testdata1.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/dir1/testdata1.txt", "utf8")
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/test/dir2/testdata2a.txt", "utf8"),
|
|
|
|
fs.readFileSync(path + "/dir2/testdata2a.txt", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|