From 7e8af3362bbe3a6175ab6ec0bbc3cc88f4d6b206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20M=C3=A5rtensson?= Date: Thu, 27 Oct 2016 12:53:14 +0200 Subject: [PATCH] Add `url` to response (#236) * Add `url` to response Fixes #235. --- index.js | 5 +---- readme.md | 2 +- test/http.js | 4 ++++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8e5aca3..0ce11d4 100644 --- a/index.js +++ b/index.js @@ -33,10 +33,7 @@ function requestAsEventEmitter(opts) { const req = fn.request(opts, res => { const statusCode = res.statusCode; - if (redirectUrl) { - res.url = redirectUrl; - } - + res.url = redirectUrl || requestUrl; res.requestUrl = requestUrl; if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { diff --git a/readme.md b/readme.md index 10cb907..094991d 100644 --- a/readme.md +++ b/readme.md @@ -56,7 +56,7 @@ It's a `GET` request by default, but can be changed in `options`. #### got(url, [options]) -Returns a Promise for a `response` object with a `body` property, a `url` property with the final URL after redirects, and a `requestUrl` property with the original request URL. +Returns a Promise for a `response` object with a `body` property, a `url` property with the request URL or the final URL after redirects, and a `requestUrl` property with the original request URL. ##### url diff --git a/test/http.js b/test/http.js index c6918a4..d5553dc 100644 --- a/test/http.js +++ b/test/http.js @@ -83,6 +83,10 @@ test('requestUrl response when sending url as param', async t => { t.is((await got({hostname: s.host, port: s.port})).requestUrl, `${s.url}/`); }); +test('response contains url', async t => { + t.is((await got(s.url)).url, `${s.url}/`); +}); + test.after('cleanup', async () => { await s.close(); });