feat: remove remnant x-axios-headers from server response

This commit is contained in:
arthurfiorette 2022-06-05 09:50:02 -03:00
parent e8cb69255d
commit d87307ae93
No known key found for this signature in database
GPG Key ID: 9D190CD53C53C555
4 changed files with 30 additions and 13 deletions

View File

@ -54,7 +54,7 @@ export function defaultResponseInterceptor(
if (__ACI_DEV__) {
axios.debug?.({
id,
msg: 'Response with config.cache === false',
msg: 'Response with config.cache falsy',
data: response
});
}
@ -82,7 +82,7 @@ export function defaultResponseInterceptor(
// Config told that this response should be cached.
if (
// For 'loading' values (post stale), this check was already run in the past.
// For 'loading' values (previous: stale), this check already ran in the past.
!cache.data &&
!(await testCachePredicate(response, cacheConfig.cachePredicate))
) {
@ -98,13 +98,11 @@ export function defaultResponseInterceptor(
return response;
}
// avoid remnant headers from remote server to break implementation
for (const header in Header) {
if (!header.startsWith('XAxiosCache')) {
continue;
// Avoid remnant headers from remote server to break implementation
for (const header of Object.keys(response.headers)) {
if (header.startsWith('x-axios-cache')) {
delete response.headers[header];
}
delete response.headers[header];
}
if (cacheConfig.etag && cacheConfig.etag !== true) {

View File

@ -8,10 +8,7 @@ export async function updateCache<T, D>(
data: CacheAxiosResponse<T, D>,
entries: Record<string, CacheUpdater<T, D>>
): Promise<void> {
for (const cacheKey in entries) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const updater = entries[cacheKey]!;
for (const [cacheKey, updater] of Object.entries(entries)) {
if (updater === 'delete') {
await storage.remove(cacheKey, data.config);
continue;

View File

@ -158,4 +158,23 @@ describe('test request interceptor', () => {
expect(id).toBeDefined();
expect(typeof id).toBe('string');
});
it('It expects that any X-axios-cache gets removed', async () => {
const headerValue = '23asdf8ghd';
const axios = mockAxios(
{},
{
[Header.XAxiosCacheEtag]: headerValue,
[Header.XAxiosCacheLastModified]: headerValue,
[Header.XAxiosCacheStaleIfError]: headerValue
}
);
const { headers } = await axios.get('url');
expect(headers[Header.XAxiosCacheEtag]).not.toBe(headerValue);
expect(headers[Header.XAxiosCacheLastModified]).not.toBe(headerValue);
expect(headers[Header.XAxiosCacheStaleIfError]).not.toBe(headerValue);
});
});

View File

@ -48,13 +48,16 @@ describe('Last-Modified handling', () => {
});
it('expects that XAxiosCacheStaleIfError is defined', async () => {
const axios = mockAxios();
const axios = mockAxios({
ttl: 127910 // random number
});
const { headers } = await axios.get('url', {
cache: { staleIfError: true }
});
expect(headers).toHaveProperty(Header.XAxiosCacheStaleIfError);
expect(headers[Header.XAxiosCacheStaleIfError]).toBe('127910');
});
it('expects staleIfError is ignore if config.cache is false', async () => {