diff options
author | Evan Lucas <evanlucas@me.com> | 2015-02-24 18:11:11 -0600 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2015-02-25 16:40:31 -0500 |
commit | 2ca22aacbdd11c572e71ee1b15af3bec1e04a0c1 (patch) | |
tree | db2d8f88b3f05e5847c1830176ee52d5b6a77b9b | |
parent | 89e133a1d8a3bfd655d5ae4f6b7071a1bbbcdc71 (diff) | |
download | android-node-v8-2ca22aacbdd11c572e71ee1b15af3bec1e04a0c1.tar.gz android-node-v8-2ca22aacbdd11c572e71ee1b15af3bec1e04a0c1.tar.bz2 android-node-v8-2ca22aacbdd11c572e71ee1b15af3bec1e04a0c1.zip |
http: emit abort event from ClientRequest
ClientRequest will now emit an abort event the first time abort()
is called.
Semver: Minor
Fixes: https://github.com/joyent/node/issues/9278
PR-URL: https://github.com/iojs/io.js/pull/945
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
-rw-r--r-- | doc/api/http.markdown | 7 | ||||
-rw-r--r-- | lib/_http_client.js | 6 | ||||
-rw-r--r-- | test/parallel/test-http-client-abort-event.js | 28 |
3 files changed, 41 insertions, 0 deletions
diff --git a/doc/api/http.markdown b/doc/api/http.markdown index f5811e4a47..49ad0160ec 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -859,6 +859,13 @@ Emitted when the server sends a '100 Continue' HTTP response, usually because the request contained 'Expect: 100-continue'. This is an instruction that the client should send the request body. +### Event: 'abort' + +`function () { }` + +Emitted when the request has been aborted by the client. This event is only +emitted on the first call to `abort()`. + ### request.flush() Flush the request headers. diff --git a/lib/_http_client.js b/lib/_http_client.js index b02ab66b29..5e722b4abc 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -166,6 +166,12 @@ ClientRequest.prototype._implicitHeader = function() { }; ClientRequest.prototype.abort = function() { + var self = this; + if (this.aborted === undefined) { + process.nextTick(function() { + self.emit('abort'); + }); + } // Mark as aborting so we can avoid sending queued request data // This is used as a truthy flag elsewhere. The use of Date.now is for // debugging purposes only. diff --git a/test/parallel/test-http-client-abort-event.js b/test/parallel/test-http-client-abort-event.js new file mode 100644 index 0000000000..1549d06101 --- /dev/null +++ b/test/parallel/test-http-client-abort-event.js @@ -0,0 +1,28 @@ +var assert = require('assert'); +var http = require('http'); +var common = require('../common'); +var server = http.createServer(function(req, res) { + res.end(); +}); +var count = 0; +server.listen(common.PORT, function() { + var req = http.request({ + port: common.PORT + }, function() { + assert(false, 'should not receive data'); + }); + + req.on('abort', function() { + // should only be emitted once + count++; + server.close(); + }); + + req.end(); + req.abort(); + req.abort(); +}); + +process.on('exit', function() { + assert.equal(count, 1); +}) |