/
cache.ts
178 lines (132 loc) 路 4.75 KB
/
cache.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import test from 'ava';
import pEvent from 'p-event';
import getStream from 'get-stream';
import {Response} from '../source/utils/types';
import withServer from './helpers/with-server';
const cacheEndpoint = (_request, response) => {
response.setHeader('Cache-Control', 'public, max-age=60');
response.end(Date.now().toString());
};
test('non-cacheable responses are not cached', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Cache-Control', 'public, no-cache, no-store');
response.end(Date.now().toString());
});
const cache = new Map();
const firstResponseInt = Number((await got({cache})).body);
const secondResponseInt = Number((await got({cache})).body);
t.is(cache.size, 0);
t.true(firstResponseInt < secondResponseInt);
});
test('cacheable responses are cached', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
const firstResponse = await got({cache});
const secondResponse = await got({cache});
t.is(cache.size, 1);
t.is(firstResponse.body, secondResponse.body);
});
test('cached response is re-encoded to current encoding option', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
const firstEncoding = 'base64';
const secondEncoding = 'hex';
const firstResponse = await got({cache, encoding: firstEncoding});
const secondResponse = await got({cache, encoding: secondEncoding});
const expectedSecondResponseBody = Buffer.from(firstResponse.body, firstEncoding).toString(secondEncoding);
t.is(cache.size, 1);
t.is(secondResponse.body, expectedSecondResponseBody);
});
test('redirects are cached and re-used internally', withServer, async (t, server, got) => {
let status301Index = 0;
server.get('/301', (_request, response) => {
if (status301Index === 0) {
response.setHeader('Cache-Control', 'public, max-age=60');
response.setHeader('Location', '/');
response.statusCode = 301;
}
response.end();
status301Index++;
});
let status302Index = 0;
server.get('/302', (_request, response) => {
if (status302Index === 0) {
response.setHeader('Cache-Control', 'public, max-age=60');
response.setHeader('Location', '/');
response.statusCode = 302;
}
response.end();
status302Index++;
});
server.get('/', cacheEndpoint);
const cache = new Map();
const firstResponse = await got('301', {cache});
const secondResponse = await got('302', {cache});
t.is(cache.size, 3);
t.is(firstResponse.body, secondResponse.body);
});
test('cached response has got options', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
const options = {
auth: 'foo:bar',
cache
};
await got(options);
const secondResponse = await got(options);
t.is(secondResponse.request.options.auth, options.auth);
});
test('cache error throws `got.CacheError`', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
const cache = {};
await t.throwsAsync(got({cache}), got.CacheError);
});
test('doesn\'t cache response when received HTTP error', withServer, async (t, server, got) => {
let calledFirstError = false;
server.get('/', (_request, response) => {
if (calledFirstError) {
response.end('ok');
return;
}
calledFirstError = true;
response.statusCode = 502;
response.end('received 502');
});
const cache = new Map();
const {statusCode, body} = await got({cache, throwHttpErrors: false});
t.is(statusCode, 200);
t.deepEqual(body, 'ok');
});
test('DNS cache works', withServer, async (t, _server, got) => {
const map = new Map();
await t.notThrowsAsync(got('https://example.com', {dnsCache: map}));
t.is(map.size, 1);
});
test('`isFromCache` stream property is undefined before the `response` event', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
const stream = got.stream({cache});
t.is(stream.isFromCache, undefined);
await getStream(stream);
});
test('`isFromCache` stream property is false after the `response` event', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
const stream = got.stream({cache});
const response = await pEvent(stream, 'response') as Response;
t.is(response.isFromCache, false);
t.is(stream.isFromCache, false);
await getStream(stream);
});
test('`isFromCache` stream property is true if the response was cached', withServer, async (t, server, got) => {
server.get('/', cacheEndpoint);
const cache = new Map();
await getStream(got.stream({cache}));
const stream = got.stream({cache});
const response = await pEvent(stream, 'response') as Response;
t.is(response.isFromCache, true);
t.is(stream.isFromCache, true);
await getStream(stream);
});