add /node endpoint
This commit is contained in:
parent
828dd3ff90
commit
78856257e4
@ -1,20 +1,31 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
|
var _ = bitcore.deps._;
|
||||||
var Block = bitcore.Block;
|
var Block = bitcore.Block;
|
||||||
|
|
||||||
// mocks
|
// mocks
|
||||||
var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
|
|
||||||
var genesisbuf = new Buffer(genesishex, 'hex');
|
var mockBlocks = require('../test/data/blocks');
|
||||||
var mockBlock = new Block(genesisbuf);
|
|
||||||
|
|
||||||
function Blocks() {}
|
function Blocks() {}
|
||||||
|
|
||||||
|
var node;
|
||||||
|
Blocks.setNode = function(aNode) {
|
||||||
|
node = aNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// params
|
// params
|
||||||
Blocks.blockHashParam = function(req, res, next, blockHash) {
|
Blocks.blockHashParam = function(req, res, next, blockHash) {
|
||||||
blockHash.toString(); // TODO: fetch block from service
|
// TODO: fetch block from service
|
||||||
req.block = mockBlock;
|
var block = mockBlocks[blockHash];
|
||||||
|
|
||||||
|
if (_.isUndefined(block)) {
|
||||||
|
res.status(404).send('Block ' + blockHash + ' not found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
req.block = block;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -22,6 +33,7 @@ Blocks.blockHashParam = function(req, res, next, blockHash) {
|
|||||||
Blocks.getBlock = function(req, res) {
|
Blocks.getBlock = function(req, res) {
|
||||||
res.send(req.block.toObject());
|
res.send(req.block.toObject());
|
||||||
};
|
};
|
||||||
|
|
||||||
Blocks.getBlockError = function(req, res) {
|
Blocks.getBlockError = function(req, res) {
|
||||||
res.status(422);
|
res.status(422);
|
||||||
res.send('blockHash parameter must be a 64 digit hex');
|
res.send('blockHash parameter must be a 64 digit hex');
|
||||||
|
|||||||
13
api/controllers/node.js
Normal file
13
api/controllers/node.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
function NodeStatus() {}
|
||||||
|
var node;
|
||||||
|
NodeStatus.setNode = function(aNode) {
|
||||||
|
node = aNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
NodeStatus.getStatus = function(req, res) {
|
||||||
|
res.send(node.status);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = NodeStatus;
|
||||||
@ -2,20 +2,27 @@
|
|||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var Blocks = require('../controllers/blocks');
|
var Blocks = require('../controllers/blocks');
|
||||||
|
var NodeStatus = require('../controllers/node');
|
||||||
|
|
||||||
|
|
||||||
function initRouter(node) {
|
function initRouter(node) {
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
|
|
||||||
|
[NodeStatus, Blocks].forEach(function(controller) {
|
||||||
|
controller.setNode(node);
|
||||||
|
});
|
||||||
|
|
||||||
function mockResponse(req, res) {
|
function mockResponse(req, res) {
|
||||||
res.send({'message': 'This is a mocked response'});
|
res.send({
|
||||||
|
'message': 'This is a mocked response'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// parameter middleware
|
// parameter middleware
|
||||||
router.param('blockHash', Blocks.blockHashParam);
|
router.param('blockHash', Blocks.blockHashParam);
|
||||||
|
|
||||||
// Node routes
|
// Node routes
|
||||||
router.get('/node', mockResponse);
|
router.get('/node', NodeStatus.getStatus);
|
||||||
|
|
||||||
// Block routes
|
// Block routes
|
||||||
router.get('/blocks', mockResponse);
|
router.get('/blocks', mockResponse);
|
||||||
|
|||||||
15
api/test/data/blocks.js
Normal file
15
api/test/data/blocks.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var bitcore = require('bitcore');
|
||||||
|
var Block = bitcore.Block;
|
||||||
|
|
||||||
|
var mockBlocks = {};
|
||||||
|
var blockHexs = require('./blocks.json');
|
||||||
|
blockHexs.map(function(hex) {
|
||||||
|
var block = new Block(new Buffer(hex, 'hex'));
|
||||||
|
return block;
|
||||||
|
}).forEach(function(block) {
|
||||||
|
mockBlocks[block.id] = block;
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mockBlocks;
|
||||||
12
api/test/data/blocks.json
Normal file
12
api/test/data/blocks.json
Normal file
File diff suppressed because one or more lines are too long
@ -10,6 +10,9 @@ var BitcoreHTTP = require('../lib/http');
|
|||||||
describe('BitcoreHTTP', function() {
|
describe('BitcoreHTTP', function() {
|
||||||
|
|
||||||
// mocks
|
// mocks
|
||||||
|
var opts = {
|
||||||
|
port: 1234
|
||||||
|
};
|
||||||
var nodeMock;
|
var nodeMock;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
nodeMock = new EventEmitter();
|
nodeMock = new EventEmitter();
|
||||||
@ -25,7 +28,7 @@ describe('BitcoreHTTP', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('starts', function() {
|
it('starts', function() {
|
||||||
var http = new BitcoreHTTP(nodeMock);
|
var http = new BitcoreHTTP(nodeMock, opts);
|
||||||
http.start.bind(http).should.not.throw();
|
http.start.bind(http).should.not.throw();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -7,19 +7,19 @@ var request = require('supertest');
|
|||||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
|
||||||
var BitcoreHTTP = require('../../lib/http');
|
var BitcoreHTTP = require('../../lib/http');
|
||||||
|
var mockBlocks = require('../data/blocks');
|
||||||
|
|
||||||
describe('BitcoreHTTP v1 blocks routes', function() {
|
describe('BitcoreHTTP v1 blocks routes', function() {
|
||||||
|
|
||||||
// mocks
|
// mocks
|
||||||
|
var b1 = mockBlocks[Object.keys(mockBlocks)[0]];
|
||||||
var nodeMock, app, agent;
|
var nodeMock, app, agent;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
var opts = {
|
|
||||||
port: 1234
|
|
||||||
};
|
|
||||||
nodeMock = new EventEmitter();
|
nodeMock = new EventEmitter();
|
||||||
app = new BitcoreHTTP(nodeMock, opts).app;
|
app = new BitcoreHTTP(nodeMock).app;
|
||||||
agent = request(app);
|
agent = request(app);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/blocks', function() {
|
describe('/blocks', function() {
|
||||||
it('works with default parameters', function(cb) {
|
it('works with default parameters', function(cb) {
|
||||||
agent.get('/v1/blocks/')
|
agent.get('/v1/blocks/')
|
||||||
@ -35,10 +35,15 @@ describe('BitcoreHTTP v1 blocks routes', function() {
|
|||||||
.expect(422)
|
.expect(422)
|
||||||
.expect('blockHash parameter must be a 64 digit hex', cb);
|
.expect('blockHash parameter must be a 64 digit hex', cb);
|
||||||
});
|
});
|
||||||
|
it('returns 404 with non existent block', function(cb) {
|
||||||
|
agent.get('/v1/blocks/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b600000000')
|
||||||
|
.expect(404)
|
||||||
|
.expect('Block 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b600000000 not found', cb);
|
||||||
|
});
|
||||||
it('works with valid blockHash', function(cb) {
|
it('works with valid blockHash', function(cb) {
|
||||||
agent.get('/v1/blocks/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
|
agent.get('/v1/blocks/' + b1.hash)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect('{"header":{"version":1,"prevHash":"0000000000000000000000000000000000000000000000000000000000000000","merkleRoot":"3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a","time":1231006505,"bits":486604799,"nonce":2083236893},"transactions":[{"version":1,"inputs":[{"prevTxId":"0000000000000000000000000000000000000000000000000000000000000000","outputIndex":4294967295,"sequenceNumber":4294967295,"script":"4 0xffff001d 1 0x04 69 0x5468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73"}],"outputs":[{"satoshis":5000000000,"script":"65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG"}],"nLockTime":0}]}', cb);
|
.expect(b1.toJSON(), cb);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
34
api/test/v1/node.js
Normal file
34
api/test/v1/node.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var chai = require('chai');
|
||||||
|
var should = chai.should();
|
||||||
|
var request = require('supertest');
|
||||||
|
|
||||||
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
|
||||||
|
var BitcoreHTTP = require('../../lib/http');
|
||||||
|
|
||||||
|
describe('BitcoreHTTP v1 node routes', function() {
|
||||||
|
|
||||||
|
// mocks
|
||||||
|
var nodeMock, app, agent;
|
||||||
|
beforeEach(function() {
|
||||||
|
nodeMock = new EventEmitter();
|
||||||
|
nodeMock.status = {
|
||||||
|
sync: 0.75,
|
||||||
|
peer_count: 8,
|
||||||
|
version: 'test'
|
||||||
|
};
|
||||||
|
app = new BitcoreHTTP(nodeMock).app;
|
||||||
|
agent = request(app);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('/node', function() {
|
||||||
|
it('works', function(cb) {
|
||||||
|
agent.get('/v1/node/')
|
||||||
|
.expect(200)
|
||||||
|
.expect(nodeMock.status, cb);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user