Browse Source

Add `requestUrl` to response in streaming mode (#230)

* Add `requestUrl` to response in streaming mode (Fixes #229).
* Set `requestUrl` inside `requestAsEventEmitter()`
node-7
Kevin Mårtensson 8 years ago
committed by Vsevolod Strukchinsky
parent
commit
205e224bcd
  1. 4
      index.js
  2. 8
      test/stream.js

4
index.js

@ -22,6 +22,7 @@ function requestAsEventEmitter(opts) {
opts = opts || {};
const ee = new EventEmitter();
const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
let redirectCount = 0;
let retryCount = 0;
let redirectUrl;
@ -36,6 +37,8 @@ function requestAsEventEmitter(opts) {
res.url = redirectUrl;
}
res.requestUrl = requestUrl;
if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();
@ -105,7 +108,6 @@ function asPromise(opts) {
const limitStatusCode = opts.followRedirect ? 299 : 399;
res.body = data;
res.requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
if (opts.json && res.body) {
try {

8
test/stream.js

@ -119,6 +119,14 @@ test.cb('accepts option.body as Stream', t => {
});
});
test.cb('redirect response contains old url', t => {
got.stream(`${s.url}/redirect`)
.on('response', res => {
t.is(res.requestUrl, `${s.url}/redirect`);
t.end();
});
});
test.after('cleanup', async () => {
await s.close();
});

Loading…
Cancel
Save