From 929cb59857848c1c14e470baca9a1ae204d0be8b Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Mon, 23 Mar 2015 21:17:25 +0100 Subject: [PATCH] Emit `response` event --- index.js | 4 ++++ readme.md | 8 ++++++++ test/test-http.js | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/index.js b/index.js index 20ff7f2..df928e9 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,10 @@ function got(url, opts, cb) { var statusCode = response.statusCode; var res = response; + if (proxy) { + proxy.emit('response', res); + } + // redirect if (status.redirect[statusCode] && 'location' in res.headers) { res.resume(); // Discard response diff --git a/readme.md b/readme.md index 082d09a..fe43a74 100644 --- a/readme.md +++ b/readme.md @@ -99,6 +99,14 @@ The data you requested. The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage). +##### .on('response', response) + +When in stream mode, you can listen for the `response` event to get the response object. + +###### response + +The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage). + #### got.get(url, [options], [callback]) #### got.post(url, [options], [callback]) #### got.put(url, [options], [callback]) diff --git a/test/test-http.js b/test/test-http.js index dca8a1b..3e5ac8d 100644 --- a/test/test-http.js +++ b/test/test-http.js @@ -72,6 +72,15 @@ tape('stream mode', function (t) { }); }); +tape('emit response object to stream', function (t) { + got(s.url) + .on('response', function (res) { + t.ok(res); + t.ok(res.headers); + t.end(); + }); +}); + tape('proxy errors to the stream', function (t) { got(s.url + '/404') .on('error', function (err) {