use config.httpAuth as default auth info for hosts

This commit is contained in:
spalger
2016-11-15 11:52:42 -07:00
parent b744b66687
commit 6d20756712
2 changed files with 18 additions and 1 deletions

View File

@ -100,6 +100,10 @@ function Host(config, globalConfig) {
config = {}; config = {};
} }
if (!config.auth && globalConfig.httpAuth) {
config.auth = globalConfig.httpAuth
}
if (config.auth) { if (config.auth) {
config.headers = config.headers || {}; config.headers = config.headers || {};
config.headers.Authorization = 'Basic ' + btoa(config.auth); config.headers.Authorization = 'Basic ' + btoa(config.auth);

View File

@ -24,6 +24,11 @@ var hostDefaults = {
} }
}; };
var base64 = function (str) {
var buffer = Buffer.from ? Buffer.from(str, 'utf8') : new Buffer(str, 'utf8')
return buffer.toString('base64')
}
describe('Host class', function () { describe('Host class', function () {
describe('construction', function () { describe('construction', function () {
it('properly sets the defaults', function () { it('properly sets the defaults', function () {
@ -120,7 +125,7 @@ describe('Host class', function () {
expect(host.host).to.eql('pizza.com'); expect(host.host).to.eql('pizza.com');
expect(host.port).to.eql(888); expect(host.port).to.eql(888);
expect(host.path).to.eql('/path'); expect(host.path).to.eql('/path');
expect(host.headers).to.eql({ Authorization: 'Basic ' + (new Buffer('joe:diner')).toString('base64') }); expect(host.headers).to.eql({ Authorization: 'Basic ' + base64('joe:diner') });
expect(host.query).to.eql({ expect(host.query).to.eql({
query: 'yes' query: 'yes'
}); });
@ -134,6 +139,14 @@ describe('Host class', function () {
expect(host).to.eql(hostDefaults); expect(host).to.eql(hostDefaults);
}); });
it('defaults auth values from the `httpAuth` setting', function () {
var host = new Host('http://localhost:9200', {
httpAuth: 'username:password'
});
expect(host.headers).to.have.property('Authorization', 'Basic ' + base64('username:password'));
});
}); });
describe('#makeUrl', function () { describe('#makeUrl', function () {