Browse Source

Implement redirect event

Closes #58
http2
Vsevolod Strukchinsky 10 years ago
parent
commit
52490b2e88
  1. 4
      index.js
  2. 10
      readme.md
  3. 12
      test/test-redirects.js

4
index.js

@ -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;
}

10
readme.md

@ -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

12
test/test-redirects.js

@ -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();

Loading…
Cancel
Save