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

83 lines
2.0 KiB
TypeScript

import { Header } from '../../src/util/headers';
import { mockAxios } from '../mocks/axios';
describe('test request interceptor', () => {
it('tests cache predicate integration', async () => {
const axios = mockAxios();
const fetch = () =>
axios.get('', {
cache: {
cachePredicate: {
responseMatch: () => false
}
}
});
// Make first request to cache it
await fetch();
const result = await fetch();
expect(result.cached).toBe(false);
});
it('tests header interpreter integration', async () => {
const axiosNoCache = mockAxios({}, { [Header.CacheControl]: 'no-cache' });
// Make first request to cache it
await axiosNoCache.get('', { cache: { interpretHeader: true } });
const resultNoCache = await axiosNoCache.get('');
expect(resultNoCache.cached).toBe(false);
const axiosCache = mockAxios(
{},
{ [Header.CacheControl]: `max-age=${60 * 60 * 24 * 365}` }
);
// Make first request to cache it
await axiosCache.get('', { cache: { interpretHeader: true } });
const resultCache = await axiosCache.get('');
expect(resultCache.cached).toBe(true);
});
it('tests update cache integration', async () => {
const axios = mockAxios();
const { id } = await axios.get('key01');
await axios.get('key02', {
cache: {
update: {
[id]: 'delete' as const
}
}
});
const cache = await axios.storage.get(id);
expect(cache.state).toBe('empty');
});
it('tests with blank cache-control header', async () => {
const defaultTtl = 60;
const axios = mockAxios(
{ ttl: defaultTtl, interpretHeader: true },
{ [Header.CacheControl]: '' }
);
const { id } = await axios.get('key01', {
cache: {
interpretHeader: true
}
});
const cache = await axios.storage.get(id);
expect(cache.state).toBe('cached');
expect(cache.ttl).toBe(defaultTtl);
});
});