8000 Fix `response` event not being emitted on cache verify request (#1305) · sindresorhus/got@da4769e · GitHub
[go: up one dir, main page]

Skip to content

Commit da4769e

Browse files
authored
Fix response event not being emitted on cache verify request (#1305)
1 parent aeb2e07 commit da4769e

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

source/core/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,16 +1324,17 @@ export default class Request extends Duplex implements RequestEvents<Request> {
13241324
// `http-cache-semantics` checks this
13251325
delete (options as unknown as NormalizedOptions).url;
13261326

1327+
let request: ClientRequest;
1328+
13271329
// This is ugly
13281330
const cacheRequest = cacheableStore.get((options as any).cache)!(options, response => {
13291331
const typedResponse = response as unknown as IncomingMessageWithTimings & {req: ClientRequest};
1330-
const {req} = typedResponse;
13311332

13321333
// TODO: Fix `cacheable-response`
13331334
(typedResponse as any)._readableState.autoDestroy = false;
13341335

1335-
if (req) {
1336-
req.emit('cacheableResponse', typedResponse);
1336+
if (request) {
1337+
request.emit('cacheableResponse', typedResponse);
13371338
}
13381339

13391340
resolve(typedResponse as unknown as ResponseLike);
@@ -1343,7 +1344,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
13431344
(options as unknown as NormalizedOptions).url = url;
13441345

13451346
cacheRequest.once('error', reject);
1346-
cacheRequest.once('request', resolve);
1347+
cacheRequest.once('request', r => {
1348+
request = r;
1349+
resolve(r);
1350+
});
13471351
});
13481352
}
13491353

test/cache.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ test('decompresses cached responses', withServer, async (t, server, got) => {
244244
} else {
245245
response.setHeader('content-encoding', 'gzip');
246246
response.setHeader('cache-control', 'public, max-age=60');
247-
response.setHeader('etag', 'foobar');
247+
response.setHeader('etag', etag);
248248
response.end(compressed);
249249
}
250250
});
@@ -310,3 +310,34 @@ test('does not hang on huge response', withServer, async (t, server, got) => {
310310

311311
t.is(body.length, bufferSize * times);
312312
});
313+
314+
test('cached response ETag', withServer, async (t, server, got) => {
315+
const etag = 'foobar';
316+
const body = 'responseBody';
317+
318+
server.get('/', (request, response) => {
319+
if (request.headers['if-none-match'] === etag) {
320+
response.writeHead(304);
321+
response.end();
322+
} else {
323+
response.writeHead(200, {ETag: etag});
324+
response.end(body);
325+
}
326+
});
327+
328+
const cache = new Map();
329+
330+
const originalResponse = await got({cache});
331+
332+
t.false(originalResponse.isFromCache);
333+
t.is(originalResponse.body, body);
334+
335+
await delay(100); // Added small delay in order to wait the cache to be populated
336+
337+
t.is(cache.size, 1);
338+
339+
const cachedResponse = await got({cache});
340+
341+
t.true(cachedResponse.isFromCache);
342+
t.is(cachedResponse.body, body);
343+
});

0 commit comments

Comments
 (0)
0