From 7b9d653c46c2a6e58bf27e615db1862156894631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Thu, 15 Feb 2018 13:33:32 +0100 Subject: [PATCH] rate limit tests structure --- test/unit/cartodb/rate-limit.test.js | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/unit/cartodb/rate-limit.test.js diff --git a/test/unit/cartodb/rate-limit.test.js b/test/unit/cartodb/rate-limit.test.js new file mode 100644 index 00000000..e079b4d3 --- /dev/null +++ b/test/unit/cartodb/rate-limit.test.js @@ -0,0 +1,66 @@ +const assert = require('assert'); +const redis = require('redis'); +const RedisPool = require('redis-mpool'); +const cartodbRedis = require('cartodb-redis'); +const rateLimitMiddleware = require('../../../lib/cartodb/middleware/rate-limit'); + +let redisClient; +const user = 'cdb'; +const endpointGroup = 'tile'; +let keysToDelete = [] + +function setLimit(count, period, burst) { + redisClient.SELECT(8, function(err) { + if (err) { + return; + } + + const key = 'rate-limit:store:' + user + ':' + endpointGroup; + redisClient.hset(key, 'b', burst, 'c', count, 'p', period, function() { + keysToDelete.push(key); + }); + }); +} + +describe.only('rate-limit', function() { + before(function() { + const redisPool = new RedisPool(global.environment.redis); + const metadataBackend = cartodbRedis({pool: redisPool}); + this.rateLimit = rateLimitMiddleware(metadataBackend); + + redisClient = redis.createClient(global.environment.redis.port); + }); + + after(function() { + keysToDelete.forEach( key => { + redisClient.del(key); + }); + }); + + it("should works", function(done) { + const count = 1; + const period = 1; + const burst = 1; + setLimit(count, period, burst); + + const req = {}; + const res = { + headers: {}, + set(headers) { + this.headers = headers; + } + }; + + this.rateLimit(req, res, function(err) { + assert.ok(!err, err); + assert.deepEqual(res.headers, { + "X-Rate-Limit-Limit": burst + 1, + "X-Rate-Limit-Remaining": burst, + "X-Rate-Limit-Reset": period, + "X-Rate-Limit-Retry-After": -1 + }); + + done(); + }) + }) +}) \ No newline at end of file