axios-cache-interceptor/test/header/interpreter.test.ts
Arthur Fiorette 92b9ed7abf
feat: more headers supports, tests and must-revalidate fix (#51)
* feat: must-revalidate and headers enum
* test: added more tests
* test: test and code fixes
* test: more tests
2021-11-11 14:16:37 -03:00

86 lines
2.4 KiB
TypeScript

import { defaultHeaderInterpreter } from '../../src/header/interpreter';
import { Header } from '../../src/util/headers';
describe('tests header interpreter', () => {
it('tests without cache-control header', () => {
const noHeader = defaultHeaderInterpreter();
expect(noHeader).toBeUndefined();
const emptyHeader = defaultHeaderInterpreter({ [Header.CacheControl]: '' });
expect(emptyHeader).toBeUndefined();
});
it('tests with cache preventing headers', () => {
const noStore = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-store'
});
expect(noStore).toBe(false);
const noCache = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-cache'
});
expect(noCache).toBe(false);
const mustRevalidate = defaultHeaderInterpreter({
[Header.CacheControl]: 'must-revalidate'
});
expect(mustRevalidate).toBe(0);
});
it('tests with maxAge header for 10 seconds', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'max-age=10'
});
// 10 Seconds in milliseconds
expect(result).toBe(10 * 1000);
});
it('tests with maxAge=10 and age=7 headers', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'max-age=10',
[Header.Age]: '3'
});
expect(result).toBe(7 * 1000);
});
it('tests with expires and cache-control present', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'max-age=10',
[Header.Expires]: new Date(new Date().getFullYear() + 1, 1, 1).toUTCString()
});
// expires should be ignored
// 10 Seconds in milliseconds
expect(result).toBe(10 * 1000);
});
it('tests with past expires', () => {
const result = defaultHeaderInterpreter({
[Header.Expires]: new Date(new Date().getFullYear() - 1, 1, 1).toUTCString()
});
// Past means cache invalid
expect(result).toBe(false);
});
it('tests with future expires', () => {
const date = new Date(new Date().getFullYear() + 1, 1, 1);
const result = defaultHeaderInterpreter({
[Header.Expires]: date.toUTCString()
});
const approx = date.getTime() - Date.now();
expect(typeof result).toBe('number');
// the result should be what the date is in milliseconds
// minus the actual epoch milliseconds
expect(Math.abs((result as number) - approx)).toBeLessThan(1);
});
});