| 1 |
import fs from 'fs'; |
| 2 |
import path from 'path'; |
| 3 |
import zlib from 'zlib'; |
| 4 |
import assert from 'assert'; |
| 5 |
import { Transform } from 'stream'; |
| 6 |
import { IncomingMessage, ClientRequest, RequestOptions } from 'http'; |
| 7 |
|
| 8 |
import miniget from '../dist'; |
| 9 |
|
| 10 |
import nock from 'nock'; |
| 11 |
import sinon from 'sinon'; |
| 12 |
import streamEqual from 'stream-equal'; |
| 13 |
import 'longjohn'; |
| 14 |
|
| 15 |
nock.disableNetConnect(); |
| 16 |
|
| 17 |
describe('Make a request', () => { |
| 18 |
afterEach(() => nock.cleanAll()); |
| 19 |
let clock: sinon.SinonFakeTimers; |
| 20 |
beforeEach(() => clock = sinon.useFakeTimers()); |
| 21 |
afterEach(() => clock.uninstall()); |
| 22 |
|
| 23 |
const stub = sinon.stub(console, 'warn'); |
| 24 |
after(() => stub.restore()); |
| 25 |
|
| 26 |
describe('with `.text()`', () => { |
| 27 |
it('Gives entire contents of page', async() => { |
| 28 |
const scope = nock('http://webby.com') |
| 29 |
.get('/pathos') |
| 30 |
.replyWithFile(200, __filename); |
| 31 |
let body = await miniget('http://webby.com/pathos').text(); |
| 32 |
scope.done(); |
| 33 |
assert.ok(body.length > 100); |
| 34 |
}); |
| 35 |
|
| 36 |
describe('that errors', () => { |
| 37 |
it('error is caught', async() => { |
| 38 |
const scope = nock('http://something.com') |
| 39 |
.get('/one/two/three') |
| 40 |
.replyWithError('NONONO'); |
| 41 |
await assert.rejects( |
| 42 |
miniget('http://something.com/one/two/three', { maxRetries: 0 }).text(), |
| 43 |
{ message: 'NONONO' }, |
| 44 |
); |
| 45 |
scope.done(); |
| 46 |
}); |
| 47 |
}); |
| 48 |
}); |
| 49 |
|
| 50 |
describe('to a working site', () => { |
| 51 |
it('Returns a stream', done => { |
| 52 |
const scope = nock('http://website.com') |
| 53 |
.get('/path') |
| 54 |
.replyWithFile(200, __filename); |
| 55 |
const stream = miniget('http://website.com/path'); |
| 56 |
stream.on('error', done); |
| 57 |
stream.on('end', () => { |
| 58 |
scope.done(); |
| 59 |
done(); |
| 60 |
}); |
| 61 |
stream.resume(); |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
describe('with options', () => { |
| 66 |
it('Makes request with options', async() => { |
| 67 |
const scope = nock('http://website.com', { |
| 68 |
reqheaders: { 'User-Agent': 'miniget' }, |
| 69 |
}) |
| 70 |
.get('/path') |
| 71 |
.replyWithFile(200, __filename); |
| 72 |
let body = await miniget('http://website.com/path', { |
| 73 |
headers: { 'User-Agent': 'miniget' }, |
| 74 |
}).text(); |
| 75 |
scope.done(); |
| 76 |
assert.ok(body.length > 100); |
| 77 |
}); |
| 78 |
}); |
| 79 |
|
| 80 |
describe('with bad path', () => { |
| 81 |
it('Emits error', done => { |
| 82 |
const scope = nock('https://mysite.com') |
| 83 |
.get('/badpath') |
| 84 |
.reply(404, 'not exists'); |
| 85 |
let stream = miniget('https://mysite.com/badpath'); |
| 86 |
stream.on('error', err => { |
| 87 |
scope.done(); |
| 88 |
assert.ok(err); |
| 89 |
done(); |
| 90 |
}); |
| 91 |
}); |
| 92 |
}); |
| 93 |
|
| 94 |
describe('that errors', () => { |
| 95 |
it('Emits error event', done => { |
| 96 |
const scope = nock('https://mysite.com') |
| 97 |
.get('/path') |
| 98 |
.replyWithError('ENOTFOUND') |
| 99 |
.get('/path') |
| 100 |
.replyWithError('ENOTFOUND') |
| 101 |
.get('/path') |
| 102 |
.reply(500, 'oh no 3'); |
| 103 |
const stream = miniget('https://mysite.com/path'); |
| 104 |
stream.on('retry', retryCount => { |
| 105 |
process.nextTick(() => { |
| 106 |
clock.tick(retryCount * 100); |
| 107 |
}); |
| 108 |
}); |
| 109 |
stream.on('error', err => { |
| 110 |
scope.done(); |
| 111 |
assert.equal(err.message, 'Status code: 500'); |
| 112 |
assert.equal(err.statusCode, 500); |
| 113 |
done(); |
| 114 |
}); |
| 115 |
}); |
| 116 |
|
| 117 |
describe('without retries', () => { |
| 118 |
it('Emits error event', done => { |
| 119 |
const scope = nock('https://mysite.com') |
| 120 |
.get('/path') |
| 121 |
.replyWithError('oh no 1'); |
| 122 |
const stream = miniget('https://mysite.com/path', { maxRetries: 0 }); |
| 123 |
stream.on('error', err => { |
| 124 |
assert.equal(err.message, 'oh no 1'); |
| 125 |
scope.done(); |
| 126 |
done(); |
| 127 |
}); |
| 128 |
}); |
| 129 |
}); |
| 130 |
}); |
| 131 |
|
| 132 |
describe('using https protocol', () => { |
| 133 |
it('Uses the https module', done => { |
| 134 |
const scope = nock('https://secureplace.net') |
| 135 |
.get('/') |
| 136 |
.reply(200); |
| 137 |
const stream = miniget('https://secureplace.net'); |
| 138 |
stream.on('error', done); |
| 139 |
stream.on('end', () => { |
| 140 |
scope.done(); |
| 141 |
done(); |
| 142 |
}); |
| 143 |
stream.resume(); |
| 144 |
}); |
| 145 |
}); |
| 146 |
|
| 147 |
describe('with auth', () => { |
| 148 |
it('Passes auth to request', async() => { |
| 149 |
const scope = nock('https://lockbox.com') |
| 150 |
.get('/vault') |
| 151 |
.basicAuth({ user: 'john', pass: 'pass' }) |
| 152 |
.reply(200); |
| 153 |
await miniget('https://john:[email protected]/vault'); |
| 154 |
scope.done(); |
| 155 |
}); |
| 156 |
}); |
| 157 |
|
| 158 |
describe('with URL object passed', () => { |
| 159 |
it('Creates request and gets correct response', async() => { |
| 160 |
const scope = nock('http://webby.com') |
| 161 |
.get('/pathos') |
| 162 |
.replyWithFile(200, __filename); |
| 163 |
let body = await miniget(new URL('http://webby.com/pathos')).text(); |
| 164 |
scope.done(); |
| 165 |
assert.ok(body.length > 100); |
| 166 |
}); |
| 167 |
}); |
| 168 |
|
| 169 |
describe('with an incorrect URL', () => { |
| 170 |
it('Emits error', done => { |
| 171 |
miniget('file:///path/to/file/').on('error', err => { |
| 172 |
assert.ok(err); |
| 173 |
assert.equal(err.message, 'Invalid URL: file:///path/to/file/'); |
| 174 |
done(); |
| 175 |
}); |
| 176 |
}); |
| 177 |
}); |
| 178 |
|
| 179 |
describe('with no URL', () => { |
| 180 |
it('Emits error', done => { |
| 181 |
miniget('undefined').on('error', err => { |
| 182 |
assert.ok(err); |
| 183 |
assert.equal(err.message, 'Invalid URL: undefined'); |
| 184 |
done(); |
| 185 |
}); |
| 186 |
}); |
| 187 |
}); |
| 188 |
|
| 189 |
describe('that redirects', () => { |
| 190 |
it('Should download file after redirect', done => { |
| 191 |
const scope = nock('http://mysite.com') |
| 192 |
.get('/pathy') |
| 193 |
.reply(302, '', { Location: 'http://mysite.com/redirected!' }) |
| 194 |
.get('/redirected!') |
| 195 |
.reply(200, 'Helloo!'); |
| 196 |
const stream = miniget('http://mysite.com/pathy'); |
| 197 |
stream.on('error', done); |
| 198 |
stream.on('data', body => { |
| 199 |
scope.done(); |
| 200 |
assert.equal(body, 'Helloo!'); |
| 201 |
done(); |
| 202 |
}); |
| 203 |
stream.on('redirect', () => { |
| 204 |
clock.tick(1); |
| 205 |
}); |
| 206 |
}); |
| 207 |
|
| 208 |
describe('too many times', () => { |
| 209 |
it('Emits error after 3 retries', done => { |
| 210 |
const scope = nock('http://yoursite.com') |
| 211 |
.get('/first-request') |
| 212 |
.reply(302, '', { Location: 'http://yoursite.com/redirect-1' }) |
| 213 |
.get('/redirect-1') |
| 214 |
.reply(302, '', { Location: 'http://yoursite.com/redirect-2' }) |
| 215 |
.get('/redirect-2') |
| 216 |
.reply(302, '', { Location: 'http://yoursite.com/redirect-3' }); |
| 217 |
const stream = miniget('http://yoursite.com/first-request', { |
| 218 |
maxRedirects: 2, |
| 219 |
}); |
| 220 |
stream.on('error', err => { |
| 221 |
assert.ok(err); |
| 222 |
scope.done(); |
| 223 |
assert.equal(err.message, 'Too many redirects'); |
| 224 |
done(); |
| 225 |
}); |
| 226 |
stream.on('redirect', () => { |
| 227 |
clock.tick(1); |
| 228 |
}); |
| 229 |
}); |
| 230 |
}); |
| 231 |
|
| 232 |
describe('with `retry-after` header', () => { |
| 233 |
it('Redirects after given time', done => { |
| 234 |
const scope = nock('http://mysite2.com') |
| 235 |
.get('/pathos/to/resource') |
| 236 |
.reply(301, '', { |
| 237 |
Location: 'http://mysite2.com/newpath/to/source', |
| 238 |
'Retry-After': '300', |
| 239 |
}) |
| 240 |
.get('/newpath/to/source') |
| 241 |
.reply(200, 'hi world!!'); |
| 242 |
const stream = miniget('http://mysite2.com/pathos/to/resource'); |
| 243 |
stream.on('error', done); |
| 244 |
stream.on('data', body => { |
| 245 |
scope.done(); |
| 246 |
assert.equal(body, 'hi world!!'); |
| 247 |
done(); |
| 248 |
}); |
| 249 |
stream.on('redirect', () => { |
| 250 |
clock.tick(300 * 1000); |
| 251 |
}); |
| 252 |
}); |
| 253 |
}); |
| 254 |
|
| 255 |
describe('without `location` header', () => { |
| 256 |
it('Throws an error', done => { |
| 257 |
const scope = nock('http://mysite.com') |
| 258 |
.get('/pathy') |
| 259 |
.reply(302, '', {}); |
| 260 |
const stream = miniget('http://mysite.com/pathy'); |
| 261 |
stream.on('error', err => { |
| 262 |
scope.done(); |
| 263 |
assert.equal(err.message, 'Redirect status code given with no location'); |
| 264 |
done(); |
| 265 |
}); |
| 266 |
}); |
| 267 |
}); |
| 268 |
}); |
| 269 |
|
| 270 |
describe('that gets rate limited', () => { |
| 271 |
it('Retries the request after some time', done => { |
| 272 |
const scope = nock('https://mysite.io') |
| 273 |
.get('/api/v1/data') |
| 274 |
.reply(429, 'slow down') |
| 275 |
.get('/api/v1/data') |
| 276 |
.reply(200, 'where are u'); |
| 277 |
const stream = miniget('https://mysite.io/api/v1/data'); |
| 278 |
stream.on('error', done); |
| 279 |
stream.on('data', data => { |
| 280 |
scope.done(); |
| 281 |
assert.equal(data, 'where are u'); |
| 282 |
done(); |
| 283 |
}); |
| 284 |
stream.on('retry', () => { |
| 285 |
clock.tick(1000); |
| 286 |
}); |
| 287 |
}); |
| 288 |
it('Emits error after multiple tries', done => { |
| 289 |
const scope = nock('https://mysite.io') |
| 290 |
.get('/api/v1/data') |
| 291 |
.reply(429, 'too many requests') |
| 292 |
.get('/api/v1/data') |
| 293 |
.reply(429, 'too many requests') |
| 294 |
.get('/api/v1/data') |
| 295 |
.reply(429, 'too many requests'); |
| 296 |
|
| 297 |
const stream = miniget('https://mysite.io/api/v1/data'); |
| 298 |
stream.on('error', err => { |
| 299 |
assert.ok(err); |
| 300 |
scope.done(); |
| 301 |
assert.equal(err.message, 'Status code: 429'); |
| 302 |
assert.equal(err.statusCode, 429); |
| 303 |
done(); |
| 304 |
}); |
| 305 |
stream.on('retry', () => { |
| 306 |
clock.tick(1000); |
| 307 |
}); |
| 308 |
}); |
| 309 |
describe('with `retry-after` header', () => { |
| 310 |
it('Retries after given time', done => { |
| 311 |
const scope = nock('https://mysite.io') |
| 312 |
.get('/api/v1/dota') |
| 313 |
.reply(429, 'slow down', { 'Retry-After': '3600' }) |
| 314 |
.get('/api/v1/dota') |
| 315 |
.reply(200, 'where are u'); |
| 316 |
const stream = miniget('https://mysite.io/api/v1/dota'); |
| 317 |
stream.on('error', done); |
| 318 |
stream.on('data', data => { |
| 319 |
scope.done(); |
| 320 |
assert.equal(data, 'where are u'); |
| 321 |
done(); |
| 322 |
}); |
| 323 |
stream.on('retry', () => { |
| 324 |
// Test that ticking by a bit does not retry the request. |
| 325 |
clock.tick(1000); |
| 326 |
assert.ok(!scope.isDone()); |
| 327 |
clock.tick(3600 * 1000); |
| 328 |
}); |
| 329 |
}); |
| 330 |
}); |
| 331 |
}); |
| 332 |
|
| 333 |
describe('using the `transform` option', () => { |
| 334 |
it('Calls `transform` function and customizes request', async() => { |
| 335 |
const scope = nock('http://other.com') |
| 336 |
.get('/http://supplies.com/boxes') |
| 337 |
.reply(200, '[ ]'); |
| 338 |
let transformCalled = false; |
| 339 |
let body = await miniget('http://supplies.com/boxes', { |
| 340 |
transform: parsed => { |
| 341 |
transformCalled = true; |
| 342 |
return { |
| 343 |
host: 'other.com', |
| 344 |
path: `/${parsed.protocol}//${parsed.host}${parsed.path}`, |
| 345 |
}; |
| 346 |
}, |
| 347 |
}).text(); |
| 348 |
assert.ok(transformCalled); |
| 349 |
scope.done(); |
| 350 |
assert.equal(body, '[ ]'); |
| 351 |
}); |
| 352 |
it('Calls `transform` function and customizes request with protocol changing', async() => { |
| 353 |
const scope = nock('http://that.com') |
| 354 |
.get('/') |
| 355 |
.reply(200, '[ ]'); |
| 356 |
let body = await miniget('https://that.com', { |
| 357 |
transform: parsed => { |
| 358 |
parsed.protocol = 'http:'; |
| 359 |
return parsed; |
| 360 |
}, |
| 361 |
}).text(); |
| 362 |
scope.done(); |
| 363 |
assert.equal(body, '[ ]'); |
| 364 |
}); |
| 365 |
describe('with bad URL', () => { |
| 366 |
it('Catches error', done => { |
| 367 |
let stream = miniget('http://supplies.com/boxes', { |
| 368 |
transform: parsed => { |
| 369 |
parsed.protocol = 'file'; |
| 370 |
return parsed; |
| 371 |
}, |
| 372 |
}); |
| 373 |
stream.on('error', err => { |
| 374 |
assert.equal(err.message, 'Invalid URL object from `transform` function'); |
| 375 |
done(); |
| 376 |
}); |
| 377 |
}); |
| 378 |
}); |
| 379 |
describe('with no object returned', () => { |
| 380 |
it('Catches error', done => { |
| 381 |
let stream = miniget('http://supplies.com/boxes', { |
| 382 |
transform: (_: RequestOptions) => undefined as unknown as RequestOptions, |
| 383 |
}); |
| 384 |
stream.on('error', err => { |
| 385 |
assert.equal(err.message, 'Invalid URL object from `transform` function'); |
| 386 |
done(); |
| 387 |
}); |
| 388 |
}); |
| 389 |
}); |
| 390 |
describe('that throws', () => { |
| 391 |
it('Catches error', done => { |
| 392 |
let stream = miniget('http://kanto.com', { |
| 393 |
transform: () => { throw Error('hello'); }, |
| 394 |
}); |
| 395 |
stream.on('error', err => { |
| 396 |
assert.equal(err.message, 'hello'); |
| 397 |
done(); |
| 398 |
}); |
| 399 |
}); |
| 400 |
}); |
| 401 |
}); |
| 402 |
|
| 403 |
describe('that disconnects before end', () => { |
| 404 |
const file = path.resolve(__dirname, 'video.flv'); |
| 405 |
let filesize: number; |
| 406 |
before(done => { |
| 407 |
fs.stat(file, (err, stat) => { |
| 408 |
assert.ifError(err); |
| 409 |
filesize = stat.size; |
| 410 |
done(); |
| 411 |
}); |
| 412 |
}); |
| 413 |
|
| 414 |
const destroy = (req: ClientRequest, res: IncomingMessage): void => { |
| 415 |
req.destroy(); |
| 416 |
res.unpipe(); |
| 417 |
}; |
| 418 |
|
| 419 |
it('Still downloads entire file', done => { |
| 420 |
const scope = nock('http://mysite.com') |
| 421 |
.get('/myfile') |
| 422 |
.replyWithFile(200, file, { |
| 423 |
'content-length': `${filesize}`, |
| 424 |
'accept-ranges': 'bytes', |
| 425 |
}); |
| 426 |
const stream = miniget('http://mysite.com/myfile', { maxReconnects: 1 }); |
| 427 |
let req: ClientRequest, res: IncomingMessage; |
| 428 |
stream.on('request', a => { req = a; }); |
| 429 |
stream.on('response', a => { res = a; }); |
| 430 |
let reconnects = 0; |
| 431 |
stream.on('reconnect', () => { |
| 432 |
reconnects++; |
| 433 |
clock.tick(100); |
| 434 |
}); |
| 435 |
let downloaded = 0, destroyed = false; |
| 436 |
stream.on('data', chunk => { |
| 437 |
downloaded += chunk.length; |
| 438 |
if (!destroyed && downloaded / filesize >= 0.3) { |
| 439 |
destroyed = true; |
| 440 |
scope.get('/myfile') |
| 441 |
.reply(206, () => fs.createReadStream(file, { start: downloaded }), { |
| 442 |
'content-range': `bytes ${downloaded}-${filesize}/${filesize}`, |
| 443 |
'content-length': `${filesize - downloaded}`, |
| 444 |
'accept-ranges': 'bytes', |
| 445 |
}); |
| 446 |
destroy(req, res); |
| 447 |
} |
| 448 |
}); |
| 449 |
stream.on('error', done); |
| 450 |
stream.on('end', () => { |
| 451 |
scope.done(); |
| 452 |
assert.ok(destroyed); |
| 453 |
assert.equal(reconnects, 1); |
| 454 |
assert.equal(downloaded, filesize); |
| 455 |
done(); |
| 456 |
}); |
| 457 |
}); |
| 458 |
|
| 459 |
describe('without an error', () => { |
| 460 |
it('Still downloads entire file', done => { |
| 461 |
const scope = nock('http://mysite.com') |
| 462 |
.get('/myfile') |
| 463 |
.replyWithFile(200, file, { |
| 464 |
'content-length': `${filesize}`, |
| 465 |
'accept-ranges': 'bytes', |
| 466 |
}); |
| 467 |
const stream = miniget('http://mysite.com/myfile', { maxReconnects: 1 }); |
| 468 |
let res: IncomingMessage; |
| 469 |
stream.on('response', a => { res = a; }); |
| 470 |
let reconnects = 0; |
| 471 |
stream.on('reconnect', () => { |
| 472 |
reconnects++; |
| 473 |
clock.tick(100); |
| 474 |
}); |
| 475 |
let downloaded = 0, destroyed = false; |
| 476 |
stream.on('data', chunk => { |
| 477 |
downloaded += chunk.length; |
| 478 |
if (!destroyed && downloaded / filesize >= 0.3) { |
| 479 |
destroyed = true; |
| 480 |
scope.get('/myfile') |
| 481 |
.reply(206, () => fs.createReadStream(file, { start: downloaded }), { |
| 482 |
'content-range': `bytes ${downloaded}-${filesize}/${filesize}`, |
| 483 |
'content-length': `${filesize - downloaded}`, |
| 484 |
'accept-ranges': 'bytes', |
| 485 |
}); |
| 486 |
res.emit('end'); |
| 487 |
} |
| 488 |
}); |
| 489 |
stream.on('error', done); |
| 490 |
stream.on('end', () => { |
| 491 |
scope.done(); |
| 492 |
assert.ok(destroyed); |
| 493 |
assert.equal(reconnects, 1); |
| 494 |
assert.equal(downloaded, filesize); |
| 495 |
done(); |
| 496 |
}); |
| 497 |
}); |
| 498 |
|
| 499 |
describe('without enough reconnects', () => { |
| 500 |
it('Downloads partial file', done => { |
| 501 |
const scope = nock('http://mysite.com') |
| 502 |
.get('/yourfile') |
| 503 |
.replyWithFile(200, file, { |
| 504 |
'content-length': `${filesize}`, |
| 505 |
'accept-ranges': 'bytes', |
| 506 |
}); |
| 507 |
const stream = miniget('http://mysite.com/yourfile', { |
| 508 |
maxReconnects: 1, |
| 509 |
maxRetries: 0, |
| 510 |
}); |
| 511 |
let res: IncomingMessage; |
| 512 |
stream.on('response', a => { |
| 513 |
res = a; |
| 514 |
}); |
| 515 |
let reconnects = 0; |
| 516 |
stream.on('reconnect', () => { |
| 517 |
reconnects++; |
| 518 |
scope.get('/yourfile') |
| 519 |
.reply(206, fs.createReadStream(file, { start: downloaded }), { |
| 520 |
'content-range': `bytes ${downloaded}-${filesize}/${filesize}`, |
| 521 |
'content-length': `${filesize - downloaded}`, |
| 522 |
'accept-ranges': 'bytes', |
| 523 |
}); |
| 524 |
clock.tick(100); |
| 525 |
}); |
| 526 |
let downloaded = 0, destroyed = false; |
| 527 |
stream.on('data', chunk => { |
| 528 |
downloaded += chunk.length; |
| 529 |
if (downloaded / filesize >= 0.3) { |
| 530 |
destroyed = true; |
| 531 |
res.emit('end'); |
| 532 |
} |
| 533 |
}); |
| 534 |
stream.on('error', done); |
| 535 |
stream.on('end', () => { |
| 536 |
scope.done(); |
| 537 |
assert.ok(destroyed); |
| 538 |
assert.equal(reconnects, 1); |
| 539 |
assert.notEqual(downloaded, filesize); |
| 540 |
done(); |
| 541 |
}); |
| 542 |
}); |
| 543 |
}); |
| 544 |
}); |
| 545 |
|
| 546 |
describe('too many times', () => { |
| 547 |
it('Emits error', done => { |
| 548 |
const scope = nock('http://mysite.com') |
| 549 |
.get('/myfile') |
| 550 |
.replyWithFile(200, file, { |
| 551 |
'content-length': `${filesize}`, |
| 552 |
'accept-ranges': 'bytes', |
| 553 |
}); |
| 554 |
const stream = miniget('http://mysite.com/myfile', { |
| 555 |
maxReconnects: 2, |
| 556 |
headers: { Range: 'bad' }, |
| 557 |
}); |
| 558 |
let req: ClientRequest, res: IncomingMessage; |
| 559 |
stream.on('request', a => { req = a; }); |
| 560 |
stream.on('response', a => { res = a; }); |
| 561 |
let reconnects = 0; |
| 562 |
stream.on('reconnect', () => { |
| 563 |
reconnects++; |
| 564 |
clock.tick(100); |
| 565 |
}); |
| 566 |
let downloaded = 0, destroyed = false; |
| 567 |
stream.on('data', chunk => { |
| 568 |
downloaded += chunk.length; |
| 569 |
if (downloaded / filesize >= 0.3) { |
| 570 |
destroyed = true; |
| 571 |
destroy(req, res); |
| 572 |
if (reconnects < 2) { |
| 573 |
scope.get('/myfile') |
| 574 |
.reply(206, () => fs.createReadStream(file, { start: downloaded }), { |
| 575 |
'content-range': `bytes ${downloaded}-${filesize}/${filesize}`, |
| 576 |
'content-length': `${filesize - downloaded}`, |
| 577 |
'accept-ranges': 'bytes', |
| 578 |
}); |
| 579 |
} else { |
| 580 |
scope.done(); |
| 581 |
assert.equal(reconnects, 2); |
| 582 |
assert.ok(destroyed); |
| 583 |
assert.notEqual(downloaded, filesize); |
| 584 |
done(); |
| 585 |
} |
| 586 |
} |
| 587 |
}); |
| 588 |
stream.on('end', () => { |
| 589 |
// Does fire in node v12 |
| 590 |
// done(Error('should not end')); |
| 591 |
}); |
| 592 |
}); |
| 593 |
}); |
| 594 |
|
| 595 |
describe('with ranged request headers', () => { |
| 596 |
it('Downloads correct portion of file', done => { |
| 597 |
const start = Math.round(filesize / 3); |
| 598 |
const scope = nock('http://mysite.com', { reqheaders: { Range: /bytes=/ } }) |
| 599 |
.get('/myfile') |
| 600 |
.reply(206, () => fs.createReadStream(file, { start }), { |
| 601 |
'content-length': `${filesize - start}`, |
| 602 |
'content-range': `bytes ${start}-${filesize}/${filesize}`, |
| 603 |
'accept-ranges': 'bytes', |
| 604 |
}); |
| 605 |
const stream = miniget('http://mysite.com/myfile', { |
| 606 |
maxReconnects: 1, |
| 607 |
headers: { Range: `bytes=${start}-` }, |
| 608 |
}); |
| 609 |
let req: ClientRequest, res: IncomingMessage; |
| 610 |
stream.on('request', a => { req = a; }); |
| 611 |
stream.on('response', a => { res = a; }); |
| 612 |
let reconnects = 0; |
| 613 |
stream.on('reconnect', () => { |
| 614 |
reconnects++; |
| 615 |
clock.tick(100); |
| 616 |
}); |
| 617 |
let downloaded = start, destroyed = false; |
| 618 |
stream.on('data', chunk => { |
| 619 |
downloaded += chunk.length; |
| 620 |
if (!destroyed && downloaded / filesize >= 0.5) { |
| 621 |
destroyed = true; |
| 622 |
scope.get('/myfile') |
| 623 |
.reply(206, () => fs.createReadStream(file, { start: downloaded }), { |
| 624 |
'content-range': `bytes ${downloaded}-${filesize}/${filesize}`, |
| 625 |
'content-length': `${filesize - downloaded}`, |
| 626 |
'accept-ranges': 'bytes', |
| 627 |
}); |
| 628 |
destroy(req, res); |
| 629 |
} |
| 630 |
}); |
| 631 |
stream.on('error', done); |
| 632 |
stream.on('end', () => { |
| 633 |
scope.done(); |
| 634 |
assert.ok(destroyed); |
| 635 |
assert.equal(reconnects, 1); |
| 636 |
assert.equal(downloaded, filesize); |
| 637 |
done(); |
| 638 |
}); |
| 639 |
}); |
| 640 |
}); |
| 641 |
}); |
| 642 |
|
| 643 |
describe('that gets destroyed', () => { |
| 644 |
describe('immediately', () => { |
| 645 |
it('Does not end stream', done => { |
| 646 |
nock('http://anime.me') |
| 647 |
.get('/') |
| 648 |
.reply(200, 'ooooaaaaaaaeeeee'); |
| 649 |
const stream = miniget('http://anime.me'); |
| 650 |
stream.on('end', () => { |
| 651 |
done(Error('`end` event should not be called')); |
| 652 |
}); |
| 653 |
stream.on('error', () => { |
| 654 |
// Ignore error on node v10, 12. |
| 655 |
}); |
| 656 |
stream.destroy(); |
| 657 |
done(); |
| 658 |
}); |
| 659 |
}); |
| 660 |
describe('after getting `request`', () => { |
| 661 |
it('Does not start download, no `response` event', done => { |
| 662 |
nock('https://friend.com') |
| 663 |
.get('/yes') |
| 664 |
.reply(200, '<html>my reply :)</html>'); |
| 665 |
const stream = miniget('https://friend.com/yes'); |
| 666 |
stream.on('end', () => { |
| 667 |
done(Error('`end` event should not emit')); |
| 668 |
}); |
| 669 |
stream.on('response', () => { |
| 670 |
done(Error('`response` event should not emit')); |
| 671 |
}); |
| 672 |
stream.on('data', () => { |
| 673 |
done(Error('Should not read any data')); |
| 674 |
}); |
| 675 |
stream.on('error', () => { |
| 676 |
// Ignore error on node v10, 12. |
| 677 |
}); |
| 678 |
stream.on('request', () => { |
| 679 |
stream.destroy(); |
| 680 |
done(); |
| 681 |
}); |
| 682 |
}); |
| 683 |
}); |
| 684 |
describe('after getting `response` but before end', () => { |
| 685 |
it('Response does not give any data', done => { |
| 686 |
const file = path.resolve(__dirname, 'video.flv'); |
| 687 |
const scope = nock('http://www.google1.com') |
| 688 |
.get('/one') |
| 689 |
.delayBody(100) |
| 690 |
.replyWithFile(200, file); |
| 691 |
const stream = miniget('http://www.google1.com/one'); |
| 692 |
stream.on('end', () => { |
| 693 |
done(Error('`end` event should not emit')); |
| 694 |
}); |
| 695 |
|
| 696 |
stream.on('data', () => { |
| 697 |
done(Error('Should not read any data')); |
| 698 |
}); |
| 699 |
const errorSpy = sinon.spy(); |
| 700 |
stream.on('error', errorSpy); |
| 701 |
stream.on('response', () => { |
| 702 |
stream.destroy(); |
| 703 |
scope.done(); |
| 704 |
assert.ok(!errorSpy.called); |
| 705 |
done(); |
| 706 |
}); |
| 707 |
}); |
| 708 |
}); |
| 709 |
|
| 710 |
describe('using `abort()`', () => { |
| 711 |
it('Emits `abort` and does not end stream', done => { |
| 712 |
nock('http://anime.me') |
| 713 |
.get('/') |
| 714 |
.reply(200, 'ooooaaaaaaaeeeee'); |
| 715 |
const stream = miniget('http://anime.me'); |
| 716 |
stream.on('end', () => { |
| 717 |
done(Error('`end` event should not be called')); |
| 718 |
}); |
| 719 |
stream.on('error', () => { |
| 720 |
// Ignore error on node v10, 12. |
| 721 |
}); |
| 722 |
stream.on('abort', done); |
| 723 |
stream.abort(); |
| 724 |
}); |
| 725 |
}); |
| 726 |
}); |
| 727 |
|
| 728 |
describe('with `acceptEncoding` option', () => { |
| 729 |
const file = path.resolve(__dirname, 'video.flv'); |
| 730 |
let filesize: number; |
| 731 |
before(done => { |
| 732 |
fs.stat(file, (err, stat) => { |
| 733 |
assert.ifError(err); |
| 734 |
filesize = stat.size; |
| 735 |
done(); |
| 736 |
}); |
| 737 |
}); |
| 738 |
|
| 739 |
it('Decompresses stream', async() => { |
| 740 |
const res = fs.createReadStream(file).pipe(zlib.createGzip()); |
| 741 |
const scope = nock('http://yoursite.com', { |
| 742 |
reqheaders: { 'Accept-Encoding': 'gzip' }, |
| 743 |
}) |
| 744 |
.get('/compressedfile') |
| 745 |
.reply(200, res, { |
| 746 |
'content-length': `${filesize}`, |
| 747 |
'content-encoding': 'gzip', |
| 748 |
}); |
| 749 |
const stream = miniget('http://yoursite.com/compressedfile', { |
| 750 |
acceptEncoding: { gzip: (): Transform => zlib.createGunzip() }, |
| 751 |
maxRetries: 0, |
| 752 |
}); |
| 753 |
let equal = await streamEqual(fs.createReadStream(file), stream); |
| 754 |
assert.ok(equal); |
| 755 |
scope.done(); |
| 756 |
}); |
| 757 |
|
| 758 |
describe('compressed twice', () => { |
| 759 |
it('Decompresses stream', async() => { |
| 760 |
const res = fs.createReadStream(file) |
| 761 |
.pipe(zlib.createGzip()) |
| 762 |
.pipe(zlib.createDeflate()); |
| 763 |
const scope = nock('http://yoursite.com', { |
| 764 |
reqheaders: {}, |
| 765 |
}) |
| 766 |
.get('/compressedfile') |
| 767 |
.reply(200, res, { |
| 768 |
'content-length': `${filesize}`, |
| 769 |
'content-encoding': 'gzip, deflate', |
| 770 |
}); |
| 771 |
const stream = miniget('http://yoursite.com/compressedfile', { |
| 772 |
acceptEncoding: { |
| 773 |
gzip: (): Transform => zlib.createGunzip(), |
| 774 |
deflate: (): Transform => zlib.createInflate(), |
| 775 |
}, |
| 776 |
maxRetries: 0, |
| 777 |
}); |
| 778 |
let equal = await streamEqual(fs.createReadStream(file), stream); |
| 779 |
assert.ok(equal); |
| 780 |
scope.done(); |
| 781 |
}); |
| 782 |
}); |
| 783 |
|
| 784 |
describe('compressed incorrectly', () => { |
| 785 |
it('Emits compression error', async() => { |
| 786 |
const res = fs.createReadStream(file) |
| 787 |
.pipe(zlib.createGzip()) |
| 788 |
.pipe(zlib.createDeflate()); |
| 789 |
const scope = nock('http://yoursite.com', { |
| 790 |
reqheaders: { 'Accept-Encoding': 'gzip, deflate' }, |
| 791 |
}) |
| 792 |
.get('/compressedfile') |
| 793 |
.reply(200, res, { |
| 794 |
'content-length': `${filesize}`, |
| 795 |
// Encoding is in reverse order. |
| 796 |
'content-encoding': 'deflate, gzip', |
| 797 |
}); |
| 798 |
const stream = miniget('http://yoursite.com/compressedfile', { |
| 799 |
maxRetries: 0, |
| 800 |
acceptEncoding: { |
| 801 |
gzip: (): Transform => zlib.createGunzip(), |
| 802 |
deflate: (): Transform => zlib.createInflate(), |
| 803 |
}, |
| 804 |
}); |
| 805 |
await assert.rejects( |
| 806 |
streamEqual(fs.createReadStream(file), stream), |
| 807 |
{ message: 'incorrect header check' }, |
| 808 |
); |
| 809 |
scope.done(); |
| 810 |
}); |
| 811 |
}); |
| 812 |
|
| 813 |
describe('without matching decompressing stream', () => { |
| 814 |
it('Gets original compressed stream', async() => { |
| 815 |
const res = fs.createReadStream(file).pipe(zlib.createGzip()); |
| 816 |
const scope = nock('http://yoursite.com', { |
| 817 |
reqheaders: { 'Accept-Encoding': 'deflate' }, |
| 818 |
}) |
| 819 |
.get('/compressedfile') |
| 820 |
.reply(200, res, { |
| 821 |
'content-length': `${filesize}`, |
| 822 |
'content-encoding': 'gzip', |
| 823 |
}); |
| 824 |
const stream = miniget('http://yoursite.com/compressedfile', { |
| 825 |
acceptEncoding: { |
| 826 |
deflate: (): Transform => zlib.createInflate(), |
| 827 |
}, |
| 828 |
maxRetries: 0, |
| 829 |
}); |
| 830 |
const expected = fs.createReadStream(file).pipe(zlib.createGzip()); |
| 831 |
let equal = await streamEqual(expected, stream); |
| 832 |
assert.ok(equal); |
| 833 |
scope.done(); |
| 834 |
}); |
| 835 |
}); |
| 836 |
|
| 837 |
describe('destroy mid-stream', () => { |
| 838 |
it('Stops stream without error', done => { |
| 839 |
const res = fs.createReadStream(file) |
| 840 |
.pipe(zlib.createGzip()) |
| 841 |
.pipe(zlib.createDeflate()); |
| 842 |
const scope = nock('http://yoursite.com', { |
| 843 |
reqheaders: { 'Accept-Encoding': 'gzip, deflate' }, |
| 844 |
}) |
| 845 |
.get('/compressedfile') |
| 846 |
.reply(200, res, { |
| 847 |
'content-length': `${filesize}`, |
| 848 |
'content-encoding': 'gzip, deflate', |
| 849 |
}); |
| 850 |
const stream = miniget('http://yoursite.com/compressedfile', { |
| 851 |
acceptEncoding: { |
| 852 |
gzip: (): Transform => zlib.createGunzip(), |
| 853 |
deflate: (): Transform => zlib.createInflate(), |
| 854 |
}, |
| 855 |
maxRetries: 0, |
| 856 |
}); |
| 857 |
stream.on('error', done); |
| 858 |
stream.resume(); |
| 859 |
let downloaded = 0; |
| 860 |
stream.on('data', chunk => { |
| 861 |
downloaded += chunk.length; |
| 862 |
if (downloaded / filesize > 0.5) { |
| 863 |
stream.destroy(); |
| 864 |
scope.done(); |
| 865 |
done(); |
| 866 |
} |
| 867 |
}); |
| 868 |
}); |
| 869 |
}); |
| 870 |
}); |
| 871 |
|
| 872 |
describe('`response` emits error after end', () => { |
| 873 |
it('Error does not get emitted to stream', done => { |
| 874 |
const scope = nock('https://hello.com') |
| 875 |
.get('/one/two') |
| 876 |
.reply(200, '<html></html>'); |
| 877 |
const stream = miniget('https://hello.com/one/two'); |
| 878 |
stream.resume(); |
| 879 |
let res: IncomingMessage; |
| 880 |
stream.on('response', a => res = a); |
| 881 |
stream.on('error', done); |
| 882 |
stream.on('end', () => { |
| 883 |
process.nextTick(() => { |
| 884 |
res.emit('error', Error('random after end error')); |
| 885 |
}); |
| 886 |
scope.done(); |
| 887 |
done(); |
| 888 |
}); |
| 889 |
}); |
| 890 |
}); |
| 891 |
|
| 892 |
describe('with `method = "HEAD"`', () => { |
| 893 |
it('Emits `response`', done => { |
| 894 |
const scope = nock('http://hello.net') |
| 895 |
.head('/world') |
| 896 |
.reply(200, '', { 'content-length': '10' }); |
| 897 |
const stream = miniget('http://hello.net/world', { method: 'HEAD' }); |
| 898 |
stream.on('error', done); |
| 899 |
stream.on('response', res => { |
| 900 |
scope.done(); |
| 901 |
assert.equal(res.headers['content-length'], '10'); |
| 902 |
done(); |
| 903 |
}); |
| 904 |
}); |
| 905 |
}); |
| 906 |
|
| 907 |
it('Events from request and response are forwarded to miniget stream', done => { |
| 908 |
const scope = nock('https://randomhost.com') |
| 909 |
.get('/randompath') |
| 910 |
.reply(200, 'hi'); |
| 911 |
const stream = miniget('https://randomhost.com/randompath'); |
| 912 |
const socketSpy = sinon.spy(); |
| 913 |
stream.on('socket', socketSpy); |
| 914 |
stream.on('end', () => { |
| 915 |
scope.done(); |
| 916 |
assert.equal(socketSpy.callCount, 1); |
| 917 |
done(); |
| 918 |
}); |
| 919 |
stream.resume(); |
| 920 |
}); |
| 921 |
}); |
| 922 |
|
| 923 |
describe('Import the module', () => { |
| 924 |
it('Exposes default options', () => { |
| 925 |
assert.ok(miniget.defaultOptions); |
| 926 |
}); |
| 927 |
it('Exposes MinigetError', () => { |
| 928 |
assert.ok(miniget.MinigetError); |
| 929 |
}); |
| 930 |
}); |