Skip to content

Commit

Permalink
Add requestUrl to response in streaming mode (#230)
Browse files Browse the repository at this point in the history
* Add `requestUrl` to response in streaming mode (Fixes #229).
* Set `requestUrl` inside `requestAsEventEmitter()`
  • Loading branch information
kevva authored and floatdrop committed Oct 20, 2016
1 parent 2e8a9a2 commit 205e224
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

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

0 comments on commit 205e224

Please sign in to comment.