one integration test working
This commit is contained in:
parent
6974c35a05
commit
739e306c50
@ -97,7 +97,9 @@ Blocks.list = function(req, res) {
|
|||||||
// TODO: return block_summary instead of block_full
|
// TODO: return block_summary instead of block_full
|
||||||
node.blockService.listBlocks(from, to, offset, limit)
|
node.blockService.listBlocks(from, to, offset, limit)
|
||||||
.then(function(blocks) {
|
.then(function(blocks) {
|
||||||
res.send(blocks);
|
res.send(blocks.map(function(b) {
|
||||||
|
return b.toObject();
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -110,7 +112,7 @@ Blocks.getLatest = function(req, res) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Blocks.get = function(req, res) {
|
Blocks.get = function(req, res) {
|
||||||
$.checkState(req.block instanceof Block);
|
$.checkState(req.block instanceof Block, JSON.stringify(req.block));
|
||||||
res.send(req.block.toObject());
|
res.send(req.block.toObject());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,15 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BitcoreHTTP = require('../lib/http');
|
var BitcoreHTTP = require('../lib/http');
|
||||||
|
var bitcore = require('bitcore');
|
||||||
|
|
||||||
module.exports = function(nodeMock) {
|
module.exports = function(nodeMock) {
|
||||||
return process.env.INTEGRATION === 'true' ? BitcoreHTTP.create().app : new BitcoreHTTP(nodeMock).app;
|
if (process.env.INTEGRATION === 'true') {
|
||||||
|
var config = require('config');
|
||||||
|
var network = config.get('BitcoreHTTP.BitcoreNode').network;
|
||||||
|
console.log('Starting test suite', network, 'network');
|
||||||
|
bitcore.Networks.defaultNetwork = bitcore.Networks.get(network);
|
||||||
|
return BitcoreHTTP.create(config.get('BitcoreHTTP')).app;
|
||||||
|
}
|
||||||
|
return new BitcoreHTTP(nodeMock).app;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,7 +9,7 @@ blockHexs.map(function(hex) {
|
|||||||
var block = new Block(new Buffer(hex, 'hex'));
|
var block = new Block(new Buffer(hex, 'hex'));
|
||||||
return block;
|
return block;
|
||||||
}).forEach(function(block) {
|
}).forEach(function(block) {
|
||||||
mockBlocks[block.id] = block.toObject();
|
mockBlocks[block.id] = block;
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = mockBlocks;
|
module.exports = mockBlocks;
|
||||||
|
|||||||
@ -54,50 +54,53 @@ describe('BitcoreHTTP v1 blocks routes', function() {
|
|||||||
var end = to - 1e5;
|
var end = to - 1e5;
|
||||||
var section = blockList.slice(start, end);
|
var section = blockList.slice(start, end);
|
||||||
var ret = section.slice(offset, offset + limit);
|
var ret = section.slice(offset, offset + limit);
|
||||||
console.log(ret);
|
|
||||||
return Promise.resolve(ret);
|
return Promise.resolve(ret);
|
||||||
};
|
};
|
||||||
app = require('../app')(nodeMock);
|
app = require('../app')(nodeMock);
|
||||||
agent = request(app);
|
agent = request(app);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.only('/blocks', function() {
|
var toObject = function(b) {
|
||||||
|
return b.toObject();
|
||||||
|
};
|
||||||
|
|
||||||
|
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/')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(JSON.stringify(blockList), cb);
|
.expect(blockList.map(toObject), cb);
|
||||||
});
|
});
|
||||||
it('fails with to<from', function(cb) {
|
it('fails with to<from', function(cb) {
|
||||||
agent.get('/v1/blocks/?from=100000&to=99999')
|
agent.get('/v1/blocks/?from=100000&to=99999')
|
||||||
.expect(422)
|
.expect(422)
|
||||||
.expect('/v1/blocks/ "to" must be >= "from"', cb);
|
.expect('/v1/blocks/ "to" must be >= "from"', cb);
|
||||||
});
|
});
|
||||||
it('works with to/from parameters', function(cb) {
|
it.only('works with to/from parameters', function(cb) {
|
||||||
agent.get('/v1/blocks/?from=100000&to=100001')
|
agent.get('/v1/blocks/?from=100000&to=100001')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(JSON.stringify([firstBlock]), cb);
|
.expect([firstBlock.toObject()], cb);
|
||||||
});
|
});
|
||||||
it('works with limit/offset parameters', function(cb) {
|
it('works with limit/offset parameters', function(cb) {
|
||||||
agent.get('/v1/blocks/?limit=1&offset=1')
|
agent.get('/v1/blocks/?limit=1&offset=1')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(JSON.stringify([secondBlock]), cb);
|
.expect([secondBlock.toObject()], cb);
|
||||||
});
|
});
|
||||||
it('works with all parameters', function(cb) {
|
it('works with all parameters', function(cb) {
|
||||||
agent.get('/v1/blocks/?from=100005&to=100020&limit=3&offset=2')
|
agent.get('/v1/blocks/?from=100005&to=100020&limit=3&offset=2')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(JSON.stringify(last3), cb);
|
.expect(last3.map(toObject), cb);
|
||||||
});
|
});
|
||||||
it('works with all parameters 2', function(cb) {
|
it('works with all parameters 2', function(cb) {
|
||||||
agent.get('/v1/blocks/?from=100000&to=100005&limit=2&offset=2')
|
agent.get('/v1/blocks/?from=100000&to=100005&limit=2&offset=2')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(JSON.stringify(some2), cb);
|
.expect(some2.map(toObject), cb);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('/blocks/latest', function() {
|
describe('/blocks/latest', function() {
|
||||||
it('returns latest block', function(cb) {
|
it('returns latest block', function(cb) {
|
||||||
agent.get('/v1/blocks/latest')
|
agent.get('/v1/blocks/latest')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect(lastBlock, cb);
|
.expect(lastBlock.toObject(), cb);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('/blocks/:blockHash', function() {
|
describe('/blocks/:blockHash', function() {
|
||||||
|
|||||||
@ -91,7 +91,7 @@ BlockService.blockRPCtoBitcore = function(blockData) {
|
|||||||
nonce: blockData.nonce,
|
nonce: blockData.nonce,
|
||||||
bits: new bitcore.deps.bnjs(
|
bits: new bitcore.deps.bnjs(
|
||||||
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
||||||
),
|
).toNumber(),
|
||||||
merkleRoot: bitcore.util.buffer.reverse(
|
merkleRoot: bitcore.util.buffer.reverse(
|
||||||
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
|
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
|
||||||
)
|
)
|
||||||
@ -192,6 +192,7 @@ BlockService.prototype.getBlockByHeight = function(height) {
|
|||||||
* @param {Number} to ditto, but for the upper limit, non inclusive
|
* @param {Number} to ditto, but for the upper limit, non inclusive
|
||||||
* @param {Number} offset skip the first offset blocks
|
* @param {Number} offset skip the first offset blocks
|
||||||
* @param {Number} limit max amount of blocks returned
|
* @param {Number} limit max amount of blocks returned
|
||||||
|
* @return {Array} a list of blocks
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
BlockService.prototype.listBlocks = function(from, to, offset, limit) {
|
BlockService.prototype.listBlocks = function(from, to, offset, limit) {
|
||||||
@ -207,12 +208,12 @@ BlockService.prototype.listBlocks = function(from, to, offset, limit) {
|
|||||||
var blocks = [];
|
var blocks = [];
|
||||||
// TODO: optimize: precompute heights and fetch all blocks in parallel?
|
// TODO: optimize: precompute heights and fetch all blocks in parallel?
|
||||||
var fetchBlock = function(height) {
|
var fetchBlock = function(height) {
|
||||||
if (height > end) {
|
if (height >= end) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return self.getBlockByHeight(height)
|
return self.getBlockByHeight(height)
|
||||||
.then(function(block) {
|
.then(function(block) {
|
||||||
blocks.push(block.toObject());
|
blocks.push(block);
|
||||||
return fetchBlock(height + 1);
|
return fetchBlock(height + 1);
|
||||||
})
|
})
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user