|
| 1 | +import 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { RequestHandlerHelper } from '../src/client-mixins/request-handler.helper'; |
| 4 | +import type { IncomingMessage } from 'http'; |
| 5 | + |
| 6 | +class TestRequestHandlerHelper extends RequestHandlerHelper<any> { |
| 7 | + public parse(res: IncomingMessage) { |
| 8 | + return this.getRateLimitFromResponse(res); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +describe('Rate limit parsing', () => { |
| 13 | + it('includes user 24-hour limits when present', () => { |
| 14 | + const helper = new TestRequestHandlerHelper({ |
| 15 | + url: new URL('https://example.com'), |
| 16 | + options: {}, |
| 17 | + } as any); |
| 18 | + |
| 19 | + const res = { |
| 20 | + headers: { |
| 21 | + 'x-rate-limit-limit': '15', |
| 22 | + 'x-rate-limit-remaining': '14', |
| 23 | + 'x-rate-limit-reset': '100', |
| 24 | + 'x-app-limit-24hour-limit': '5000', |
| 25 | + 'x-app-limit-24hour-remaining': '4999', |
| 26 | + 'x-app-limit-24hour-reset': '200', |
| 27 | + 'x-user-limit-24hour-limit': '1000', |
| 28 | + 'x-user-limit-24hour-remaining': '999', |
| 29 | + 'x-user-limit-24hour-reset': '300', |
| 30 | + }, |
| 31 | + } as unknown as IncomingMessage; |
| 32 | + |
| 33 | + const rateLimit = helper.parse(res)!; |
| 34 | + expect(rateLimit.limit).to.equal(15); |
| 35 | + expect(rateLimit.day).to.deep.equal({ limit: 5000, remaining: 4999, reset: 200 }); |
| 36 | + expect(rateLimit.userDay).to.deep.equal({ limit: 1000, remaining: 999, reset: 300 }); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
0 commit comments