Skip to content

Commit

Permalink
tests: splited header interpreter tests into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 8, 2022
1 parent 85336da commit 4f938a4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 85 deletions.
33 changes: 33 additions & 0 deletions test/header/cache-control.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { defaultHeaderInterpreter } from '../../src/header/interpreter';
import { Header } from '../../src/util/headers';

describe('test Cache-Control header', () => {
it('tests with cache preventing headers', () => {
const noStore = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-store'
});

expect(noStore).toBe('dont cache');

const noCache = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-cache'
});

expect(noCache).toBe('dont cache');

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);
});
});
42 changes: 42 additions & 0 deletions test/header/expires.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defaultHeaderInterpreter } from '../../src/header/interpreter';
import { Header } from '../../src/util/headers';

describe('test Expires header', () => {
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');

if (typeof result !== 'number') {
return expect(true).toBeFalsy();
}

// the result should be what the date is in milliseconds
// minus the actual epoch milliseconds
expect(Math.abs(result - approx)).toBeLessThanOrEqual(1);
});

it('expects Expires to be used when invalid Cache-Control is provided', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: '',
[Header.Expires]: new Date(new Date().getFullYear() - 1, 1, 1).toUTCString()
});

expect(result).toBe('dont cache');
});

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('dont cache');
});
});
53 changes: 0 additions & 53 deletions test/header/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,6 @@ describe('tests header interpreter', () => {
);
});

it('tests with cache preventing headers', () => {
const noStore = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-store'
});

expect(noStore).toBe('dont cache');

const noCache = defaultHeaderInterpreter({
[Header.CacheControl]: 'no-cache'
});

expect(noCache).toBe('dont cache');

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',
Expand All @@ -65,15 +36,6 @@ describe('tests header interpreter', () => {
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('dont cache');
});

it('tests with immutable', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'immutable'
Expand All @@ -82,19 +44,4 @@ describe('tests header interpreter', () => {
// 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);
});
});
65 changes: 33 additions & 32 deletions test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,39 +107,40 @@ describe('tests cache predicate object', () => {
expect(testError).toBeFalsy();
});

it('tests generics and typescript types', () => {
() => {
const axios = mockAxios();
axios.get<{ a: boolean; b: number }>('/', {
cache: {
ttl: ({ data }) => {
return data.b;
},
cachePredicate: {
responseMatch: ({ data }) => {
return data.a;
}
},
update: {
id: (
_,
{ data: { a, b }, headers, status, statusText }
): CachedStorageValue => {
return {
state: 'cached',
ttl: Infinity,
createdAt: Date.now(),
data: {
headers,
status,
statusText,
data: { a, b }
}
};
}
it('tests generics and typescript types', async () => {
const axios = mockAxios();

const result = await axios.get<{ a: boolean; b: number }>('url', {
cache: {
ttl: ({ data }) => {
return data.b;
},
cachePredicate: {
responseMatch: ({ data }) => {
return data.a;
}
},
update: {
id: (
_,
{ data: { a, b }, headers, status, statusText }
): CachedStorageValue => {
return {
state: 'cached',
ttl: Infinity,
createdAt: Date.now(),
data: {
headers,
status,
statusText,
data: { a, b }
}
};
}
}
});
};
}
});

expect(result).toBeDefined();
});
});

0 comments on commit 4f938a4

Please sign in to comment.