fix(route-utils.js): getAxiosOptions() replaces setEnvVars()

This commit is contained in:
Chris Troutner
2020-01-27 14:50:00 -08:00
parent 5a13ba114f
commit 725adc1ce1
3 changed files with 49 additions and 18 deletions
+19 -10
View File
@@ -56,18 +56,27 @@ function root(req, res, next) {
*/
async function getBestBlockHash(req, res, next) {
try {
const {
BitboxHTTP,
username,
password,
requestConfig
} = routeUtils.setEnvVars()
// const {
// BitboxHTTP,
// username,
// password,
// requestConfig
// } = routeUtils.setEnvVars()
//
// requestConfig.data.id = "getbestblockhash"
// requestConfig.data.method = "getbestblockhash"
// requestConfig.data.params = []
//
// const response = await BitboxHTTP(requestConfig)
requestConfig.data.id = "getbestblockhash"
requestConfig.data.method = "getbestblockhash"
requestConfig.data.params = []
const options = routeUtils.getAxiosOptions()
options.data.id = "getbestblockhash"
options.data.method = "getbestblockhash"
options.data.params = []
// const response = await axios.post(options.baseURL, options)
const response = await axios.request(options)
const response = await BitboxHTTP(requestConfig)
return res.json(response.data.result)
} catch (err) {
// Attempt to decode the error message.
+20 -2
View File
@@ -17,7 +17,8 @@ module.exports = {
validateNetwork, // Prevents a common user error
setEnvVars, // Allows RPC variables to be set dynamically based on changing env vars.
decodeError, // Extract and interpret error messages.
validateArraySize // Ensure the passed array meets rate limiting requirements.
validateArraySize, // Ensure the passed array meets rate limiting requirements.
getAxiosOptions
}
// This function expects the Request Express.js object and an array as input.
@@ -74,7 +75,8 @@ function validateNetwork(addr) {
// Dynamically set these based on env vars. Allows unit testing.
function setEnvVars() {
const BitboxHTTP = axios.create({
baseURL: process.env.RPC_BASEURL
baseURL: process.env.RPC_BASEURL,
timeout: 15000
})
const username = process.env.RPC_USERNAME
const password = process.env.RPC_PASSWORD
@@ -93,6 +95,22 @@ function setEnvVars() {
return { BitboxHTTP, username, password, requestConfig }
}
// Axios options used when calling axios.post() to talk with a full node.
function getAxiosOptions() {
return {
method: "post",
baseURL: process.env.RPC_BASEURL,
timeout: 15000,
auth: {
username: process.env.RPC_USERNAME,
password: process.env.RPC_PASSWORD
},
data: {
jsonrpc: "1.0"
}
}
}
// Error messages returned by a full node can be burried pretty deep inside the
// error object returned by Axios. This function attempts to extract and interpret
// error messages.
+10 -6
View File
@@ -115,16 +115,20 @@ describe("#BlockchainRouter", () => {
)
})
// it("return proper error connection is refused", async () => {
//
// })
it("should GET /getBestBlockHash", async () => {
// Mock the RPC call for unit tests.
if (process.env.TEST === "unit") {
nock(`${process.env.RPC_BASEURL}`)
.post(uri => uri.includes("/"))
.reply(200, { result: mockData.mockBlockHash })
}
// if (process.env.TEST === "unit") {
// nock(`${process.env.RPC_BASEURL}`)
// .post(uri => uri.includes("/"))
// .reply(200, { result: mockData.mockBlockHash })
// }
const result = await getBestBlockHash(req, res)
//console.log(`result: ${util.inspect(result)}`)
console.log(`result: ${util.inspect(result)}`)
assert.isString(result)
assert.equal(result.length, 64, "Hash string is fixed length")