Browse Source

fix url and responseUrl properties on gzip'ed response

Closes #240
v5.x
Vsevolod Strukchinsky 8 years ago
parent
commit
382a0cfdd3
  1. 9
      index.js
  2. 6
      test/gzip.js

9
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);
var response = typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res;
response.url = redirectUrl || requestUrl;
response.requestUrl = requestUrl;
ee.emit('response', response);
});
});

6
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();
});

Loading…
Cancel
Save