-
Notifications
You must be signed in to change notification settings - Fork 2k
/
cacheControlExtension.test.ts
117 lines (98 loc) 路 3.91 KB
/
cacheControlExtension.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { ResponsePath, GraphQLError } from 'graphql';
import { GraphQLResponse } from 'graphql-extensions';
import { Headers } from 'apollo-server-env';
import { CacheControlExtension, CacheScope } from '../';
describe('CacheControlExtension', () => {
let cacheControlExtension: CacheControlExtension;
beforeEach(() => {
cacheControlExtension = new CacheControlExtension();
});
describe('willSendResponse', () => {
let graphqlResponse: GraphQLResponse;
beforeEach(() => {
cacheControlExtension.options.calculateHttpHeaders = true;
cacheControlExtension.computeOverallCachePolicy = () => ({
maxAge: 300,
scope: CacheScope.Public,
});
graphqlResponse = {
http: {
headers: new Headers(),
},
data: { test: 'test' },
};
});
it('sets cache-control header', () => {
cacheControlExtension.willSendResponse &&
cacheControlExtension.willSendResponse({ graphqlResponse });
expect(graphqlResponse.http!.headers.get('Cache-Control')).toBe(
'max-age=300, public',
);
});
const shouldNotSetCacheControlHeader = () => {
cacheControlExtension.willSendResponse &&
cacheControlExtension.willSendResponse({ graphqlResponse });
expect(graphqlResponse.http!.headers.get('Cache-Control')).toBeNull();
};
it('does not set cache-control header if calculateHttpHeaders is set to false', () => {
cacheControlExtension.options.calculateHttpHeaders = false;
shouldNotSetCacheControlHeader();
});
it('does not set cache-control header if graphqlResponse has errors', () => {
graphqlResponse.errors = [new GraphQLError('Test Error')];
shouldNotSetCacheControlHeader();
});
it('does not set cache-control header if there is no overall cache policy', () => {
cacheControlExtension.computeOverallCachePolicy = () => undefined;
shouldNotSetCacheControlHeader();
});
});
describe('computeOverallCachePolicy', () => {
const responsePath: ResponsePath = {
key: 'test',
prev: undefined,
};
const responseSubPath: ResponsePath = {
key: 'subTest',
prev: responsePath,
};
const responseSubSubPath: ResponsePath = {
key: 'subSubTest',
prev: responseSubPath,
};
it('returns undefined without cache hints', () => {
const cachePolicy = cacheControlExtension.computeOverallCachePolicy();
expect(cachePolicy).toBeUndefined();
});
it('returns lowest max age value', () => {
cacheControlExtension.addHint(responsePath, { maxAge: 10 });
cacheControlExtension.addHint(responseSubPath, { maxAge: 20 });
const cachePolicy = cacheControlExtension.computeOverallCachePolicy();
expect(cachePolicy).toHaveProperty('maxAge', 10);
});
it('returns undefined if any cache hint has a maxAge of 0', () => {
cacheControlExtension.addHint(responsePath, { maxAge: 120 });
cacheControlExtension.addHint(responseSubPath, { maxAge: 0 });
cacheControlExtension.addHint(responseSubSubPath, { maxAge: 20 });
const cachePolicy = cacheControlExtension.computeOverallCachePolicy();
expect(cachePolicy).toBeUndefined();
});
it('returns PUBLIC scope by default', () => {
cacheControlExtension.addHint(responsePath, { maxAge: 10 });
const cachePolicy = cacheControlExtension.computeOverallCachePolicy();
expect(cachePolicy).toHaveProperty('scope', CacheScope.Public);
});
it('returns PRIVATE scope if any cache hint has PRIVATE scope', () => {
cacheControlExtension.addHint(responsePath, {
maxAge: 10,
scope: CacheScope.Public,
});
cacheControlExtension.addHint(responseSubPath, {
maxAge: 10,
scope: CacheScope.Private,
});
const cachePolicy = cacheControlExtension.computeOverallCachePolicy();
expect(cachePolicy).toHaveProperty('scope', CacheScope.Private);
});
});
});