Browse Source

Merge pull request #385 from isocolsky/feat/version-in-header

Add service version to header for all requests
activeAddress
Matias Alejo Garcia 9 years ago
parent
commit
f5cb854b9a
  1. 4
      lib/expressapp.js
  2. 9
      lib/server.js
  3. 12
      test/expressapp.js
  4. 7
      test/integration/server.js

4
lib/expressapp.js

@ -34,6 +34,7 @@ ExpressApp.prototype.start = function(opts, cb) {
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'x-signature,x-identity,x-client-version,X-Requested-With,Content-Type,Authorization'); res.setHeader('Access-Control-Allow-Headers', 'x-signature,x-identity,x-client-version,X-Requested-With,Content-Type,Authorization');
res.setHeader('x-service-version', WalletService.getServiceVersion());
next(); next();
}); });
var allowCORS = function(req, res, next) { var allowCORS = function(req, res, next) {
@ -430,9 +431,8 @@ ExpressApp.prototype.start = function(opts, cb) {
}); });
router.get('/v1/version/', function(req, res) { router.get('/v1/version/', function(req, res) {
var server = getServer(req, res);
res.json({ res.json({
serviceVersion: server.serviceVersion serviceVersion: WalletService.getServiceVersion(),
}); });
res.end(); res.end();
}); });

9
lib/server.js

@ -15,7 +15,6 @@ var Address = Bitcore.Address;
var ClientError = require('./errors/clienterror'); var ClientError = require('./errors/clienterror');
var Errors = require('./errors/errordefinitions'); var Errors = require('./errors/errordefinitions');
var Package = require('../package');
var Utils = require('./utils'); var Utils = require('./utils');
var Lock = require('./lock'); var Lock = require('./lock');
@ -33,6 +32,7 @@ var storage;
var blockchainExplorer; var blockchainExplorer;
var blockchainExplorerOpts; var blockchainExplorerOpts;
var messageBroker; var messageBroker;
var serviceVersion;
var MAX_KEYS = 100; var MAX_KEYS = 100;
@ -50,7 +50,12 @@ function WalletService() {
this.blockchainExplorerOpts = blockchainExplorerOpts; this.blockchainExplorerOpts = blockchainExplorerOpts;
this.messageBroker = messageBroker; this.messageBroker = messageBroker;
this.notifyTicker = 0; this.notifyTicker = 0;
this.serviceVersion = 'bws-' + Package.version; };
WalletService.getServiceVersion = function() {
if (!serviceVersion)
serviceVersion = 'bws-' + require('../package').version;
return serviceVersion;
}; };

12
test/expressapp.js

@ -57,7 +57,7 @@ describe('ExpressApp', function() {
it('/v2/wallets', function(done) { it('/v2/wallets', function(done) {
var server = { var server = {
getStatus: sinon.stub().callsArgWith(1, null, {}) getStatus: sinon.stub().callsArgWith(1, null, {}),
}; };
var TestExpressApp = proxyquire('../lib/expressapp', { var TestExpressApp = proxyquire('../lib/expressapp', {
'./server': { './server': {
@ -73,9 +73,11 @@ describe('ExpressApp', function() {
'x-signature': 'signature' 'x-signature': 'signature'
} }
}; };
request(requestOptions, function(err, response, body) { request(requestOptions, function(err, res, body) {
should.not.exist(err); should.not.exist(err);
response.statusCode.should.equal(200); should.exist(res.headers['x-service-version']);
res.headers['x-service-version'].should.equal('bws-' + require('../package').version);
res.statusCode.should.equal(200);
body.should.equal('{}'); body.should.equal('{}');
done(); done();
}); });
@ -102,9 +104,9 @@ describe('ExpressApp', function() {
'x-signature': 'signature' 'x-signature': 'signature'
} }
}; };
request(requestOptions, function(err, response, body) { request(requestOptions, function(err, res, body) {
should.not.exist(err); should.not.exist(err);
response.statusCode.should.equal(200); res.statusCode.should.equal(200);
body.should.equal('{}'); body.should.equal('{}');
server.getNotifications.calledWith({ server.getNotifications.calledWith({
notificationId: '123', notificationId: '123',

7
test/integration/server.js

@ -813,13 +813,18 @@ describe('Wallet service', function() {
}); });
describe('#getServiceVersion', function() {
it('should get version from package', function() {
WalletService.getServiceVersion().should.equal('bws-' + require('../../package').version);
});
});
describe('#getInstance', function() { describe('#getInstance', function() {
it('should get server instance', function() { it('should get server instance', function() {
var server = WalletService.getInstance({ var server = WalletService.getInstance({
clientVersion: 'bwc-0.0.1', clientVersion: 'bwc-0.0.1',
}); });
server.clientVersion.should.equal('bwc-0.0.1'); server.clientVersion.should.equal('bwc-0.0.1');
server.serviceVersion.indexOf('bws-').should.equal(0);
}); });
}); });

Loading…
Cancel
Save