fixed tests for host that were too specific to begin with
This commit is contained in:
@ -2,19 +2,23 @@ var Host = require('../../../src/lib/host');
|
|||||||
var _ = require('lodash-node');
|
var _ = require('lodash-node');
|
||||||
var expect = require('expect.js');
|
var expect = require('expect.js');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
var expectSubObject = require('../../utils/expect_sub_object');
|
||||||
|
|
||||||
describe('Host class', function () {
|
var hostDefaults = {
|
||||||
describe('construction', function () {
|
|
||||||
it('properly sets the defaults', function () {
|
|
||||||
var host = new Host();
|
|
||||||
expect(host).to.eql({
|
|
||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
port: 9200,
|
port: 9200,
|
||||||
path: '',
|
path: '',
|
||||||
auth: null,
|
auth: null,
|
||||||
query: {}
|
query: {},
|
||||||
});
|
headers: null
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Host class', function () {
|
||||||
|
describe('construction', function () {
|
||||||
|
it('properly sets the defaults', function () {
|
||||||
|
var host = new Host();
|
||||||
|
expect(host).to.eql(hostDefaults);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('accepts a string for query', function () {
|
it('accepts a string for query', function () {
|
||||||
@ -36,7 +40,7 @@ describe('Host class', function () {
|
|||||||
it('accepts a string for the entire url', function () {
|
it('accepts a string for the entire url', function () {
|
||||||
var host = new Host('john:dude@pizza.com:420/pizza/cheese?shrooms=true');
|
var host = new Host('john:dude@pizza.com:420/pizza/cheese?shrooms=true');
|
||||||
|
|
||||||
expect(host).to.eql({
|
expectSubObject(host, {
|
||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
host: 'pizza.com',
|
host: 'pizza.com',
|
||||||
port: 420,
|
port: 420,
|
||||||
@ -95,14 +99,7 @@ describe('Host class', function () {
|
|||||||
it('ignores anything that\'s not a string or object-y', function () {
|
it('ignores anything that\'s not a string or object-y', function () {
|
||||||
var host = new Host(1234);
|
var host = new Host(1234);
|
||||||
|
|
||||||
expect(host).to.eql({
|
expect(host).to.eql(hostDefaults);
|
||||||
protocol: 'http',
|
|
||||||
host: 'localhost',
|
|
||||||
port: 9200,
|
|
||||||
path: '',
|
|
||||||
auth: null,
|
|
||||||
query: {}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -121,7 +118,7 @@ describe('Host class', function () {
|
|||||||
param: 1
|
param: 1
|
||||||
},
|
},
|
||||||
auth: 'user:pass'
|
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¶m=1');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('ensures that path starts with a forward-slash', function () {
|
it('ensures that path starts with a forward-slash', function () {
|
||||||
|
|||||||
@ -1,18 +1,19 @@
|
|||||||
describe('Http Connector', function () {
|
describe('Http Connector', function () {
|
||||||
|
|
||||||
var expect = require('expect.js');
|
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 nock = require('nock');
|
||||||
var sinon = require('sinon');
|
var sinon = require('sinon');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var ForeverAgent = require('forever-agent');
|
var ForeverAgent = require('forever-agent');
|
||||||
|
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var https = require('https');
|
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 MockRequest = require('../../mocks/request');
|
||||||
var MockIncommingMessage = require('../../mocks/incomming_message');
|
var MockIncommingMessage = require('../../mocks/incomming_message');
|
||||||
|
|
||||||
@ -97,7 +98,7 @@ describe('Http Connector', function () {
|
|||||||
expect(reqParams.path).to.eql('/?user_id=123&jvm=yes');
|
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 con = new HttpConnection(new Host('https://google.com/path/prefix/for/user/1'));
|
||||||
var reqParams = con.makeReqParams({
|
var reqParams = con.makeReqParams({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@ -107,15 +108,8 @@ describe('Http Connector', function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(reqParams).to.eql({
|
expectSubObject(reqParams, {
|
||||||
method: 'GET',
|
|
||||||
protocol: 'https:',
|
|
||||||
auth: null,
|
|
||||||
hostname: 'google.com',
|
|
||||||
port: 443,
|
|
||||||
path: '/path/prefix/for/user/1/items?q=pizza',
|
path: '/path/prefix/for/user/1/items?q=pizza',
|
||||||
headers: undefined,
|
|
||||||
agent: con.agent
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -130,15 +124,8 @@ describe('Http Connector', function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(reqParams).to.eql({
|
expectSubObject(reqParams, {
|
||||||
method: 'PUT',
|
|
||||||
protocol: 'http:',
|
|
||||||
auth: null,
|
|
||||||
hostname: 'google.com',
|
|
||||||
port: 80,
|
|
||||||
path: '/pref-x/stuff?userId=12345&token=42069&q=pizza',
|
path: '/pref-x/stuff?userId=12345&token=42069&q=pizza',
|
||||||
headers: undefined,
|
|
||||||
agent: con.agent
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -157,7 +144,7 @@ describe('Http Connector', function () {
|
|||||||
hostname: 'google.com',
|
hostname: 'google.com',
|
||||||
port: 80,
|
port: 80,
|
||||||
path: '/stuff',
|
path: '/stuff',
|
||||||
headers: undefined,
|
headers: null,
|
||||||
agent: con.agent
|
agent: con.agent
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
12
test/utils/expect_sub_object.js
Normal file
12
test/utils/expect_sub_object.js
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user