diff --git a/index.js b/index.js index 67b733878..0372022fb 100644 --- a/index.js +++ b/index.js @@ -37,9 +37,6 @@ function requestAsEventEmitter(opts) { var req = fn.request(opts, function (res) { var statusCode = res.statusCode; - res.url = redirectUrl || requestUrl; - res.requestUrl = requestUrl; - if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { res.resume(); @@ -59,7 +56,11 @@ function requestAsEventEmitter(opts) { // do not write ee.bind(...) instead of function - it will break gzip in Node.js 0.10 setImmediate(function () { - ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res); + const response = typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res; + response.url = redirectUrl || requestUrl; + response.requestUrl = requestUrl; + + ee.emit('response', response); }); }); diff --git a/test/gzip.js b/test/gzip.js index d36e6d80d..839eb6197 100644 --- a/test/gzip.js +++ b/test/gzip.js @@ -67,6 +67,12 @@ test('ignore missing data', async t => { t.is((await got(`${s.url}/missing-data`)).body, testContent); }); +test('has url and requestUrl properties', async t => { + const res = await got(s.url); + t.truthy(res.url); + t.truthy(res.requestUrl); +}); + test.after('cleanup', async () => { await s.close(); });