Permalink
Browse files

request abortion support (closes #44)

  • Loading branch information...
1 parent 6e55df7 commit 3f2153112101f4edd57f719b763e1b3e9321d1c6 @disfated disfated committed Jan 8, 2012
Showing with 53 additions and 20 deletions.
  1. +9 −0 README.md
  2. +6 −1 lib/restler.js
  3. +38 −19 test/restler.js
View
@@ -31,13 +31,22 @@ API
Basic method to make a request of any type. The function returns a RestRequest object that emits events:
+#### events
+
* `complete: function(data, response)` - emitted when the request has finished whether it was successful or not. Gets passed the response data and the response object as arguments. If some error has occurred, `data` is always instance of `Error`.
* `success: function(data, response)` - emitted when the request was successful. Gets passed the response data and the response object as arguments.
* `fail`: function(data, response)` - emitted when the request was successful, but 4xx status code returned. Gets passed the response data and the response object as arguments.
* `error`: function(err, response)` - emitted when some errors have occurred (eg. connection aborted, parse, encoding, decoding failed or some other unhandled errors). Gets passed the `Error` object and the response object (when available) as arguments.
+* `abort`: function()` - emitted when `request.abort()` is called.
* `2XX`, `3XX`, `4XX`, `5XX: function(data, response)` - emitted for all requests with response codes in the range (eg. `2XX` emitted for 200, 201, 203).
* <code><i>actual response code</i>: function(data, response)<code> - emitted for every single response code (eg. 404, 201, etc).
+#### members
+
+* `abort()` Cancels request. `abort` event is emitted. `aborted` property is set to `true`. only `complete` and `error` event should.
+* `aborted` Determines if request was aborted.
+
+
### get(url, options)
Create a GET request.
View
@@ -170,7 +170,6 @@ mixin(Request.prototype, {
},
_makeRequest: function() {
var self = this;
-
this.request.on('response', function(response) {
self._responseHandler(response);
}).on('error', function(err) {
@@ -192,6 +191,12 @@ mixin(Request.prototype, {
}
return this;
+ },
+ abort: function() {
+ this.aborted = true;
+ this.request.abort();
+ this.emit('abort');
+ return this;
}
});
View
@@ -3,6 +3,7 @@ var rest = require('../lib/restler');
var http = require('http');
var sys = require('util');
var zlib = require('zlib');
+var p = sys.inspect;
var port = 9000;
var hostname = 'localhost';
@@ -285,35 +286,35 @@ module.exports['Deserialization'] = {
'Should parse JSON': function(test) {
rest.get(host + '/json').on('complete', function(data) {
- test.equal(data.ok, true, 'returned: ' + sys.inspect(data));
+ test.equal(data.ok, true, 'returned: ' + p(data));
test.done();
});
},
'Should parse XML': function(test) {
rest.get(host + '/xml').on('complete', function(data, response) {
- test.equal(data.ok, 'true', 'returned: ' + response.raw + ' || ' + sys.inspect(data));
+ test.equal(data.ok, 'true', 'returned: ' + response.raw + ' || ' + p(data));
test.done();
});
},
'Should parse YAML': function(test) {
rest.get(host + '/yaml').on('complete', function(data) {
- test.equal(data.ok, true, 'returned: ' + sys.inspect(data));
+ test.equal(data.ok, true, 'returned: ' + p(data));
test.done();
});
},
'Should gunzip': function(test) {
rest.get(host + '/gzip').on('complete', function(data) {
- test.re(data, /^(compressed data){10}$/, 'returned: ' + sys.inspect(data));
+ test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
test.done();
})
},
'Should inflate': function(test) {
rest.get(host + '/deflate').on('complete', function(data) {
- test.re(data, /^(compressed data){10}$/, 'returned: ' + sys.inspect(data));
+ test.re(data, /^(compressed data){10}$/, 'returned: ' + p(data));
test.done();
})
},
@@ -324,9 +325,9 @@ module.exports['Deserialization'] = {
with (data) {
var result = what + (is + the + answer + to + life + the + universe + and + everything).length;
}
- test.equal(result, 42, 'returned: ' + sys.inspect(data));
+ test.equal(result, 42, 'returned: ' + p(data));
} catch (err) {
- test.ok(false, 'returned: ' + sys.inspect(data));
+ test.ok(false, 'returned: ' + p(data));
}
test.done();
})
@@ -335,23 +336,23 @@ module.exports['Deserialization'] = {
'Should decode as buffer': function(test) {
rest.get(host + '/binary', { decoding: 'buffer' }).on('complete', function(data) {
test.ok(data instanceof Buffer, 'should be buffer');
- test.equal(data.toString('base64'), 'CR5Ah8g=', 'returned: ' + sys.inspect(data));
+ test.equal(data.toString('base64'), 'CR5Ah8g=', 'returned: ' + p(data));
test.done();
})
},
'Should decode as binary': function(test) {
rest.get(host + '/binary', { decoding: 'binary' }).on('complete', function(data) {
- test.ok(typeof data == 'string', 'should be string: ' + sys.inspect(data));
- test.equal(data, '\t\u001e@‡È', 'returned: ' + sys.inspect(data));
+ test.ok(typeof data == 'string', 'should be string: ' + p(data));
+ test.equal(data, '\t\u001e@‡È', 'returned: ' + p(data));
test.done();
})
},
'Should decode as base64': function(test) {
rest.get(host + '/binary', { decoding: 'base64' }).on('complete', function(data) {
- test.ok(typeof data == 'string', 'should be string: ' + sys.inspect(data));
- test.equal(data, 'CR5Ah8g=', 'returned: ' + sys.inspect(data));
+ test.ok(typeof data == 'string', 'should be string: ' + p(data));
+ test.equal(data, 'CR5Ah8g=', 'returned: ' + p(data));
test.done();
})
},
@@ -364,15 +365,15 @@ module.exports['Deserialization'] = {
},
data: JSON.stringify(obj)
}).on('complete', function(data) {
- test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
+ test.equal(obj.secret, data.secret, 'returned: ' + p(data));
test.done();
})
},
'Should post and parse JSON via shortcut method': function(test) {
var obj = { secret : 'very secret string' };
rest.postJson(host + '/push-json', obj).on('complete', function(data) {
- test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
+ test.equal(obj.secret, data.secret, 'returned: ' + p(data));
test.done();
});
},
@@ -386,11 +387,29 @@ module.exports['Deserialization'] = {
};
rest.get(host + '/custom-mime').on('complete', function(data) {
test.expect(3);
- test.ok(Array.isArray(data), 'should be array, returned: ' + sys.inspect(data));
- test.equal(data.join(''), '666', 'should be [6,6,6], returned: ' + sys.inspect(data));
- test.equal(data.__parsedBy__, 'github', 'should use vendor-specific parser, returned: ' + sys.inspect(data.__parsedBy__));
+ test.ok(Array.isArray(data), 'should be array, returned: ' + p(data));
+ test.equal(data.join(''), '666', 'should be [6,6,6], returned: ' + p(data));
+ test.equal(data.__parsedBy__, 'github', 'should use vendor-specific parser, returned: ' + p(data.__parsedBy__));
test.done();
});
+ },
+
+ '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));
+ test.done();
+ }).on('error', 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();
}
};
@@ -422,7 +441,7 @@ module.exports['Redirect'] = {
'Should follow redirects': function(test) {
rest.get(host).on('complete', function(data) {
- test.equal(data, 'redirected', 'returned: ' + sys.inspect(data));
+ test.equal(data, 'redirected', 'returned: ' + p(data));
test.done();
});
},
@@ -431,7 +450,7 @@ module.exports['Redirect'] = {
rest.get(host, {
headers: { 'x-redirects': '5' }
}).on('complete', function(data) {
- test.equal(data, '5', 'returned: ' + sys.inspect(data));
+ test.equal(data, '5', 'returned: ' + p(data));
test.done();
});
}

0 comments on commit 3f21531

Please sign in to comment.