Skip to content

Commit

Permalink
fix(headers): filter out headers with undefined value (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed May 15, 2019
1 parent e330afd commit de09586
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -93,7 +93,7 @@ codestyle-fix: $(NODE_MODULES) ## Run and fix style check errors


.PHONY: prepush
prepush: $(NODE_MODULES) lint codestyle test versioncheck ## Run all required tasks for a git push
prepush: $(NODE_MODULES) lint codestyle test ## Run all required tasks for a git push


.PHONY: test
Expand Down
14 changes: 14 additions & 0 deletions lib/HttpClient.js
Expand Up @@ -1053,6 +1053,20 @@ HttpClient.prototype._options = function (method, options) {
}
}

// filter out `undefined` headers as Node throws
// `ERR_HTTP_INVALID_HEADER_VALUE` exception.
opts.headers = Object.keys(opts.headers).reduce(
function sanitizeHeaders(_headers, key) {
var value = opts.headers[key];

if (value !== undefined) {
_headers[key] = value;
}
return _headers;
},
{}
);

return (opts);
};

Expand Down
47 changes: 46 additions & 1 deletion test/StringClient.test.js
Expand Up @@ -6,6 +6,7 @@ var fs = require('fs');
var path = require('path');

// external files
var _ = require('lodash');
var assert = require('chai').assert;
var bunyan = require('bunyan');
var restify = require('restify');
Expand All @@ -23,10 +24,21 @@ describe('StringClient', function () {
var LOG = bunyan.createLogger({
name: 'clientlog'
});
var headers = {
string: 'dark roast is awful',
number: 0,
negative_integer: -1,
undefined: undefined,
object: {},
true: true,
false: false,
null: null
};
var CLIENT = clients.createStringClient({
url: 'http://localhost:3000',
log: LOG,
retry: false
retry: false,
headers: Object.assign({}, headers)
});

beforeEach(function (done) {
Expand Down Expand Up @@ -59,6 +71,39 @@ describe('StringClient', function () {
});
});

it('should filter out undefined headers', function (done) {
SERVER.get('/ping', function (req, res, next) {
res.json({
reqHeaders: req.headers
});
return next();
});

CLIENT.get({
path: '/ping',
headers: Object.assign({}, headers)
}, function (err, req, res, text) {
assert.ifError(err);
assert.deepStrictEqual(
_.pick(req._headers, Object.keys(headers)),
_.omit(headers, 'undefined')
);
assert.deepStrictEqual(
_.pick(JSON.parse(text).reqHeaders, Object.keys(headers)),
{
false: 'false',
negative_integer: '-1',
null: 'null',
number: '0',
object: '[object Object]',
string: 'dark roast is awful',
true: 'true'
}
);
return done();
});
});

it('should support decoding gzipped utf8 multibyte responses',
function (done) {
var payload = fs.readFileSync(path.join(
Expand Down

0 comments on commit de09586

Please sign in to comment.