Skip to content

Commit

Permalink
Add redirect url to response object (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rowno authored and floatdrop committed Nov 1, 2016
1 parent 0a5167b commit 0dae0a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ function requestAsEventEmitter(opts) {
var ee = new EventEmitter();
var redirectCount = 0;
var retryCount = 0;
var redirectUrl;

var get = function (opts) {
var fn = opts.protocol === 'https:' ? https : http;

var req = fn.request(opts, function (res) {
var statusCode = res.statusCode;

if (redirectUrl) {
res.url = redirectUrl;
}

if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();

Expand All @@ -44,7 +49,7 @@ function requestAsEventEmitter(opts) {
return;
}

var redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));

ee.emit('redirect', res, redirectOpts);
Expand Down Expand Up @@ -94,29 +99,29 @@ function asCallback(opts, cb) {
});

ee.on('response', function (res) {
readAllStream(res, opts.encoding, function (err, data) {
readAllStream(res, opts.encoding, function (error, data) {
var statusCode = res.statusCode;
var limitStatusCode = opts.followRedirect ? 299 : 399;

if (err) {
cb(new got.ReadError(err, opts), null, res);
if (error) {
cb(new got.ReadError(error, opts), null, res);
return;
}

if (statusCode < 200 || statusCode > limitStatusCode) {
err = new got.HTTPError(statusCode, opts);
error = new got.HTTPError(statusCode, opts);
}

if (opts.json && data) {
try {
data = parseJson(data);
} catch (e) {
e.fileName = urlLib.format(opts);
err = new got.ParseError(e, statusCode, opts);
} catch (err) {
err.fileName = urlLib.format(opts);
error = new got.ParseError(err, statusCode, opts);
}
}

cb(err, data, res);
cb(error, data, res);
});
});

Expand Down Expand Up @@ -268,7 +273,7 @@ function normalizeArguments(url, opts) {
opts.method = opts.method.toUpperCase();

if (opts.hostname === 'unix') {
var matches = /(.+)\:(.+)/.exec(opts.path);
var matches = /(.+):(.+)/.exec(opts.path);

if (matches) {
opts.socketPath = matches[1];
Expand All @@ -285,7 +290,7 @@ function normalizeArguments(url, opts) {
}

var noise = Math.random() * 100;
return (1 << iter) * 1000 + noise;
return ((1 << iter) * 1000) + noise;
};
}

Expand All @@ -309,8 +314,8 @@ function got(url, opts, cb) {

try {
return asPromise(normalizeArguments(url, opts));
} catch (error) {
return PinkiePromise.reject(error);
} catch (err) {
return PinkiePromise.reject(err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"pem": "^1.4.4",
"pify": "^2.3.0",
"tempfile": "^1.1.1",
"xo": "*"
"xo": "0.16.x"
},
"xo": {
"ignores": [
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ It's a `GET` request by default, but can be changed in `options`.

#### got(url, [options], [callback])

Without `callback` argument returns a Promise, that resolves to `response` object with `body` property and a `url` property (which contains the final URL after redirects).

Otherwise calls callback with `response` object (same as in previous case).

##### url

Type: `string`, `object`
Expand Down
7 changes: 6 additions & 1 deletion test/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ test('redirects works with lowercase method', async t => {
t.is(body, '');
});

test.after('cleanup', async t => {
test('redirect response contains new url', async t => {
const url = (await got(`${s.url}/finite`)).url;
t.is(url, `${s.url}/`);
});

test.after('cleanup', async () => {
await s.close();
});

0 comments on commit 0dae0a8

Please sign in to comment.