Adding tests for empty and [] values for elasticsearch.requestHeadersWhitelist

This commit is contained in:
Shaunak Kashyap 2016-04-15 12:15:58 -07:00
parent eeedc54ce8
commit c5ce30f81c
No known key found for this signature in database
GPG key ID: 0512E188DDE4FF2A
2 changed files with 47 additions and 10 deletions

View file

@ -5,18 +5,9 @@ import sinon from 'sinon';
describe('plugins/elasticsearch', function () {
describe('lib/map_uri', function () {
let server;
let request;
beforeEach(function () {
const get = sinon.stub()
.withArgs('elasticsearch.url').returns('http://foobar:9200')
.withArgs('elasticsearch.requestHeadersWhitelist').returns(['x-my-custom-HEADER', 'Authorization']);
const config = function () { return { get: get }; };
server = {
config: config
};
request = {
path: '/elasticsearch/some/path',
headers: {
@ -31,7 +22,16 @@ describe('plugins/elasticsearch', function () {
};
});
it('only keeps the whitelisted request headers', function () {
it('only sends the whitelisted request headers', function () {
const get = sinon.stub()
.withArgs('elasticsearch.url').returns('http://foobar:9200')
.withArgs('elasticsearch.requestHeadersWhitelist').returns(['x-my-custom-HEADER', 'Authorization']);
const config = function () { return { get: get }; };
const server = {
config: config
};
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) {
expect(err).to.be(null);
expect(upstreamHeaders).to.have.property('authorization');
@ -39,5 +39,38 @@ describe('plugins/elasticsearch', function () {
expect(Object.keys(upstreamHeaders).length).to.be(2);
});
});
it('sends no headers if whitelist is set to []', function () {
const get = sinon.stub()
.withArgs('elasticsearch.url').returns('http://foobar:9200')
.withArgs('elasticsearch.requestHeadersWhitelist').returns([]);
const config = function () { return { get: get }; };
const server = {
config: config
};
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) {
expect(err).to.be(null);
expect(Object.keys(upstreamHeaders).length).to.be(0);
});
});
it('sends no headers if whitelist is set to no value', function () {
const get = sinon.stub()
.withArgs('elasticsearch.url').returns('http://foobar:9200')
.withArgs('elasticsearch.requestHeadersWhitelist').returns([ null ]); // This is how Joi returns it
const config = function () { return { get: get }; };
const server = {
config: config
};
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) {
expect(err).to.be(null);
expect(Object.keys(upstreamHeaders).length).to.be(0);
});
});
});
});

View file

@ -3,6 +3,10 @@ import _ from 'lodash';
module.exports = function (originalHeaders, headersToKeep) {
const normalizeHeader = function (header) {
if (!header) {
return '';
}
header = header.toString();
return header.trim().toLowerCase();
};