Skip to content

Commit

Permalink
fix url and responseUrl properties on gzip'ed response
Browse files Browse the repository at this point in the history
Closes #240
  • Loading branch information
floatdrop committed Nov 2, 2016
1 parent 295a6e9 commit aeafdac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
});
});

Expand Down
6 changes: 6 additions & 0 deletions test/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit aeafdac

Please sign in to comment.