From 52490b2e88f263ab8abfb9498cb65575f3a15b01 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Tue, 5 May 2015 09:00:33 +0500 Subject: [PATCH] Implement redirect event Closes #58 --- index.js | 4 ++++ readme.md | 10 +++++++++- test/test-redirects.js | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2db2ef5..c108511 100644 --- a/index.js +++ b/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; } diff --git a/readme.md b/readme.md index f8aab5d..056d400 100644 --- a/readme.md +++ b/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 diff --git a/test/test-redirects.js b/test/test-redirects.js index cfa0f1e..a41204a 100644 --- a/test/test-redirects.js +++ b/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();