Skip to content
This repository was archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from lemonde/support-credentials
Browse files Browse the repository at this point in the history
Support credentials
  • Loading branch information
vvo committed Mar 27, 2014
2 parents d3b715e + df53fee commit 0baa147
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 55 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ client.search('my_index', {

#### oss.createClient(options)

Create a new client, avalaible options are `hostname`, `port` and `protocol`.
Create a new client, avalaible options are
* hostname
* port
* protocol
* login
* key

```js
var client = oss.createClient({
Expand Down
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function Client(options) {

this.options = _.defaults(options || {}, {
hostname: 'localhost',
query: _.pick(options, 'login', 'key'),
port: 9090
});

Expand All @@ -29,7 +30,7 @@ function Client(options) {
Client.prototype.request = function (options, callback) {

var url = urlParser.format(_(this.options)
.pick(['hostname', 'port', 'protocol'])
.pick(['hostname', 'port', 'protocol', 'query'])
.defaults({
protocol: 'http',
pathname: options.pathname
Expand Down
139 changes: 86 additions & 53 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,125 @@ describe('Client', function () {
});

describe('#request', function () {
var client;
describe('without credentials', function () {
var client;

beforeEach(function () {
client = new Client();
beforeEach(function () {
client = new Client();

nock('http://localhost:9090')
.post('/my-path')
.reply(200, {
foo: 'bar'
});
nock('http://localhost:9090')
.post('/my-path')
.reply(200, {
foo: 'bar'
});

nock('http://localhost:9090')
.post('/my-error-path')
.reply(400, 'My error');
nock('http://localhost:9090')
.post('/my-error-path')
.reply(400, 'My error');

nock('http://localhost:9090')
.post('/my-internal-error-path')
.reply(500);
nock('http://localhost:9090')
.post('/my-internal-error-path')
.reply(500);

});
});

it('should be possible to make a request', function (done) {
it('should be possible to make a request', function (done) {

client.request({
pathname: '/my-path',
method: 'POST'
}, function (err, res) {
if (err) return done(err);
client.request({
pathname: '/my-path',
method: 'POST'
}, function (err, res) {
if (err) return done(err);

expect(res).to.deep.equal({
foo: 'bar'
expect(res).to.deep.equal({
foo: 'bar'
});

done();
});
});

it('should handle error correctly', function (done) {

client.request({
pathname: '/my-error-path',
method: 'POST'
}, function (err, res) {
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('My error');
expect(res).to.be.undefined;

done();
done();
});
});
});

it('should handle error correctly', function (done) {
it('should handle OSS internal error correctly', function (done) {

client.request({
pathname: '/my-error-path',
method: 'POST'
}, function (err, res) {
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('My error');
expect(res).to.be.undefined;
client.request({
pathname: '/my-internal-error-path',
method: 'POST'
}, function (err, res) {
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('');
expect(res).to.be.undefined;

done();
done();
});
});
});

it('should handle OSS internal error correctly', function (done) {
describe('when the server is not reachable', function() {

client.request({
pathname: '/my-internal-error-path',
method: 'POST'
}, function (err, res) {
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('');
expect(res).to.be.undefined;
beforeEach(function() {
client = new Client({
hostname: 'localhost',
port: 99999999
});
});

done();
it('gives a usefull error message', function(done) {
client.request({
pathname: '/my-path',
method: 'POST'
}, function(err) {
var msg = 'Cannot connect to OpenSearchServer at http://localhost:99999999/my-path';
expect(err.message).to.equals(msg);
done();
});
});
});
});

describe('when the server is not reachable', function() {
describe('with credentials', function () {
var client;

beforeEach(function() {
beforeEach(function () {
client = new Client({
hostname: 'localhost',
port: 99999999
login: 'mylogin',
key: 'mykey'
});

nock('http://localhost:9090')
.post('/my-path?login=mylogin&key=mykey')
.reply(200, {
foo: 'bar'
});
});

it('gives a usefull error message', function(done) {
it('should be possible to make a request', function (done) {
client.request({
pathname: '/my-path',
method: 'POST'
}, function(err) {
var msg = 'Cannot connect to OpenSearchServer at http://localhost:99999999/my-path';
expect(err.message).to.equals(msg);
}, function (err, res) {
if (err) return done(err);

expect(res).to.deep.equal({
foo: 'bar'
});

done();
});
});
});

});

describe('instances', function () {
Expand Down

0 comments on commit 0baa147

Please sign in to comment.