express endpoint + tests
This commit is contained in:
parent
bf40b5a000
commit
685df45e36
|
@ -13,7 +13,7 @@ var Stats = require('./stats');
|
||||||
|
|
||||||
log.disableColor();
|
log.disableColor();
|
||||||
log.debug = log.verbose;
|
log.debug = log.verbose;
|
||||||
log.level = 'debug';
|
log.level = 'info';
|
||||||
|
|
||||||
var ExpressApp = function() {
|
var ExpressApp = function() {
|
||||||
this.app = express();
|
this.app = express();
|
||||||
|
@ -85,7 +85,8 @@ ExpressApp.prototype.start = function(opts, cb) {
|
||||||
message: err.message,
|
message: err.message,
|
||||||
}).end();
|
}).end();
|
||||||
} else {
|
} else {
|
||||||
var code = 500, message;
|
var code = 500,
|
||||||
|
message;
|
||||||
if (_.isObject(err)) {
|
if (_.isObject(err)) {
|
||||||
code = err.code || err.statusCode;
|
code = err.code || err.statusCode;
|
||||||
message = err.message || err.body;
|
message = err.message || err.body;
|
||||||
|
@ -430,10 +431,25 @@ 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);
|
var server = getServer(req, res);
|
||||||
res.json( { serviceVersion: server.serviceVersion } );
|
res.json({
|
||||||
|
serviceVersion: server.serviceVersion
|
||||||
|
});
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/v1/notifications/', function(req, res) {
|
||||||
|
getServerWithAuth(req, res, function(server) {
|
||||||
|
var opts = {
|
||||||
|
minTs: +Date.now() - (60 * 1000),
|
||||||
|
notificationId: req.query.notificationId,
|
||||||
|
};
|
||||||
|
server.getNotifications(opts, function(err, notifications) {
|
||||||
|
if (err) return returnError(err, res, req);
|
||||||
|
res.json(notifications);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
this.app.use(opts.basePath || '/bws/api', router);
|
this.app.use(opts.basePath || '/bws/api', router);
|
||||||
|
|
||||||
WalletService.initialize(opts, cb);
|
WalletService.initialize(opts, cb);
|
||||||
|
|
|
@ -38,6 +38,22 @@ describe('ExpressApp', function() {
|
||||||
describe('Routes', function() {
|
describe('Routes', function() {
|
||||||
var testPort = 3239;
|
var testPort = 3239;
|
||||||
var testHost = 'http://127.0.0.1';
|
var testHost = 'http://127.0.0.1';
|
||||||
|
var httpServer;
|
||||||
|
|
||||||
|
function start(ExpressApp, done) {
|
||||||
|
var app = new ExpressApp();
|
||||||
|
httpServer = http.Server(app.app);
|
||||||
|
|
||||||
|
app.start(config, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
httpServer.listen(testPort);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
httpServer.close();
|
||||||
|
});
|
||||||
|
|
||||||
it('/v2/wallets', function(done) {
|
it('/v2/wallets', function(done) {
|
||||||
var server = {
|
var server = {
|
||||||
|
@ -49,20 +65,15 @@ describe('ExpressApp', function() {
|
||||||
getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server),
|
getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var app = new TestExpressApp();
|
start(TestExpressApp, function() {
|
||||||
var httpServer = http.Server(app.app);
|
|
||||||
|
|
||||||
app.start(config, function(err) {
|
|
||||||
should.not.exist(err);
|
|
||||||
httpServer.listen(testPort);
|
|
||||||
var requestOptions = {
|
var requestOptions = {
|
||||||
url: testHost + ':' + testPort + config.basePath + '/v1/wallets',
|
url: testHost + ':' + testPort + config.basePath + '/v2/wallets',
|
||||||
headers: {
|
headers: {
|
||||||
'x-identity': 'identity',
|
'x-identity': 'identity',
|
||||||
'x-signature': 'signature'
|
'x-signature': 'signature'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
request(requestOptions, function(err, response, body){
|
request(requestOptions, function(err, response, body) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
response.statusCode.should.equal(200);
|
response.statusCode.should.equal(200);
|
||||||
body.should.equal('{}');
|
body.should.equal('{}');
|
||||||
|
@ -70,6 +81,41 @@ describe('ExpressApp', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('/v1/notifications', function(done) {
|
||||||
|
var clock = sinon.useFakeTimers(1234000, 'Date');
|
||||||
|
|
||||||
|
var server = {
|
||||||
|
getNotifications: sinon.stub().callsArgWith(1, null, {})
|
||||||
|
};
|
||||||
|
var TestExpressApp = proxyquire('../lib/expressapp', {
|
||||||
|
'./server': {
|
||||||
|
initialize: sinon.stub().callsArg(1),
|
||||||
|
getInstanceWithAuth: sinon.stub().callsArgWith(1, null, server),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
start(TestExpressApp, function() {
|
||||||
|
var requestOptions = {
|
||||||
|
url: testHost + ':' + testPort + config.basePath + '/v1/notifications' + '?notificationId=123&minTs=0',
|
||||||
|
headers: {
|
||||||
|
'x-identity': 'identity',
|
||||||
|
'x-signature': 'signature'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
request(requestOptions, function(err, response, body) {
|
||||||
|
should.not.exist(err);
|
||||||
|
response.statusCode.should.equal(200);
|
||||||
|
body.should.equal('{}');
|
||||||
|
server.getNotifications.calledWith({
|
||||||
|
notificationId: '123',
|
||||||
|
minTs: 1234000 - 60000, // override minTs argument with a hardcoded 60 seconds span
|
||||||
|
}).should.be.true;
|
||||||
|
|
||||||
|
clock.restore();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue