add /blocks/latest

This commit is contained in:
Manuel Araoz 2015-03-06 10:49:27 -03:00
parent 125d7b22f8
commit 91984e89a1
3 changed files with 24 additions and 6 deletions

View File

@ -17,7 +17,9 @@ Blocks.setNode = function(aNode) {
};
// params
/*
* params
*/
Blocks.blockHashParam = function(req, res, next, blockHash) {
// TODO: fetch block from service
var block = mockBlocks[blockHash];
@ -43,11 +45,19 @@ Blocks.heightParam = function(req, res, next, height) {
next();
};
Blocks.getBlock = function(req, res) {
/*
* controllers
*/
Blocks.getLatest = function(req, res) {
req.block = mockBlocks[Object.keys(mockBlocks).splice(-1)[0]];
Blocks.get(req, res);
};
Blocks.get = function(req, res) {
$.checkState(req.block instanceof Block);
res.send(req.block.toObject());
};
Blocks.getBlockError = function(req, res) {
res.status(422);
res.send('/v1/blocks/ parameter must be a 64 digit hex or block height integer');

View File

@ -27,9 +27,9 @@ function initRouter(node) {
// Block routes
router.get('/blocks', mockResponse);
router.get('/blocks/latest', mockResponse);
router.get('/blocks/:blockHash([A-Fa-f0-9]{64})', Blocks.getBlock);
router.get('/blocks/:height([0-9]+)', Blocks.getBlock);
router.get('/blocks/latest', Blocks.getLatest);
router.get('/blocks/:blockHash([A-Fa-f0-9]{64})', Blocks.get);
router.get('/blocks/:height([0-9]+)', Blocks.get);
router.get('/blocks/*', Blocks.getBlockError);
router.get('/blocks/:blockHash/transactions/:txIndex', mockResponse);

View File

@ -13,6 +13,7 @@ describe('BitcoreHTTP v1 blocks routes', function() {
// mocks
var b1 = mockBlocks[Object.keys(mockBlocks)[0]];
var lastBlock = mockBlocks[Object.keys(mockBlocks).splice(-1)[0]];
var nodeMock, app, agent;
beforeEach(function() {
nodeMock = new EventEmitter();
@ -29,6 +30,13 @@ describe('BitcoreHTTP v1 blocks routes', function() {
}, cb);
});
});
describe('/blocks/latest', function() {
it('returns latest block', function(cb) {
agent.get('/v1/blocks/latest')
.expect(200)
.expect(lastBlock.toJSON(), cb);
});
});
describe('/blocks/:blockHash', function() {
it('fails with invalid blockHash', function(cb) {
agent.get('/v1/blocks/abad1dea')