Skip to content

Commit

Permalink
support for soft- and hard-aborts
Browse files Browse the repository at this point in the history
  • Loading branch information
disfated committed Jan 15, 2012
1 parent 57d80be commit 0ef4893
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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.


Expand Down
33 changes: 28 additions & 5 deletions lib/restler.js
Expand Up @@ -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() {
Expand All @@ -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;
}
});
Expand Down
51 changes: 37 additions & 14 deletions test/restler.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0ef4893

Please sign in to comment.