Skip to content

Commit

Permalink
fixed tests for host that were too specific to begin with
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Alger committed Apr 2, 2014
1 parent 47f0e52 commit b1ee674
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
33 changes: 15 additions & 18 deletions test/unit/specs/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ var Host = require('../../../src/lib/host');
var _ = require('lodash-node');
var expect = require('expect.js');
var url = require('url');
var expectSubObject = require('../../utils/expect_sub_object');

var hostDefaults = {
protocol: 'http',
host: 'localhost',
port: 9200,
path: '',
auth: null,
query: {},
headers: null
};

describe('Host class', function () {
describe('construction', function () {
it('properly sets the defaults', function () {
var host = new Host();
expect(host).to.eql({
protocol: 'http',
host: 'localhost',
port: 9200,
path: '',
auth: null,
query: {}
});
expect(host).to.eql(hostDefaults);
});

it('accepts a string for query', function () {
Expand All @@ -36,7 +40,7 @@ describe('Host class', function () {
it('accepts a string for the entire url', function () {
var host = new Host('john:[email protected]:420/pizza/cheese?shrooms=true');

expect(host).to.eql({
expectSubObject(host, {
protocol: 'http',
host: 'pizza.com',
port: 420,
Expand Down Expand Up @@ -95,14 +99,7 @@ describe('Host class', function () {
it('ignores anything that\'s not a string or object-y', function () {
var host = new Host(1234);

expect(host).to.eql({
protocol: 'http',
host: 'localhost',
port: 9200,
path: '',
auth: null,
query: {}
});
expect(host).to.eql(hostDefaults);
});
});

Expand All @@ -121,7 +118,7 @@ describe('Host class', function () {
param: 1
},
auth: 'user:pass'
})).to.be('http://user:pass@localhost:9200/prefix/this and that?param=1&user_id=123');
})).to.be('http://user:pass@localhost:9200/prefix/this and that?user_id=123&param=1');
});

it('ensures that path starts with a forward-slash', function () {
Expand Down
33 changes: 10 additions & 23 deletions test/unit/specs/http_connector.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
describe('Http Connector', function () {

var expect = require('expect.js');
var Host = require('../../../src/lib/host');
var errors = require('../../../src/lib/errors');
var HttpConnection = require('../../../src/lib/connectors/http');
var ConnectionAbstract = require('../../../src/lib/connection');
var nock = require('nock');
var sinon = require('sinon');
var util = require('util');
var ForeverAgent = require('forever-agent');

var http = require('http');
var https = require('https');

var Host = require('../../../src/lib/host');
var errors = require('../../../src/lib/errors');
var HttpConnection = require('../../../src/lib/connectors/http');
var ConnectionAbstract = require('../../../src/lib/connection');

var expectSubObject = require('../../utils/expect_sub_object');
var MockRequest = require('../../mocks/request');
var MockIncommingMessage = require('../../mocks/incomming_message');

Expand Down Expand Up @@ -97,7 +98,7 @@ describe('Http Connector', function () {
expect(reqParams.path).to.eql('/?user_id=123&jvm=yes');
});

it('merges the path prefex', function () {
it('merges the path prefix', function () {
var con = new HttpConnection(new Host('https://google.com/path/prefix/for/user/1'));
var reqParams = con.makeReqParams({
method: 'GET',
Expand All @@ -107,15 +108,8 @@ describe('Http Connector', function () {
}
});

expect(reqParams).to.eql({
method: 'GET',
protocol: 'https:',
auth: null,
hostname: 'google.com',
port: 443,
expectSubObject(reqParams, {
path: '/path/prefix/for/user/1/items?q=pizza',
headers: undefined,
agent: con.agent
});
});

Expand All @@ -130,15 +124,8 @@ describe('Http Connector', function () {
}
});

expect(reqParams).to.eql({
method: 'PUT',
protocol: 'http:',
auth: null,
hostname: 'google.com',
port: 80,
expectSubObject(reqParams, {
path: '/pref-x/stuff?userId=12345&token=42069&q=pizza',
headers: undefined,
agent: con.agent
});
});

Expand All @@ -157,7 +144,7 @@ describe('Http Connector', function () {
hostname: 'google.com',
port: 80,
path: '/stuff',
headers: undefined,
headers: null,
agent: con.agent
});
});
Expand Down
12 changes: 12 additions & 0 deletions test/utils/expect_sub_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var _ = require('lodash-node');
var expect = require('expect.js');
module.exports = function expectSubObject(obj, subObj) {
_.forOwn(subObj, function (val, prop) {
if (typeof obj[prop] === 'object') {
// non-strict equals
expect(obj[prop]).to.eql(val, 'Expected property' + prop + ' of object to equal ' + val);
} else {
expect(obj).property(prop, val);
}
});
};

0 comments on commit b1ee674

Please sign in to comment.