diff --git a/api/controllers/blocks.js b/api/controllers/blocks.js index 7459d78c..e51266f3 100644 --- a/api/controllers/blocks.js +++ b/api/controllers/blocks.js @@ -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'); diff --git a/api/routes/v1.js b/api/routes/v1.js index d4452f89..7fabad25 100644 --- a/api/routes/v1.js +++ b/api/routes/v1.js @@ -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); diff --git a/api/test/v1/blocks.js b/api/test/v1/blocks.js index d10f63e0..0aeab00c 100644 --- a/api/test/v1/blocks.js +++ b/api/test/v1/blocks.js @@ -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')