From f016c050e38cf67828299ac9cfe16711cd108439 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 11 Aug 2019 08:41:42 -0700 Subject: [PATCH] Fixed rate-limit integration tests --- test/v3/integration/rate-limits.js | 53 ++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/test/v3/integration/rate-limits.js b/test/v3/integration/rate-limits.js index 8540797..e487d2d 100644 --- a/test/v3/integration/rate-limits.js +++ b/test/v3/integration/rate-limits.js @@ -1,4 +1,7 @@ /* + This integration tests should be run against a live bch-api REST server. It + tests to ensure the rate-limits are working as expected. Adjust the values + in the tests below to match the rate limit setting in your own installation. */ "use strict" @@ -15,10 +18,10 @@ const SERVER = `http://192.168.0.36:12400/v3/` //const SERVER = `http://localhost:3000/v2/` describe("#rate limits", () => { - it("should get control/getInfo() with no auth", async () => { + it("should get control/getNetworkInfo() with no auth", async () => { const options = { method: "GET", - url: `${SERVER}control/getInfo` + url: `${SERVER}control/getNetworkInfo` } const result = await axios(options) @@ -29,16 +32,16 @@ describe("#rate limits", () => { assert.hasAnyKeys(result.data, ["version"]) }) - it("should trigger rate-limit handler if rate limits exceeds 60 request per minute", async () => { + it("should trigger rate-limit handler if rate limits exceeds 30 request per minute", async () => { try { // Actual rate limit is 60 per minute X 4 nodes = 240 rpm. const options = { method: "GET", - url: `${SERVER}control/getInfo` + url: `${SERVER}control/getNetworkInfo` } const promises = [] - for (let i = 0; i < 250; i++) { + for (let i = 0; i < 30; i++) { const promise = axios(options) promises.push(promise) } @@ -68,12 +71,12 @@ describe("#rate limits", () => { const options = { method: "GET", - url: `${SERVER}control/getInfo`, + url: `${SERVER}control/getNetworkInfo`, headers: { Authorization: readyCredential } } const promises = [] - for (let i = 0; i < 250; i++) { + for (let i = 0; i < 30; i++) { const promise = axios(options) promises.push(promise) } @@ -91,4 +94,40 @@ describe("#rate limits", () => { ) } }) + + it("should trigger rate-limit handler if rate limits exceeds pro-tier limit", async () => { + try { + const username = "BITBOX" + + // Pro-tier is accessed by using the right password. + const password = "BITBOX" + //const password = "something" + + const combined = `${username}:${password}` + const base64Credential = Buffer.from(combined).toString("base64") + const readyCredential = `Basic ${base64Credential}` + + // Actual rate limit is 60 per minute X 4 nodes = 240 rpm. + const options = { + method: "GET", + url: `${SERVER}control/getNetworkInfo`, + headers: { Authorization: readyCredential } + } + + const promises = [] + for (let i = 0; i < 80; i++) { + const promise = axios(options) + promises.push(promise) + } + + await Promise.all(promises) + + assert.equal(true, false, "Unexpected result!") + } catch (err) { + //console.log(`err.response: ${util.inspect(err.response)}`) + + assert.equal(err.response.status, 429) + assert.include(err.response.data.error, "Too many requests") + } + }) })