UNPKG

tribune

Version:

Holy advocate to the Roman Senate, your Tribune will ensure the Consuls have your interests in mind.

144 lines (110 loc) 4.3 kB
/* global describe it */ // const httpMethods = require('http').METHODS; const nock = require('nock'); const sinon = require('sinon'); const assert = require('assert'); const Tribune = require('../lib/tribune'); const Service = require('../lib/service'); const Endpoint = require('../lib/endpoint'); describe('new Service(tribune, name) -> this', () => { describe('#endpoint(cb(err, endpoint))', () => { const executePreparedQueryResponse = { Service : 'serviceName', DNS : { TTL: '' }, Datacenter : 'dc1', Failovers : 0, Index : 0, KnownLeader: true, LastContact: 0, Nodes: [{ Service: { ID : '__SERVICE_ID__', Address : 'service.url', CreateIndex : 0, EnableTagOverride: false, ModifyIndex : 0, Port : 'service.port', Service : 'serviceName', Tags : ['%24protocol%3Ahttp%5C%3A', '%24routes%3A%2Ffoo%2Fbar%2C%2Fbaz'] } }] }; it('calls back with an endpoint instance', (cb) => { nock('http://agent.url') .get('/v1/query/serviceName/execute') .query({ limit: 1 }) .reply(200, executePreparedQueryResponse); const tribune = new Tribune({ agentUrl: 'http://agent.url' }); const service = new Service(tribune, 'serviceName'); service.endpoint((err, endpoint) => { assert.ifError(err); assert.ok(endpoint instanceof Endpoint); nock.cleanAll(); cb(null); }); }); it('calls back with the cached endpoint if a request was made previously', (cb) => { nock('http://agent.url') .get('/v1/query/serviceName/execute') .query({ limit: 1 }) .reply(200, executePreparedQueryResponse); const tribune = new Tribune({ agentUrl: 'http://agent.url' }); const service = new Service(tribune, 'serviceName'); service.endpoint((err, endpoint) => { const cachedEndpoint = endpoint; assert.ifError(err); service.endpoint((err, endpoint) => { assert.ifError(err); assert.equal(endpoint, cachedEndpoint); nock.cleanAll(); cb(null); }); }); }); it('clears the cached endpoint after the ttl expires', function(cb) { nock('http://agent.url') .get('/v1/query/serviceName/execute') .query({ limit: 1 }) .times(2) .reply(200, executePreparedQueryResponse); const tribune = new Tribune({ agentUrl: 'http://agent.url' }); const service = new Service(tribune, 'serviceName'); this.timeout(tribune._serviceTtl + 300); service.endpoint((err, endpoint) => { const cachedEndpoint = endpoint; assert.ifError(err); setTimeout(() => { service.endpoint((err, endpoint) => { assert.ifError(err); assert.notEqual(endpoint, cachedEndpoint); nock.cleanAll(); cb(null); }); }, tribune._serviceTtl + 100); }); }); }); describe('#request([url], [opts], [cb(err, res)]) -> res', () => { it('calls endpoint and _tribune._consul._request methods to make the request', (cb) => { const tribune = new Tribune({ agentUrl: 'http://agent.url' }); const service = new Service(tribune, 'serviceName'); sinon.stub(service._tribune._consul, '_request').callsArgWith(2, null, { response: 'foo' }); sinon.stub(service, 'endpoint').callsArgWith(0, null, new Endpoint({ Address: 'service.url', ID : '__SERVICE_ID__', Port : 'service.port', Service: 'serviceName', Tags : ['%24protocol%3Ahttp%5C%3A', '%24routes%3A%2Ffoo%2Fbar%2C%2Fbaz'] })); service.request(undefined, {}, (err, res) => { if (err) { return cb(err); } assert.deepEqual(res, { response: 'foo' }); service._tribune._consul._request.restore(); service.endpoint.restore(); cb(null); }); }); it('uses request with the baseUrl set to the endpoint to make the request'); it('attempts a request 10 times before giving up on ENOTFOUND errors'); }); });