[connector/angular] don't modify global header deaults, fixed #146
This commit is contained in:
34
src/lib/connectors/angular.js
vendored
34
src/lib/connectors/angular.js
vendored
@ -10,25 +10,37 @@ var _ = require('../utils');
|
||||
var ConnectionAbstract = require('../connection');
|
||||
var ConnectionFault = require('../errors').ConnectionFault;
|
||||
|
||||
function makeAuthHeader(auth) {
|
||||
return 'Basic ' + (new Buffer(auth, 'utf8')).toString('base64');
|
||||
}
|
||||
|
||||
function AngularConnector(host, config) {
|
||||
ConnectionAbstract.call(this, host, config);
|
||||
var connector = this;
|
||||
var self = this;
|
||||
self.headerDefaults = {};
|
||||
|
||||
if (self.host.auth) {
|
||||
self.headerDefaults.Authorization = makeAuthHeader(self.host.auth);
|
||||
}
|
||||
|
||||
config.$injector.invoke(['$http', '$q', function ($http, $q) {
|
||||
connector.$q = $q;
|
||||
connector.$http = $http;
|
||||
|
||||
if (connector.host.auth) {
|
||||
connector.$http.defaults.headers.common.Authorization = 'Basic ' + (new Buffer(connector.host.auth, 'utf8')).toString('base64');
|
||||
}
|
||||
self.$q = $q;
|
||||
self.$http = $http;
|
||||
}]);
|
||||
|
||||
|
||||
}
|
||||
_.inherits(AngularConnector, ConnectionAbstract);
|
||||
|
||||
AngularConnector.prototype.request = function (params, cb) {
|
||||
AngularConnector.prototype.request = function (userParams, cb) {
|
||||
var abort = this.$q.defer();
|
||||
var params = _.cloneDeep(userParams);
|
||||
|
||||
params.headers = _.defaults(params.headers || {}, this.headerDefaults);
|
||||
if (params.auth) {
|
||||
params.headers.Authorization = makeAuthHeader(params.auth);
|
||||
}
|
||||
|
||||
// inform the host not to use the auth, by overriding it in the params
|
||||
params.auth = false;
|
||||
|
||||
this.$http({
|
||||
method: params.method,
|
||||
@ -51,4 +63,4 @@ AngularConnector.prototype.request = function (params, cb) {
|
||||
return function () {
|
||||
abort.resolve();
|
||||
};
|
||||
};
|
||||
};
|
||||
@ -125,7 +125,7 @@ Host.prototype.makeUrl = function (params) {
|
||||
var auth = '';
|
||||
if (params.auth) {
|
||||
auth = params.auth + '@';
|
||||
} else if (this.auth) {
|
||||
} else if (this.auth && params.auth !== false) {
|
||||
auth = this.auth + '@';
|
||||
}
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
<!-- libs -->
|
||||
<script src="jquery.js"></script>
|
||||
<script src="angular.js"></script>
|
||||
<script src="angular-mocks.js"></script>
|
||||
|
||||
<!-- builds -->
|
||||
<script src="elasticsearch.js"></script>
|
||||
|
||||
207
test/unit/browser_builds/angular.js
vendored
207
test/unit/browser_builds/angular.js
vendored
@ -1,7 +1,7 @@
|
||||
/* jshint browser:true */
|
||||
/* global angular */
|
||||
|
||||
var _ = require('lodash-node');
|
||||
var expect = require('expect.js');
|
||||
var Promise = require('bluebird');
|
||||
var sinon = require('sinon');
|
||||
|
||||
describe('Angular esFactory', function () {
|
||||
before(function () {
|
||||
@ -9,110 +9,137 @@ describe('Angular esFactory', function () {
|
||||
});
|
||||
|
||||
var uuid = (function () { var i = 0; return function () { return ++i; }; }());
|
||||
var esFactory;
|
||||
var $http;
|
||||
var $rootScope;
|
||||
var $httpBackend;
|
||||
|
||||
/**
|
||||
* Perform promise based async code in a way that mocha will understand
|
||||
* @param {Function} cb - node style callback
|
||||
* @param {Function} body - function that executes async and returns a promise/value
|
||||
*/
|
||||
var prom = function (cb, body) {
|
||||
expect(cb).to.be.a('function');
|
||||
expect(body).to.be.a('function');
|
||||
function bootstrap(env) {
|
||||
beforeEach(function () {
|
||||
var promiseProvider = _.noop;
|
||||
if (env.bluebirdPromises) {
|
||||
promiseProvider = function ($provide) {
|
||||
$provide.service('$q', function () {
|
||||
return {
|
||||
defer: function () {
|
||||
return _.bindAll(Promise.defer(), ['resolve', 'reject']);
|
||||
},
|
||||
reject: Promise.reject,
|
||||
when: Promise.resolve,
|
||||
all: Promise.all
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
var promise = body();
|
||||
expect(promise.then).to.be.a('function');
|
||||
promise.then(function () { cb(); }, cb);
|
||||
};
|
||||
|
||||
function directive(makeDirective) {
|
||||
var root = document.createElement('div');
|
||||
root.setAttribute('ng-controller', 'empty-controller');
|
||||
var id = uuid();
|
||||
root.setAttribute('test-directive-' + id, 'test-directive');
|
||||
document.body.appendChild(root);
|
||||
|
||||
after(function () {
|
||||
document.body.removeChild(root);
|
||||
root = null;
|
||||
angular.mock.module(promiseProvider, 'elasticsearch');
|
||||
});
|
||||
|
||||
angular
|
||||
.module('mod' + id, ['elasticsearch'])
|
||||
.controller('empty-controller', function () {})
|
||||
.directive('testDirective' + id, makeDirective);
|
||||
|
||||
angular.bootstrap(root, ['mod' + id]);
|
||||
beforeEach(angular.mock.inject(function ($injector) {
|
||||
$http = $injector.get('$http');
|
||||
esFactory = $injector.get('esFactory');
|
||||
$rootScope = $injector.get('$rootScope');
|
||||
$httpBackend = $injector.get('$httpBackend');
|
||||
}));
|
||||
}
|
||||
|
||||
it('is available in the elasticsearch module', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
expect(esFactory).to.be.a('function');
|
||||
done();
|
||||
};
|
||||
describe('basic', function () {
|
||||
bootstrap({
|
||||
bluebirdPromises: true
|
||||
});
|
||||
|
||||
it('is available in the elasticsearch module', function () {
|
||||
expect(esFactory).to.be.a('function');
|
||||
});
|
||||
|
||||
it('has Transport and ConnectionPool properties', function () {
|
||||
expect(esFactory).to.have.property('Transport');
|
||||
expect(esFactory).to.have.property('ConnectionPool');
|
||||
});
|
||||
|
||||
it('returns a new client when it is called', function () {
|
||||
var client = esFactory({
|
||||
hosts: null
|
||||
});
|
||||
|
||||
expect(client).to.have.keys('transport');
|
||||
expect(client.transport).to.be.a(esFactory.Transport);
|
||||
client.close();
|
||||
});
|
||||
|
||||
it('returns an error created by calling a method incorrectly', function () {
|
||||
var client = esFactory({ hosts: null });
|
||||
var err;
|
||||
|
||||
var prom = client.get().then(function () {
|
||||
throw new Error('expected request to fail');
|
||||
}, function (err) {
|
||||
expect(err).to.have.property('message');
|
||||
expect(err.message).to.match(/unable/i);
|
||||
});
|
||||
|
||||
$rootScope.$apply();
|
||||
return prom;
|
||||
});
|
||||
});
|
||||
|
||||
it('has Transport and ConnectionPool properties', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
expect(esFactory).to.have.property('Transport');
|
||||
expect(esFactory).to.have.property('ConnectionPool');
|
||||
done();
|
||||
};
|
||||
describe('ping', function () {
|
||||
bootstrap({
|
||||
bluebirdPromises: true
|
||||
});
|
||||
|
||||
it('works', function () {
|
||||
$httpBackend.expect('HEAD', 'http://some-es-host.com/').respond(200);
|
||||
|
||||
var client = esFactory({
|
||||
host: 'http://some-es-host.com/'
|
||||
});
|
||||
|
||||
var connection = client.transport.connectionPool.getConnections().pop();
|
||||
var stub = sinon.stub(connection, '$http', function (config) {
|
||||
process.nextTick($httpBackend.flush);
|
||||
return $http(config);
|
||||
});
|
||||
|
||||
return client.ping();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a new client when it is called', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
try {
|
||||
var client = esFactory({
|
||||
hosts: null
|
||||
});
|
||||
describe('$http', function () {
|
||||
bootstrap({
|
||||
bluebirdPromises: true
|
||||
});
|
||||
|
||||
expect(client).to.have.keys('transport');
|
||||
expect(client.transport).to.be.a(esFactory.Transport);
|
||||
client.close();
|
||||
} catch (e) {
|
||||
return done(e);
|
||||
it('uses the auth header provided', function () {
|
||||
var authString = 'user:password';
|
||||
var authHeader = 'Basic ' + (new Buffer(authString, 'utf8')).toString('base64');
|
||||
var $httpParams = null;
|
||||
var client = esFactory({
|
||||
host: {
|
||||
host: 'some-other-es-host.com',
|
||||
auth: authString
|
||||
}
|
||||
done();
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error created by calling a method incorrectly', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
prom(done, function () {
|
||||
var client = esFactory({ hosts: null });
|
||||
return client.get().then(function () {
|
||||
expect.fail('promise should have been rejected');
|
||||
}, function (err) {
|
||||
expect(err.message).to.match(/unable/i);
|
||||
});
|
||||
// once the client calls the $http method, flush the requests and trigger an
|
||||
// error if the expected request was not made
|
||||
var connection = client.transport.connectionPool.getConnections().pop();
|
||||
var stub = sinon.stub(connection, '$http', function (params) {
|
||||
$httpParams = params;
|
||||
return Promise.resolve({
|
||||
data: null,
|
||||
status: 200,
|
||||
headers: function () {
|
||||
return {};
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('ping\'s properly', function (done) {
|
||||
directive(function (esFactory) {
|
||||
return function () {
|
||||
prom(done, function () {
|
||||
var client = esFactory({
|
||||
hosts: 'not-a-valid-es-host.es'
|
||||
});
|
||||
|
||||
return client.ping().then(function () {
|
||||
expect.fail('promise should have been rejected');
|
||||
}, function (err) {
|
||||
// this error should be "NoConnections", but in some browsers it will be a Timeout due to testing proxy or because it's IE
|
||||
expect(err).to.be.ok();
|
||||
});
|
||||
});
|
||||
};
|
||||
var prom = client.ping();
|
||||
return prom.then(function () {
|
||||
expect($httpParams).to.have.property('headers');
|
||||
expect($httpParams.headers).to.have.property('Authorization', authHeader);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user