cheerio-download.js
12 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*eslint-env mocha*/
/*eslint no-invalid-this:0, max-nested-callbacks:[1, 6]*/
/*jshint -W100*/
var assert = require('power-assert');
var helper = require('./_helper');
var cli = require('../index');
var isSteram = require('isstream');
var devNull = require('dev-null');
describe('cheerio:download', function () {
beforeEach(function () {
cli.download.state = { queue: 0, complete: 0, error: 0 };
cli.download.removeAllListeners();
cli.download.clearCache();
cli.download.on('ready', function () {});
});
afterEach(function () {
cli.set('timeout', 30000);
});
it('img要素以外 => エラー', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
try {
$('body').download();
throw new Error('not thrown');
} catch (e) {
assert(e.message === 'element is not img');
}
done();
});
});
describe('同時実行数指定', function () {
after(function () {
cli.download.parallel = 3;
});
it('1未満', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
try {
cli.download.parallel = 0;
$('.rel').download();
throw new Error('not thrown');
} catch (e) {
assert(e.message === 'valid download parallel range is 1 and 5');
}
done();
});
});
it('6以上', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
try {
cli.download.parallel = 6;
$('.rel').download();
throw new Error('not thrown');
} catch (e) {
assert(e.message === 'valid download parallel range is 1 and 5');
}
done();
});
});
});
describe('URL登録', function () {
it('二重登録不可', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
$('.rel').download();
setTimeout(function () {
$('.rel').download();
setTimeout(function () {
assert.deepEqual(cli.download.state, { queue: 0, complete: 1, error: 0 });
done();
}, 100);
}, 100);
});
});
it('clearCahce()後は再度登録可能', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
$('.rel').download();
setTimeout(function () {
cli.download.clearCache();
$('.rel').download();
setTimeout(function () {
assert.deepEqual(cli.download.state, { queue: 0, complete: 2, error: 0 });
done();
}, 100);
}, 100);
});
});
});
it('画像のStreamがreadyイベントに送られる', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
var img = helper.url('img') + '/img/cat.png';
assert(stream.url.href === img);
assert(stream.type === 'image/png');
assert(stream.length === 15572);
assert(isSteram(stream));
stream.end();
done();
})
.on('error', function (e) {
throw e;
});
assert($('.rel').download() === 1);
});
});
describe('stream.toBuffer()', function () {
it('streamがBuffer化される', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
var img = helper.url('img') + '/img/cat.png';
assert(stream.url.href === img);
assert(stream.type === 'image/png');
assert(stream.length === 15572);
stream.toBuffer((function (err, buffer) {
var expected = helper.readBuffer('fixtures/img/img/cat.png');
assert.deepEqual(buffer, expected);
assert.deepEqual(this.state, { queue: 0, complete: 1, error: 0 });
done();
}).bind(this));
})
.on('error', function (e) {
throw e;
});
assert($('.rel').download() === 1);
});
});
it('stream使用後に実行 => 例外', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
stream.pipe(devNull());
try {
stream.toBuffer(function (err, buffer) {
throw new Error('not thrown');
});
} catch (e) {
assert(e.message === 'stream has already been read');
}
done();
})
.on('error', function (e) {
throw e;
});
assert($('.rel').download() === 1);
});
});
it('コールバック未設定 => 例外', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
stream.pipe(devNull());
try {
return stream.toBuffer();
} catch (e) {
assert(e.message === 'callback is not function');
return done();
}
})
.on('error', function (e) {
throw e;
});
assert($('.rel').download() === 1);
});
});
});
it('Base64エンコードされた画像 => Buffer化される', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
assert(isSteram(stream));
stream.toBuffer((function (err, buffer) {
var expected = helper.readBuffer('fixtures/img/img/sports.jpg');
assert(stream.url === 'base64');
assert(stream.type === 'image/jpg');
assert(stream.length === 2268);
assert.deepEqual(buffer, expected);
assert.deepEqual(this.state, { queue: 0, complete: 1, error: 0 });
done();
}).bind(this));
})
.on('error', function (e) {
throw e;
});
assert($('.base64').download() === 1);
});
});
it('画像要素なし => ダウンロードキューに登録されない', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
assert($('.xxxxyyyyzzzz').download() === 0);
done();
});
});
it('javascript => ダウンロードキューに登録されない', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
assert($('.js').download() === 0);
done();
});
});
it('404 => errorイベントに送られる', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
stream.end();
throw new Error('not thrown');
})
.on('error', function (e) {
var img = helper.url('img') + '/not-found.gif';
assert(e.url === img);
assert(e.message === 'server status');
assert.deepEqual(this.state, { queue: 0, complete: 0, error: 1 });
done();
});
assert($('.err404').download() === 1);
});
});
it('リクエストタイムアウト => errorイベントに送られる', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
stream.end();
throw new Error('not thrown');
})
.on('error', function (e) {
var img = helper.url('img') + '/img/cat.png';
assert(e.url === img);
assert(helper.isTimedOut(e));
assert.deepEqual(this.state, { queue: 0, complete: 0, error: 1 });
done();
});
cli.set('timeout', 1);
assert($('.rel').download() === 1);
});
});
it('streamを使用しないままタイムアウト時間が過ぎるとエラー', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
cli.download
.on('ready', function (stream) {
})
.on('error', function (e) {
var img = helper.url('img') + '/~mega';
assert(e.url === img);
assert(e.message === 'stream timeout (maybe stream is not used)');
assert.deepEqual(this.state, { queue: 0, complete: 0, error: 1 });
done();
});
cli.set('timeout', 3000);
assert($('.mega').download() === 1);
});
});
it('ダウンロードが完了するまではqueueにカウントされている', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
var expected = [
{ queue: 1, complete: 1, error: 0 },
{ queue: 0, complete: 2, error: 0 }
];
var eIdx = 0;
cli.download
.on('ready', function (stream) {
stream.toBuffer((function (err, buffer) {
var st = expected[eIdx++];
assert.deepEqual(this.state, st);
if (eIdx === expected.length) {
return done();
}
/*eslint-disable consistent-return*/ return; /*eslint-enable consistent-return*/
}).bind(this));
})
.on('error', function (e) {
throw e;
});
$('.root').attr('src', $('.root').attr('src') + '&wait=1000');
assert($('.root').download() === 1);
assert($('.rel').download() === 1);
});
});
describe('srcAttrs', function () {
it('無指定 => デフォルトの優先順で属性を検索してダウンロード', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
var expected = {
url: helper.url('img') + '/img/cat.png',
buffer: helper.readBuffer('fixtures/img/img/cat.png')
};
cli.download
.on('ready', function (stream) {
stream.toBuffer(function (err, buffer) {
var actual = { url: stream.url.href, buffer: buffer };
assert.deepEqual(actual, expected);
done();
});
})
.on('error', function (e) {
throw e;
});
assert($('.lazy1').download() === 1);
});
});
it('文字列 => 指定した文字列属性をsrcよりも優先してダウンロード', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
var expected = {
url: helper.url('img') + '/img/sports.jpg',
buffer: helper.readBuffer('fixtures/img/img/sports.jpg')
};
cli.download
.on('ready', function (stream) {
assert(stream.type === 'image/jpeg');
assert(stream.length === 2268);
stream.toBuffer(function (err, buffer) {
var actual = { url: stream.url.href, buffer: buffer };
assert.deepEqual(actual, expected);
done();
});
})
.on('error', function (e) {
throw e;
});
assert($('.lazy3').download('data-original-src') === 1);
});
});
it('空配列 => srcのURLをダウンロード', function (done) {
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
var expected = {
url: helper.url('img') + '/img/1x1.gif',
buffer: helper.readBuffer('fixtures/img/img/1x1.gif')
};
cli.download
.on('ready', function (stream) {
assert(stream.type === 'image/gif');
assert(stream.length === 37);
stream.toBuffer(function (err, buffer) {
var actual = { url: stream.url.href, buffer: buffer };
assert.deepEqual(actual, expected);
done();
});
})
.on('error', function (e) {
throw e;
});
assert($('.lazy2').download([]) === 1);
});
});
});
it('ダウンロードマネージャー未設定', function (done) {
cli.download.removeAllListeners('ready');
cli.fetch(helper.url('img', 'index'), function (err, $, res, body) {
try {
$('.rel').download();
throw new Error('not thrown');
} catch (e) {
assert(e.message === 'download manager configured no event');
}
done();
});
});
});