Skip to content

Commit

Permalink
Emit response event
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Mar 24, 2015
1 parent 8873df9 commit 929cb59
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 9 additions & 0 deletions test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 929cb59

Please sign in to comment.