From 3032c7b1bedf55d64b2a1a510de052463b3189de Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Sat, 8 May 2021 18:12:54 -0400 Subject: [PATCH] Fixed unit tests --- src/rpc/rate-limit.js | 2 +- test/unit/json-rpc/a14-rate-limits.js | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/rpc/rate-limit.js b/src/rpc/rate-limit.js index 83637a6..bc03c69 100644 --- a/src/rpc/rate-limit.js +++ b/src/rpc/rate-limit.js @@ -41,7 +41,7 @@ class RateLimit { console.log( `this.rateLimitOptions: ${JSON.stringify(this.rateLimitOptions, null, 2)}` ) - this.rateLimit = this.RateLimitLib.middleware(this.rateLimitOptiolimiterns) + this.rateLimit = this.RateLimitLib.middleware(this.rateLimitOptions) } // This function is called when the user hits their rate limits. diff --git a/test/unit/json-rpc/a14-rate-limits.js b/test/unit/json-rpc/a14-rate-limits.js index 4ae1d92..def07b7 100644 --- a/test/unit/json-rpc/a14-rate-limits.js +++ b/test/unit/json-rpc/a14-rate-limits.js @@ -25,7 +25,42 @@ describe('#rate-limit', () => { }) afterEach(() => sandbox.restore()) + describe('#constructor', () => { + it('should use the options provided', async () => { + try { + const options = { + interval: { min: 10 }, + delayAfter: 1, + timeWait: { sec: 5 }, + max: 2, + onLimitReached: () => { + throw new Error('custom message error') + } + } + const _uut = new RateLimit(options) + // Assert options + assert.equal(_uut.rateLimitOptions.interval.min, options.interval.min) + assert.equal(_uut.rateLimitOptions.delayAfter, options.delayAfter) + assert.equal(_uut.rateLimitOptions.timeWait.sec, options.timeWait.sec) + + const from = 'constructor test' + const firstRequest = await _uut.limiter(from) + assert.isTrue(firstRequest) + + const secondRequest = await _uut.limiter(from) + assert.isTrue(secondRequest) + + await _uut.limiter(from) + assert.fail('unexpected error') + } catch (error) { + assert.include( + error.message, + 'custom message error' + ) + } + }) + }) describe('#onLimitReached', () => { it('should throw error', async () => { try {