-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from octotravel/feat/fixes
Feat/fixes
- Loading branch information
Showing
7 changed files
with
1,361 additions
and
684 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class HeaderParser { | ||
public static getRetryAfterInSeconds(response: Response): number { | ||
if (!response?.headers) { | ||
return 0; | ||
} | ||
|
||
const retryAfterHeader = response.headers.get('retry-after'); | ||
if (retryAfterHeader === null) { | ||
return 0; | ||
} | ||
|
||
const retryAfterHeaderNumberValue = Number(retryAfterHeader); | ||
if (Number.isFinite(retryAfterHeaderNumberValue)) { | ||
return retryAfterHeaderNumberValue || 1; | ||
} | ||
|
||
const retryDateMS = Date.parse(retryAfterHeader); | ||
if (Number.isNaN(retryDateMS)) { | ||
return 0; | ||
} | ||
|
||
const deltaMS = retryDateMS - Date.now(); | ||
return deltaMS > 0 ? Math.ceil(deltaMS / 1000) : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { HeaderParser } from '../HeaderParser'; | ||
|
||
describe('HeaderParser.getRetryAfterInSeconds', () => { | ||
it('should return 0 if response is null or undefined', () => { | ||
expect(HeaderParser.getRetryAfterInSeconds(null as any)).toBe(0); | ||
expect(HeaderParser.getRetryAfterInSeconds(undefined as any)).toBe(0); | ||
}); | ||
|
||
it('should return 0 if response.headers is null or undefined', () => { | ||
const response = { headers: null } as any; | ||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBe(0); | ||
}); | ||
|
||
it('should return 0 if the "retry-after" header is not present', () => { | ||
const response = { | ||
headers: { | ||
get: vi.fn().mockReturnValue(null), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBe(0); | ||
expect(response.headers.get).toHaveBeenCalledWith('retry-after'); | ||
}); | ||
|
||
it('should return the numeric value if "retry-after" header is a valid number', () => { | ||
const response = { | ||
headers: { | ||
get: vi.fn().mockReturnValue('10'), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBe(10); | ||
expect(response.headers.get).toHaveBeenCalledWith('retry-after'); | ||
}); | ||
|
||
it('should return 1 if "retry-after" header is 0 or NaN when parsed as a number', () => { | ||
const responseWithZero = { | ||
headers: { | ||
get: vi.fn().mockReturnValue('0'), | ||
}, | ||
} as unknown as Response; | ||
|
||
const responseWithNaN = { | ||
headers: { | ||
get: vi.fn().mockReturnValue('not-a-number'), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(responseWithZero)).toBe(1); | ||
expect(HeaderParser.getRetryAfterInSeconds(responseWithNaN)).toBe(0); | ||
}); | ||
|
||
it('should return the difference in seconds if "retry-after" is a valid date string in the future', () => { | ||
const futureDate = new Date(Date.now() + 5000).toUTCString(); // 5 seconds in the future | ||
const response = { | ||
headers: { | ||
get: vi.fn().mockReturnValue(futureDate), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBeGreaterThanOrEqual(4); | ||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBeLessThanOrEqual(5); | ||
}); | ||
|
||
it('should return 1 if "retry-after" is a valid date string in the past', () => { | ||
const pastDate = new Date(Date.now() - 5000).toUTCString(); // 5 seconds in the past | ||
const response = { | ||
headers: { | ||
get: vi.fn().mockReturnValue(pastDate), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBe(1); | ||
}); | ||
|
||
it('should return 0 if "retry-after" is an invalid date string', () => { | ||
const invalidDate = 'invalid-date'; | ||
const response = { | ||
headers: { | ||
get: vi.fn().mockReturnValue(invalidDate), | ||
}, | ||
} as unknown as Response; | ||
|
||
expect(HeaderParser.getRetryAfterInSeconds(response)).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters