File size: 890 Bytes
cf30fac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import test from 'node:test'
import assert from 'node:assert/strict'
import { checkRateLimit } from './rate-limit.js'
test('rate limiter allows requests within threshold', () => {
const ip = 'test-ip-1'
const result1 = checkRateLimit(ip, 3, 1000)
assert.equal(result1.allowed, true)
assert.equal(result1.remaining, 2)
const result2 = checkRateLimit(ip, 3, 1000)
assert.equal(result2.allowed, true)
assert.equal(result2.remaining, 1)
const result3 = checkRateLimit(ip, 3, 1000)
assert.equal(result3.allowed, true)
assert.equal(result3.remaining, 0)
})
test('rate limiter blocks requests exceeding threshold', () => {
const ip = 'test-ip-blocked'
checkRateLimit(ip, 2, 5000)
checkRateLimit(ip, 2, 5000)
const blocked = checkRateLimit(ip, 2, 5000)
assert.equal(blocked.allowed, false)
assert.equal(blocked.remaining, 0)
assert.ok(blocked.resetMs > 0)
})
|