mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
* feat(WIP): etag and if-modified-since support * test: fixed test name * fix: merge response headers * test: add etag / last-modified tests * test: add must-revalidate tests * fix: handle expirationTime 0 as true. * tests: refactored some tests * test: added keepIfStale tests * fix: remove axios error for 304 requests * fix: possible infinite loop on validateStatus function * tests: ignore code that is hard test from coverage * fix: use Last-Modified header for modifiedSince If-Modified-Since is never sent by a server, it's a client only header. However Last-Modified is sent by the server to indicate the last time the returned version was modified (it might not be the last version depending on cache configuration on intermediate servers) * test: use validateStatus in mock This more closely match default axios adapter. * fix: validateStatus handling all cases * refactor: use cache.createdAt if the last-modified header isn't present * test: etag with predefined value * test: added more test cases * fix: fixed logic in some tests * docs: initial documentation * fix: manual values work out of the box This removes header requirement from server. * docs: add details to etag and modifiedSince features * fix: delete custom headers from response * feat: accept all configurations from global axios Merging global into local configuration enable user to use global configuration for all options and remove the need to check local and global values every time. * fix: preserve original types from AxiosInstance The only value axios needs is a URL, and in the second definition of the method, there is already a URL parameter, so it can be undefined. * Fix: defaults modifiedSince back to false. Avoids breaking changes. * docs: fix etag examples * docs: document internal headers * refactor: ternary operator :) * style: prettified code * test: remove modifiedSince: false in tests since this is the default * docs: fix headers examples * docs: fixed example formatting * tests: split tests into multiple files to test them faster * docs: correct jsdoc empty object Co-authored-by: Charly Koza <cka@f4-group.com>
95 lines
2.6 KiB
TypeScript
95 lines
2.6 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=3 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 immutable', () => {
|
|
const result = defaultHeaderInterpreter({
|
|
[Header.CacheControl]: 'immutable'
|
|
});
|
|
|
|
// 1 year
|
|
expect(result).toBe(1000 * 60 * 60 * 24 * 365);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|