UNPKG

service-router

Version:

router to bounce requests to either api or web servers

121 lines (111 loc) 3.84 kB
var http = require('http'); var inspect = require('eyespect').inspector(); var assert = require('assert'); var should = require('should'); var request = require('request'); var path = require('path'); var fs = require('fs'); var seaport = require('seaport'); var configFilePath = path.join(__dirname, 'local_config.json'); var config = require('docparse-config')(configFilePath); var configFileExists = fs.existsSync(configFilePath); assert.ok(configFileExists, 'config json file not found at path: ' + configFilePath); describe('Docparse router', function() { var router; it('should start router', function(done) { router = require('../index')(config); should.exist(router,'router server not returned'); done(); }); it('should return 500 status code for unavailable service', function(done) { var url = 'http://localhost:' + config.get('application:port') + '/api/test'; var opts = { json: true, url: url } request(opts, function (err, res, body) { should.not.exist(err); res.statusCode.should.eql(500); body.should.have.property('message'); body.should.have.property('error'); body.error.should.eql('no hosts available'); done(); }); }); it('should bounce to api service', function(done) { // register an "api" service createApiServer(router, function (err) { should.not.exist(err) var url = 'http://localhost:' + config.get('application:port') + '/api/test'; var opts = { json: true, url: url } request(opts, function (err, res, body) { should.not.exist(err); res.statusCode.should.eql(200); body.should.eql('Hello API World') done(); }); }); }); it('should bounce to web service', function(done) { // register an "api" service createWebServer(router, function (err) { should.not.exist(err) var url = 'http://localhost:' + config.get('application:port') + '/'; var opts = { json: true, url: url } request(opts, function (err, res, body) { should.not.exist(err); res.statusCode.should.eql(200); body.should.eql('Hello Web World') done(); }); }); }); }); function createWebServer(router, cb) { // Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello Web World"); }); var ports = seaport.connect(config.get('seaport:host'), config.get('seaport:port')); var service_name = 'web' var service_version = '0.0.1' // the semver service name to register with seaport when this server spins up var service = service_name + '@' + service_version; var port = ports.register(service_name) server.listen(port, function (err, reply) { router.on('register', function(service) { var role = service.role; if (role === 'web') { cb(); } }); }); } function createApiServer(router, cb) { // Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello API World"); }); var ports = seaport.connect(config.get('seaport:host'), config.get('seaport:port')); var service_name = 'api' var service_version = '0.0.1' // the semver service name to register with seaport when this server spins up var service = service_name + '@' + service_version; var port = ports.register(service_name) server.listen(port, function (err, reply) { router.on('register', function(service) { var role = service.role; if (role === 'api') { cb(); } }); }); }