1 | var assert = require('assert');
|
2 | var app = require('./service/app');
|
3 | var util = require('utils-extend');
|
4 | var request = require('../index');
|
5 | var fs = require('file-system');
|
6 | var path = require('path');
|
7 | var srcPath = path.join(__dirname, 'service/assets');
|
8 | var rootPath = path.join(__dirname, 'download');
|
9 | var server;
|
10 |
|
11 | describe('Download request', function() {
|
12 |
|
13 | beforeEach(function(done){
|
14 | server = app.listen(3100, function() {
|
15 | done();
|
16 | });
|
17 | });
|
18 |
|
19 | it('html', function(done) {
|
20 | request.download({
|
21 | url: 'http://127.0.0.1:3100/index.html',
|
22 | rootPath: rootPath
|
23 | }, function(err, res, body, dest) {
|
24 | var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
|
25 | var c2 = fs.readFileSync(path.join(rootPath, 'index.html'));
|
26 |
|
27 | assert.equal(c1.length, c2.length);
|
28 | assert.equal(c1.toString(), c2.toString());
|
29 | done();
|
30 | });
|
31 | });
|
32 |
|
33 | it('png', function(done) {
|
34 | request.download({
|
35 | url: 'http://127.0.0.1:3100/index.png',
|
36 | rootPath: rootPath
|
37 | }, function(err, res, body, dest) {
|
38 | var c1 = fs.readFileSync(path.join(srcPath, 'index.png'));
|
39 | var c2 = fs.readFileSync(path.join(rootPath, 'index.png'));
|
40 |
|
41 | assert.equal(c1.length, c2.length);
|
42 | assert.equal(c1.toString(), c2.toString());
|
43 | done();
|
44 | });
|
45 | });
|
46 |
|
47 | it('gif', function(done) {
|
48 | request.download({
|
49 | url: 'http://127.0.0.1:3100/index.gif',
|
50 | rootPath: rootPath
|
51 | }, function(err, res, body, dest) {
|
52 | var c1 = fs.readFileSync(path.join(srcPath, 'index.gif'));
|
53 | var c2 = fs.readFileSync(path.join(rootPath, 'index.gif'));
|
54 |
|
55 | assert.equal(c1.length, c2.length);
|
56 | assert.equal(c1.toString(), c2.toString());
|
57 | done();
|
58 | });
|
59 | });
|
60 |
|
61 | it('zip', function(done) {
|
62 | request.download({
|
63 | url: 'http://127.0.0.1:3100/index.zip',
|
64 | rootPath: rootPath
|
65 | }, function(err, res, body, dest) {
|
66 | var c1 = fs.readFileSync(path.join(srcPath, 'index.zip'));
|
67 | var c2 = fs.readFileSync(path.join(rootPath, 'index.zip'));
|
68 |
|
69 | assert.equal(c1.length, c2.length);
|
70 | assert.equal(c1.toString(), c2.toString());
|
71 | done();
|
72 | });
|
73 | });
|
74 |
|
75 | it('deep path', function(done) {
|
76 | request.download({
|
77 | url: 'http://127.0.0.1:3100/index.html',
|
78 | rootPath: path.join(rootPath, '1/2')
|
79 | }, function(err, res, body, dest) {
|
80 | var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
|
81 | var c2 = fs.readFileSync(path.join(path.join(rootPath, '1/2'), 'index.html'));
|
82 |
|
83 | assert.equal(c1.length, c2.length);
|
84 | assert.equal(c1.toString(), c2.toString());
|
85 | done();
|
86 | });
|
87 | });
|
88 |
|
89 | it('ignore', function(done) {
|
90 | request.download({
|
91 | url: 'http://127.0.0.1:3100/Capital/index.html',
|
92 | ignore: true,
|
93 | rootPath: rootPath
|
94 | }, function(err, res, body, dest) {
|
95 | var c1 = fs.readFileSync(path.join(srcPath, 'Capital/index.html'));
|
96 | var c2 = fs.readFileSync(path.join(rootPath, 'capital/index.html'));
|
97 |
|
98 | assert.equal(c1.length, c2.length);
|
99 | assert.equal(c1.toString(), c2.toString());
|
100 | done();
|
101 | });
|
102 | });
|
103 |
|
104 | it('destPath string', function(done) {
|
105 | var dest = path.join(rootPath, 'dest/index.html');
|
106 | request.download({
|
107 | url: 'http://127.0.0.1:3100/index.html',
|
108 | destPath: dest
|
109 | }, function(err, res, body, dest) {
|
110 | var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
|
111 | var c2 = fs.readFileSync(dest);
|
112 |
|
113 | assert.equal(c1.length, c2.length);
|
114 | assert.equal(c1.toString(), c2.toString());
|
115 | done();
|
116 | });
|
117 | });
|
118 |
|
119 | it('destPath function', function(done) {
|
120 | var dest = path.join(rootPath, 'dest/index.html');
|
121 | request.download({
|
122 | url: 'http://127.0.0.1:3100/index.html',
|
123 | destPath: function(filename) {
|
124 | assert.equal(filename, 'index.html');
|
125 | return dest;
|
126 | }
|
127 | }, function(err, res, body, dest) {
|
128 | var c1 = fs.readFileSync(path.join(srcPath, 'index.html'));
|
129 | var c2 = fs.readFileSync(dest);
|
130 |
|
131 | assert.equal(c1.length, c2.length);
|
132 | assert.equal(c1.toString(), c2.toString());
|
133 | done();
|
134 | });
|
135 | });
|
136 |
|
137 | afterEach(function(){
|
138 | server.close();
|
139 | fs.rmdirSync(rootPath);
|
140 | });
|
141 | }); |
\ | No newline at end of file |