Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add requestUrl to response in streaming mode #230

Merged
merged 2 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});