diff --git a/dist/routes/v2/blockchain.js b/dist/routes/v2/blockchain.js index 4c287a35..12df7fbd 100644 --- a/dist/routes/v2/blockchain.js +++ b/dist/routes/v2/blockchain.js @@ -168,13 +168,14 @@ router.get("/getBlockchainInfo", config.blockchainRateLimit4, function (req, res return [4 /*yield*/, BitboxHTTP(requestConfig)]; case 2: response = _a.sent(); - res.json(response.data.result); return [3 /*break*/, 4]; case 3: error_3 = _a.sent(); - res.status(500).send(error_3.response.data.error); - return [3 /*break*/, 4]; - case 4: return [2 /*return*/]; + return [2 /*return*/, res.status(500).send(error_3.response.data.error)]; + case 4: + res.json(response.data.result); + res.end(); + return [2 /*return*/]; } }); }); }); diff --git a/package.json b/package.json index 038eb6de..784f36e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rest.bitcoin.com", - "version": "1.10.3", + "version": "1.10.4", "description": "REST API for Bitcoin.com's Cloud", "author": "Gabriel Cardona @ Bitcoin.com", "license": "MIT", @@ -8,10 +8,10 @@ "start": "node ./dist/app.js", "build": "node ./node_modules/gulp/bin/gulp.js build && ./node_modules/typescript/bin/tsc", "dev": "nodemon ./dist/app.js", - "test:panda": "NETWORK=local RPC_BASEURL=http://localhost:18332/ RPC_USERNAME=regtest RPC_PASSWORD=regtest npm test", "test": "npm run build && nyc --reporter=text mocha --require babel-core/register --timeout 15000 test/v1/", "test-v1": "nyc --reporter=text mocha --require babel-core/register --timeout 15000 test/v1/", "test-v2": "nyc --reporter=text mocha --require babel-core/register --timeout 15000 test/v2/", + "test-v2:blockchain": "nyc --reporter=text mocha --require babel-core/register --timeout 15000 test/v2/blockchain", "test-all": "TEST=integration nyc --reporter=text mocha --require babel-core/register --timeout 15000 test/v1/ test/v2/", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --require babel-core/register --timeout 15000" @@ -63,6 +63,7 @@ "node-mocks-http": "^1.7.0", "nodemon": "^1.18.1", "nyc": "^11.6.0", + "pandacash-cli": "^0.4.0", "prettier": "^1.14.2", "sinon": "^6.3.4", "typescript": "^3.1.4" diff --git a/src/routes/v2/blockchain.ts b/src/routes/v2/blockchain.ts index 9d0f4c45..44b9f94a 100644 --- a/src/routes/v2/blockchain.ts +++ b/src/routes/v2/blockchain.ts @@ -155,12 +155,16 @@ router.get( requestConfig.data.method = "getblockchaininfo" requestConfig.data.params = [] + let response; + try { - const response = await BitboxHTTP(requestConfig) - res.json(response.data.result) + response = await BitboxHTTP(requestConfig) } catch (error) { - res.status(500).send(error.response.data.error) + return res.status(500).send(error.response.data.error) } + + res.json(response.data.result); + res.end(); } ) diff --git a/test/v2/blockchain.js b/test/v2/blockchain.js new file mode 100644 index 00000000..ea34f3f2 --- /dev/null +++ b/test/v2/blockchain.js @@ -0,0 +1,75 @@ +"use strict" + +//const chai = require("chai"); +const assert = require("assert") +const httpMocks = require("node-mocks-http") +const panda = require("./helpers/panda"); +process.env.RPC_BASEURL="http://localhost:48332/"; +const blockchainRoute = require("../../dist/routes/v2/blockchain") + +describe("#BlockchainRouter", () => { + // local node will be started in regtest mode on the port 48332 + before(panda.runLocalNode); + after(() => { + // otherwise the panda will run forever + process.exit(); + }); + + describe("#root", () => { + it("should return 'blockchain' for GET /", () => { + const mockRequest = httpMocks.createRequest({ + method: "GET", + url: "/" + }) + + const mockResponse = httpMocks.createResponse() + + blockchainRoute(mockRequest, mockResponse) + + const actualResponseBody = mockResponse._getData() + const expectedResponseBody = { + status: "blockchain" + } + + assert.deepEqual(JSON.parse(actualResponseBody), expectedResponseBody) + }) + }) + + describe("#BlockchainGetBlockchainInfo", () => { + it("should GET /getBlockchainInfo ", (done) => { + const mockRequest = httpMocks.createRequest({ + method: "GET", + url: "/getBlockchainInfo" + }) + + const mockResponse = httpMocks.createResponse({ + eventEmitter: require("events").EventEmitter + }) + + blockchainRoute(mockRequest, mockResponse) + + mockResponse.on("end", () => { + const actualResponseBody = Object.keys( + JSON.parse(mockResponse._getData()) + ) + + assert.deepEqual(actualResponseBody, [ + "chain", + "blocks", + "headers", + "bestblockhash", + "difficulty", + "mediantime", + "verificationprogress", + "chainwork", + "pruned", + "softforks", + "bip9_softforks", + "pruneheight" + ]); + + return done(); + }); + }) + }) +}) diff --git a/test/v2/helpers/panda.js b/test/v2/helpers/panda.js new file mode 100644 index 00000000..69bce571 --- /dev/null +++ b/test/v2/helpers/panda.js @@ -0,0 +1,26 @@ +"use strict" + +/** + * Read more about panda here: https://panda-suite.github.io/ + */ +const panda = require("pandacash-cli") + +const runLocalNode = done => { + const server = panda.server({ + // always the same mnemonic + // mnemonic: "cigar magnet ocean purchase travel damp snack alone theme budget wagon wrong", + seedAccounts: true, + enableLogs: false, + debug: false + }) + + server.listen(48332, (err, pandaCashCore) => { + if (err) return console.error(err) + + done() + }) +} + +module.exports = { + runLocalNode +}