Browse files

Add http#req wildcard status code handling, e.g. 20x, 3xx.

  • Loading branch information...
1 parent d0601cf commit 79f760ca18573393b02eecc9ef515fb3e4196595 @cliffano committed Mar 26, 2013
Showing with 86 additions and 5 deletions.
  1. +1 −1 CHANGELOG.md
  2. +28 −1 lib/http.js
  3. +3 −3 package.json
  4. +54 −0 test/http.js
View
2 CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.1.3-pre
-*
+* Add http#req wildcard status code handling, e.g. 20x, 3xx
### 0.1.2
* http#proxy now handles HTTP_PROXY and HTTPS_PROXY along with http_proxy and https_proxy environment variables
View
29 lib/http.js
@@ -8,11 +8,13 @@ var request = require('request');
* @param {String} url: URL without query string
* @param {Object} opts: optional
* - proxy: proxy server URL with format http://user:pass@host:port
- * - quseryStrings: object containing URL query strings with format { name: value }
+ * - queryStrings: object containing URL query strings with format { name: value }
* - handlers: response handlers with format { statuscode: function(result, cb) }
* @param {Function} cb: standard cb(err, result) callback
*/
function req(method, url, opts, cb) {
+ opts.handlers = opts.handlers || {};
+
var params = { url: url },
envProxy = proxy(url);
@@ -26,14 +28,39 @@ function req(method, url, opts, cb) {
params.qs = opts.queryStrings;
}
+ function _wildcardMatch(statusCode) {
+ var match;
+ Object.keys(opts.handlers).forEach(function (handlerStatusCode) {
+ var regex = new RegExp(handlerStatusCode.replace(/x/g, '.'));
+ if (!match && statusCode.toString().match(regex)) {
+ match = handlerStatusCode;
+ }
+ });
+ return match;
+ }
+
request[method.toLowerCase()](params, function (err, result) {
if (err) {
cb(err);
+ } else {
+ var wildcardMatch = _wildcardMatch(result.statusCode);
+ if (opts.handlers[result.statusCode]) {
+ opts.handlers[result.statusCode](result, cb);
+ } else if (wildcardMatch) {
+ opts.handlers[wildcardMatch](result, cb);
+ } else {
+ cb(new Error('Unexpected status code: ' + result.statusCode + '\nResponse body:\n' + result.body));
+ }
+ }
+ /*
+ if (err) {
+ cb(err);
} else if (opts.handlers && opts.handlers[result.statusCode]) {
opts.handlers[result.statusCode](result, cb);
} else {
cb(new Error('Unexpected status code: ' + result.statusCode + '\nResponse body:\n' + result.body));
}
+ */
});
}
View
6 package.json
@@ -21,11 +21,11 @@
"test": "./test"
},
"dependencies": {
- "async": "0.1.22",
+ "async": "0.2.6",
"commander": "1.1.1",
- "dateformat": "1.0.4-1.2.3",
+ "dateformat": "1.0.6-1.2.3",
"jazz": "0.0.18",
- "request": "2.12.0",
+ "request": "2.16.2",
"underscore": "1.4.4"
},
"devDependencies": {
View
54 test/http.js
@@ -37,6 +37,60 @@ buster.testCase('http - request', {
done();
});
},
+ 'should handle result based on wildcard status code': function (done) {
+ this.stub(process, 'env', {});
+ this.stub(request, 'get', function (params, cb) {
+ cb(null, { statusCode: '200', body: 'somebody' });
+ });
+ function _success(result, cb) {
+ assert.equals(result.statusCode, 200);
+ cb(null, result);
+ }
+ function _honeypot(result, cb) {
+ // should never be called
+ }
+ http.request('GET', 'http://someurl', { proxy: 'http://someproxy', queryStrings: { param1: 'value1' }, handlers: { 201: _honeypot, '20x': _success } }, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result.statusCode, 200);
+ done();
+ });
+ },
+ 'should handle result based on wildcard status code with multiple wildcard characters': function (done) {
+ this.stub(process, 'env', {});
+ this.stub(request, 'get', function (params, cb) {
+ cb(null, { statusCode: '200', body: 'somebody' });
+ });
+ function _success(result, cb) {
+ assert.equals(result.statusCode, 200);
+ cb(null, result);
+ }
+ function _honeypot(result, cb) {
+ // should never be called
+ }
+ http.request('GET', 'http://someurl', { proxy: 'http://someproxy', queryStrings: { param1: 'value1' }, handlers: { '2xx': _success, 201: _honeypot } }, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result.statusCode, 200);
+ done();
+ });
+ },
+ 'should handle result based on first match when there are multiple matches': function (done) {
+ this.stub(process, 'env', {});
+ this.stub(request, 'get', function (params, cb) {
+ cb(null, { statusCode: '200', body: 'somebody' });
+ });
+ function _success(result, cb) {
+ assert.equals(result.statusCode, 200);
+ cb(null, result);
+ }
+ function _honeypot(result, cb) {
+ // should never be called
+ }
+ http.request('GET', 'http://someurl', { proxy: 'http://someproxy', queryStrings: { param1: 'value1' }, handlers: { 200: _success, '2xx': _honeypot } }, function (err, result) {
+ assert.isNull(err);
+ assert.equals(result.statusCode, 200);
+ done();
+ });
+ },
'should pass error to callback when result status code is not expected': function (done) {
this.stub(process, 'env', {});
this.stub(request, 'get', function (params, cb) {

0 comments on commit 79f760c

Please sign in to comment.