Comparing version 2.6.2 to 2.7.0
@@ -61,3 +61,6 @@ var merge = require("./merge"); | ||
return next().then(function(response) { | ||
if (response.statusCode >= 400 && request.options.exceptions !== false) { | ||
var exceptions = request.options.exceptions; | ||
var isException = exceptions == false? false: typeof exceptions == 'function'? exceptions(response): response.statusCode >= 400; | ||
if (isException) { | ||
var msg = request.method.toUpperCase() + " " + obfuscateUrlPassword(request.url) + " => " + response.statusCode + " " + response.statusText; | ||
@@ -64,0 +67,0 @@ var error = extend(new Error(msg), response); |
{ | ||
"name": "httpism", | ||
"version": "2.6.2", | ||
"version": "2.7.0", | ||
"description": "HTTP client with middleware and good defaults", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,6 +16,11 @@ module.exports = { | ||
stringify: function (params) { | ||
return Object.keys(params).map(function (key) { | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); | ||
}).join('&'); | ||
return Object.keys(params) | ||
.filter(function (key) { | ||
return typeof(params[key]) !== 'undefined'; | ||
}) | ||
.map(function (key) { | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); | ||
}) | ||
.join('&'); | ||
} | ||
}; |
@@ -264,3 +264,3 @@ # httpism [![npm version](https://img.shields.io/npm/v/httpism.svg)](https://www.npmjs.com/package/httpism) [![npm](https://img.shields.io/npm/dm/httpism.svg)](https://www.npmjs.com/package/httpism) [![Build Status](https://travis-ci.org/featurist/httpism.svg?branch=master)](https://travis-ci.org/featurist/httpism) | ||
* `exceptions`: default `true`, throw exceptions on reception of 400-500 status codes. Set to `false` to simply return the response. | ||
* `exceptions`: default `true`, throw exceptions on reception of 400-500 status codes. Set to `false` to simply return the response. If set to a function, the function is passed the response, and returns true to throw the response as an exception, or false to treat it as a normal response. | ||
* `redirect`: default `true`, follow redirects for 300, 301, 302, 303 and 307 status codes with `Location` response headers. Set to `false` to simply return the redirect response. | ||
@@ -267,0 +267,0 @@ * `headers`: default `undefined`, can be set to an object that is merged with middleware headers. |
@@ -548,2 +548,28 @@ var httpism = require("../index"); | ||
describe('error predicate', function () { | ||
it("throws exceptions when predicate returns true", function() { | ||
function isError(response) { | ||
return response.statusCode == 400; | ||
} | ||
return httpism.api(baseurl).get("/400", { exceptions: isError }).then(function() { | ||
assert.fail("expected an exception to be thrown"); | ||
}).catch(function(e) { | ||
e.message.should.equal("GET " + baseurl + "/400 => 400 Bad Request"); | ||
e.statusCode.should.equal(400); | ||
e.body.message.should.equal("oh dear"); | ||
}); | ||
}); | ||
it("doesn't throw exceptions when predicate returns false", function() { | ||
function isError(response) { | ||
return response.statusCode != 400; | ||
} | ||
return httpism.api(baseurl).get("/400", { exceptions: isError }).then(function(response) { | ||
response.body.message.should.equal("oh dear"); | ||
}); | ||
}); | ||
}); | ||
it("throws if it cannot connect", function() { | ||
@@ -550,0 +576,0 @@ return expect(httpism.get("http://localhost:50000/")).to.eventually.be.rejectedWith("ECONNREFUSED"); |
101924
23
2313