Skip to content

Commit

Permalink
feat: remove remnant x-axios-headers from server response
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jun 5, 2022
1 parent e8cb692 commit d87307a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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))
) {
Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions src/util/update-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 4 additions & 1 deletion test/interceptors/stale-if-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit d87307a

Please sign in to comment.