Skip to content

Commit

Permalink
Implement redirect event
Browse files Browse the repository at this point in the history
Closes #58
  • Loading branch information
floatdrop committed May 5, 2015
1 parent 94387b1 commit 52490b2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ function got(url, opts, cb) {
delete opts.port;
delete opts.path;

if (proxy) {
proxy.emit('redirect', res, opts);
}

get(urlLib.resolve(url, res.headers.location), opts, cb);
return;
}
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,17 @@ The data you requested.

The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).


When in stream mode, you can listen for events:

##### .on('response', response)

When in stream mode, you can listen for the `response` event to get the response object.
`response` event to get the response object.

##### .on('redirect', response, nextOpts)

`redirect` event to get the response object of redirect. Second argument is options for next request to redirect location.


###### response

Expand Down
12 changes: 12 additions & 0 deletions test/test-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ tape('redirect only GET and HEAD requests', function (t) {
});
});

tape('redirect event', function (t) {
got(s.url + '/endless')
.on('redirect', function (res, opts) {
t.equal(res.headers.location, s.url + '/endless');
opts.path = '/';
})
.on('data', function (data) {
t.equal(data.toString(),'reached');
t.end();
});
});

tape('cleanup', function (t) {
s.close();
t.end();
Expand Down

0 comments on commit 52490b2

Please sign in to comment.