|
| 1 | +import { HttpResponseHeadersUtils } from '../../src/lib/httpResponseHeadersUtils.js'; |
| 2 | + |
| 3 | +describe('HttpResponseHeadersUtils', () => { |
| 4 | + describe('getClientQuotaLimit', () => { |
| 5 | + it('should return a valid TokenQuotaBucket when auth0-client-quota-limit header is present', () => { |
| 6 | + const headers = { |
| 7 | + 'auth0-client-quota-limit': 'per_hour;q=100;r=50;t=3600,per_day;q=1000;r=500;t=86400', |
| 8 | + }; |
| 9 | + const result = HttpResponseHeadersUtils.getClientQuotaLimit(headers); |
| 10 | + expect(result).toEqual({ |
| 11 | + perHour: { quota: 100, remaining: 50, resetAfter: 3600 }, |
| 12 | + perDay: { quota: 1000, remaining: 500, resetAfter: 86400 }, |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return null when no relevant headers are present', () => { |
| 17 | + const headers = { 'some-other-header': 'value' }; |
| 18 | + const result = HttpResponseHeadersUtils.getClientQuotaLimit(headers); |
| 19 | + expect(result).toBeNull(); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('getOrganizationQuotaLimit', () => { |
| 24 | + it('should return a valid TokenQuotaBucket when auth0-organization-quota-limit header is present', () => { |
| 25 | + const headers = { 'auth0-organization-quota-limit': 'per_hour;q=200;r=150;t=3600' }; |
| 26 | + const result = HttpResponseHeadersUtils.getOrganizationQuotaLimit(headers); |
| 27 | + expect(result).toEqual({ |
| 28 | + perHour: { quota: 200, remaining: 150, resetAfter: 3600 }, |
| 29 | + perDay: undefined, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return null when no relevant headers are present', () => { |
| 34 | + const headers = { 'some-other-header': 'value' }; |
| 35 | + const result = HttpResponseHeadersUtils.getOrganizationQuotaLimit(headers); |
| 36 | + expect(result).toBeNull(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('parseQuota', () => { |
| 41 | + it('should correctly parse a token quota string into a TokenQuotaBucket', () => { |
| 42 | + const tokenQuota = 'per_hour;q=300;r=250;t=3600,per_day;q=3000;r=2500;t=86400'; |
| 43 | + const result = HttpResponseHeadersUtils['parseQuota'](tokenQuota); |
| 44 | + expect(result).toEqual({ |
| 45 | + perHour: { quota: 300, remaining: 250, resetAfter: 3600 }, |
| 46 | + perDay: { quota: 3000, remaining: 2500, resetAfter: 86400 }, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle missing attributes gracefully', () => { |
| 51 | + const tokenQuota = 'per_hour;q=300;r=250'; |
| 52 | + const result = HttpResponseHeadersUtils['parseQuota'](tokenQuota); |
| 53 | + expect(result).toEqual({ |
| 54 | + perHour: { quota: 300, remaining: 250, resetAfter: 0 }, |
| 55 | + perDay: undefined, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return an empty TokenQuotaBucket for invalid input', () => { |
| 60 | + const tokenQuota = 'invalid_format'; |
| 61 | + const result = HttpResponseHeadersUtils['parseQuota'](tokenQuota); |
| 62 | + expect(result).toEqual({ perHour: undefined, perDay: undefined }); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments