2015-02-10 19:41:24 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
"use strict";
|
|
|
|
"use server";
|
2015-04-15 14:05:15 +00:00
|
|
|
"use mocha";
|
|
|
|
|
|
|
|
require("c9/inline-mocha")(module);
|
|
|
|
if (typeof define === "undefined") {
|
|
|
|
require("amd-loader");
|
|
|
|
}
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
var assert = require("assert");
|
|
|
|
var fs = require("fs");
|
|
|
|
var tmp = require("tmp");
|
|
|
|
var http = require("http");
|
|
|
|
var localfs = require("vfs-local");
|
|
|
|
var download = require("./download");
|
|
|
|
var urlParse = require('url').parse;
|
|
|
|
var execFile = require('child_process').execFile;
|
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
describe(__filename, function(){
|
|
|
|
this.timeout(4000);
|
2015-02-10 19:41:24 +00:00
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
beforeEach(function(next) {
|
2015-02-10 19:41:24 +00:00
|
|
|
var that = this;
|
|
|
|
var vfs = localfs({root: "/"});
|
|
|
|
download({}, {
|
|
|
|
"Plugin": function() {
|
|
|
|
var that = this;
|
|
|
|
this.freezePublicAPI = function(api) {
|
|
|
|
for (var key in api)
|
|
|
|
that[key] = api[key];
|
|
|
|
};
|
|
|
|
},
|
|
|
|
"vfs.cache": {
|
|
|
|
registerExtension: function() { }
|
|
|
|
}
|
|
|
|
}, function(err, api) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
that.server.listen(8787, "0.0.0.0", next);
|
|
|
|
});
|
2015-04-15 14:05:15 +00:00
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
afterEach(function(next) {
|
2015-02-10 19:41:24 +00:00
|
|
|
this.server.close(next);
|
2015-04-15 14:05:15 +00:00
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
describe("download", function() {
|
|
|
|
|
|
|
|
it("should download", function(next) {
|
|
|
|
tmp.dir({unsafeCleanup: true}, function(err, path) {
|
|
|
|
var filename = path + "/download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(filename);
|
|
|
|
http.get("http://localhost:8787/?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);
|
|
|
|
|
|
|
|
res.on("end", function() {
|
|
|
|
execFile("tar", ["-zxvf", filename, "c9.vfs.server/download.js"], {cwd: path}, function(err, stdout, stderr) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/download.js", "utf8"),
|
|
|
|
fs.readFileSync(path + "/c9.vfs.server/download.js", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
it("should download sub directory", function(next) {
|
|
|
|
tmp.dir({unsafeCleanup: true}, function(err, path) {
|
|
|
|
assert.equal(err, null);
|
2015-02-10 19:41:24 +00:00
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
var filename = path + "/download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(filename);
|
|
|
|
http.get("http://localhost:8787/views?download=download.tar.gz", function(res) {
|
|
|
|
res.pipe(file);
|
|
|
|
|
|
|
|
res.on("end", function() {
|
|
|
|
execFile("tar", ["-zxvf", filename, "views/status.html.ejs"], {cwd: path}, function(err) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/views/status.html.ejs", "utf8"),
|
|
|
|
fs.readFileSync(path + "/views/status.html.ejs", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
it("should download without specifying a name", function(next) {
|
|
|
|
tmp.dir({unsafeCleanup: true}, function(err, path) {
|
|
|
|
assert.equal(err, null);
|
2015-02-10 19:41:24 +00:00
|
|
|
|
2015-04-15 14:05:15 +00:00
|
|
|
var filename = path + "/download.tar.gz";
|
|
|
|
var file = fs.createWriteStream(filename);
|
|
|
|
http.get("http://localhost:8787/views?download", function(res) {
|
|
|
|
assert.equal(res.headers["content-type"], "application/x-gzip");
|
|
|
|
assert.equal(res.headers["content-disposition"], "attachment; filename*=utf-8''views.tar.gz");
|
|
|
|
|
|
|
|
res.pipe(file);
|
|
|
|
|
|
|
|
res.on("end", function() {
|
|
|
|
execFile("tar", ["-zxvf", filename, "views/status.html.ejs"], {cwd: path}, function(err) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(
|
|
|
|
fs.readFileSync(__dirname + "/views/status.html.ejs", "utf8"),
|
|
|
|
fs.readFileSync(path + "/views/status.html.ejs", "utf8")
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-15 14:05:15 +00:00
|
|
|
});
|
|
|
|
});
|