download.js 4.06 KB
Newer Older
Muddsair Sharif's avatar
Muddsair Sharif committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var assert = require('assert');
var app = require('./service/app');
var util = require('utils-extend');
var request = require('../index');
var fs = require('file-system');
var path = require('path');
var srcPath = path.join(__dirname, 'service/assets');
var rootPath = path.join(__dirname, 'download');
var server;

describe('Download request', function() {
  // Start service
  beforeEach(function(done){
    server = app.listen(3100, function() {
      done();
    });
  });

  it('html', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/index.html',
      rootPath: rootPath
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
      var c2 = fs.readFileSync(path.join(rootPath, 'index.html'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('png', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/index.png',
      rootPath: rootPath
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.png'));
      var c2 = fs.readFileSync(path.join(rootPath, 'index.png'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('gif', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/index.gif',
      rootPath: rootPath
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.gif'));
      var c2 = fs.readFileSync(path.join(rootPath, 'index.gif'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('zip', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/index.zip',
      rootPath: rootPath
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.zip'));
      var c2 = fs.readFileSync(path.join(rootPath, 'index.zip'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('deep path', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/index.html',
      rootPath: path.join(rootPath, '1/2')
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
      var c2 = fs.readFileSync(path.join(path.join(rootPath, '1/2'), 'index.html'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('ignore', function(done) {
    request.download({
      url: 'http://127.0.0.1:3100/Capital/index.html',
      ignore: true,
      rootPath: rootPath
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'Capital/index.html'));
      var c2 = fs.readFileSync(path.join(rootPath, 'capital/index.html'));

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('destPath string', function(done) {
    var dest = path.join(rootPath, 'dest/index.html');
    request.download({
      url: 'http://127.0.0.1:3100/index.html',
      destPath: dest
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
      var c2 = fs.readFileSync(dest);

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });

  it('destPath function', function(done) {
    var dest = path.join(rootPath, 'dest/index.html');
    request.download({
      url: 'http://127.0.0.1:3100/index.html',
      destPath: function(filename) {
        assert.equal(filename, 'index.html');
        return dest;
      }
    }, function(err, res, body, dest) {
      var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
      var c2 = fs.readFileSync(dest);

      assert.equal(c1.length, c2.length);
      assert.equal(c1.toString(), c2.toString());
      done();
    });
  });



  afterEach(function(){
    server.close();
    fs.rmdirSync(rootPath);
  });
});