Skip to content

Commit

Permalink
fix: emit error on redirect response with no location
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Dec 5, 2020
1 parent 021b985 commit 5de3107
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
let parsed: RequestOptions = {}, httpLib;
try {
parsed = urlParse(url);
httpLib = httpLibs[parsed.protocol as string];
httpLib = httpLibs[parsed.protocol];
} catch (err) {
// Let the error be caught by the if statement below.
}
Expand All @@ -175,7 +175,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
return;
}
if (!parsed || parsed.protocol) {
httpLib = httpLibs[parsed?.protocol as string];
httpLib = httpLibs[parsed?.protocol];
if (!httpLib) {
stream.emit('error', new Miniget.MinigetError('Invalid URL object from `transform` function'));
return;
Expand Down Expand Up @@ -223,7 +223,14 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
if (redirects++ >= opts.maxRedirects) {
stream.emit('error', new Miniget.MinigetError('Too many redirects'));
} else {
url = res.headers.location as string;
if (res.headers.location) {
url = res.headers.location;
} else {
let err = new Miniget.MinigetError('Redirect status code given with no location', res.statusCode);
stream.emit('error', err);
cleanup();
return;
}
setTimeout(doDownload, res.headers['retry-after'] ? parseInt(res.headers['retry-after'], 10) * 1000 : 0);
stream.emit('redirect', url);
}
Expand Down
14 changes: 14 additions & 0 deletions test/request-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ describe('Make a request', () => {
});
});
});

describe('without `location` header', () => {
it('Throws an error', done => {
const scope = nock('http://mysite.com')
.get('/pathy')
.reply(302, '', {});
const stream = miniget('http://mysite.com/pathy');
stream.on('error', err => {
scope.done();
assert.equal(err.message, 'Redirect status code given with no location');
done();
});
});
});
});

describe('that gets api limited', () => {
Expand Down

0 comments on commit 5de3107

Please sign in to comment.