Permalink
Browse files

support for soft- and hard-aborts

  • Loading branch information...
1 parent 57d80be commit 0ef489319b8c3cb94284aa8eec154f26ca3b0dcd @disfated disfated committed Jan 15, 2012
Showing with 66 additions and 20 deletions.
  1. +1 −1 README.md
  2. +28 −5 lib/restler.js
  3. +37 −14 test/restler.js
View
@@ -44,7 +44,7 @@ Basic method to make a request of any type. The function returns a RestRequest o
#### members
-* `abort()` Cancels request. `abort` event is emitted. `aborted` property is set to `true`. only `complete` and `error` event should.
+* `abort([error])` Cancels request. `abort` event is emitted. `request.aborted` is set to `true`. If non-falsy `error` is passed, then `error` will be additionaly emitted (with `error` passed as a param and `error.type` is set to `"abort"`). Otherwise only `complete` event will raise.
* `aborted` Determines if request was aborted.
View
@@ -206,7 +206,9 @@ mixin(Request.prototype, {
this.request.on('response', function(response) {
self._responseHandler(response);
}).on('error', function(err) {
- self._fireError(err, null);
+ if (!self.aborted) {
+ self._fireError(err, null);
+ }
});
},
run: function() {
@@ -225,10 +227,31 @@ mixin(Request.prototype, {
return this;
},
- abort: function() {
- this.aborted = true;
- this.request.abort();
- this.emit('abort');
+ abort: function(err) {
+ var self = this;
+
+ if (err) {
+ if (typeof err == 'string') {
+ err = new Error(err);
+ } else if (!(err instanceof Error)) {
+ err = new Error('AbortError');
+ }
+ err.type = 'abort';
+ } else {
+ err = null;
+ }
+
+ self.request.on('close', function() {
+ if (err) {
+ self._fireError(err, null);
+ } else {
+ self.emit('complete', null, null);
+ }
+ });
+
+ self.aborted = true;
+ self.request.abort();
+ self.emit('abort', err);
return this;
}
});
View
@@ -298,6 +298,12 @@ function dataResponse(request, response) {
response.writeHead(200, { 'content-type': 'application/yaml' });
response.end('{Чебурашка');
break;
+ case '/abort':
+ setTimeout(function() {
+ response.writeHead(200);
+ response.end('not aborted');
+ }, 100);
+ break;
default:
response.writeHead(404);
response.end();
@@ -431,22 +437,39 @@ module.exports['Deserialization'] = {
});
},
- 'Should correctly abort request': function(test) {
- test.expect(5);
- rest.get(host + '/json').on('complete', function(data) {
- test.ok(data instanceof Error, 'should be error, got: ' + p(data));
- test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
+ 'Should correctly soft-abort request': function(test) {
+ test.expect(4);
+ rest.get(host + '/abort').on('complete', function(data) {
+ test.equal(data, null, 'data should be null');
+ test.equal(this.aborted, true, 'should be aborted');
test.done();
- }).on('error', function(data) {
+ }).on('error', function(err) {
+ test.ok(false, 'should not emit error event');
+ }).on('abort', function(err) {
+ test.equal(err, null, 'err should be null');
+ test.equal(this.aborted, true, 'should be aborted');
+ }).on('success', function() {
+ test.ok(false, 'should not emit success event');
+ }).on('fail', function() {
+ test.ok(false, 'should not emit fail event');
+ }).abort();
+ },
+
+ 'Should correctly hard-abort request': function(test) {
+ test.expect(4);
+ rest.get(host + '/abort').on('complete', function(data) {
test.ok(data instanceof Error, 'should be error, got: ' + p(data));
- test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
- }).on('abort', function() {
- test.equal(this.aborted, true, 'should aborted, got: ' + p(this.aborted));
- }).on('success', function() {
- test.ok(false, 'should not have got here');
- }).on('fail', function() {
- test.ok(false, 'should not have got here');
- }).abort();
+ test.equal(this.aborted, true, 'should be aborted');
+ test.done();
+ }).on('error', function(err) {
+ test.ok(err instanceof Error, 'should be error, got: ' + p(err));
+ }).on('abort', function(err) {
+ test.equal(this.aborted, true, 'should be aborted');
+ }).on('success', function() {
+ test.ok(false, 'should not emit success event');
+ }).on('fail', function() {
+ test.ok(false, 'should not emit fail event');
+ }).abort(true);
},
'Should correctly handle malformed JSON': function(test) {

0 comments on commit 0ef4893

Please sign in to comment.