From 18281f947a946ef7b0440dcebd4cc538262772e3 Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Tue, 20 Sep 2022 09:01:39 +0000 Subject: [PATCH 1/4] fix: pass request body to cache handler --- source/core/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/source/core/index.ts b/source/core/index.ts index 5a803c737..0a054fd64 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1110,6 +1110,7 @@ export default class Request extends Duplex implements RequestEvents { if (options.cache) { (this._requestOptions as any)._request = request; (this._requestOptions as any).cache = options.cache; + (this._requestOptions as any).body = options.body; this._prepareCache(options.cache as StorageAdapter); } From f815992380e773b4ce50980e725ec3693c3d340e Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Tue, 20 Sep 2022 09:12:24 +0000 Subject: [PATCH 2/4] add tests --- test/cache.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/cache.ts b/test/cache.ts index a6059cbf9..5a2ec7958 100644 --- a/test/cache.ts +++ b/test/cache.ts @@ -11,6 +11,7 @@ import CacheableLookup from 'cacheable-lookup'; import delay from 'delay'; import got, {CacheError, type Response} from '../source/index.js'; import withServer from './helpers/with-server.js'; +import { Readable } from 'stream'; const cacheEndpoint: Handler = (_request, response) => { response.setHeader('Cache-Control', 'public, max-age=60'); @@ -44,6 +45,34 @@ test('cacheable responses are cached', withServer, async (t, server, got) => { t.is(firstResponse.body, secondResponse.body); }); +test('cacheable responses to POST requests are cached', withServer, async (t, server, got) => { + server.post('/', cacheEndpoint); + + const cache = new Map(); + + const firstResponse = await got({method: "POST", body: "test", cache}); + const secondResponse = await got({method: "POST", body: "test", cache}); + + t.is(cache.size, 1); + t.is(firstResponse.body, secondResponse.body); +}); + +test('non-cacheable responses to POST requests are not cached', withServer, async (t, server, got) => { + server.post('/', cacheEndpoint); + + const cache = new Map(); + + // POST requests with streams are not cached + const body1 = Readable.from(Buffer.from([1,2,3])); + const body2 = Readable.from(Buffer.from([1,2,3])); + + const firstResponseInt = Number((await got({method: "POST", body: body1, cache})).body); + const secondResponseInt = Number((await got({method: "POST", body: body2, cache})).body); + + t.is(cache.size, 0); + t.true(firstResponseInt < secondResponseInt); +}); + test('cached response is re-encoded to current encoding option', withServer, async (t, server, got) => { server.get('/', cacheEndpoint); From f08eb2404d34e9c812c874e3465c5d62484a26a1 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 20 Sep 2022 17:18:09 +0700 Subject: [PATCH 3/4] Update cache.ts --- test/cache.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/cache.ts b/test/cache.ts index 5a2ec7958..62c270b1a 100644 --- a/test/cache.ts +++ b/test/cache.ts @@ -1,5 +1,6 @@ import {Buffer} from 'buffer'; import {promisify} from 'util'; +import {Readable as ReadableStream} from 'stream'; import {Agent} from 'http'; import {gzip} from 'zlib'; import test from 'ava'; @@ -11,7 +12,6 @@ import CacheableLookup from 'cacheable-lookup'; import delay from 'delay'; import got, {CacheError, type Response} from '../source/index.js'; import withServer from './helpers/with-server.js'; -import { Readable } from 'stream'; const cacheEndpoint: Handler = (_request, response) => { response.setHeader('Cache-Control', 'public, max-age=60'); @@ -63,8 +63,8 @@ test('non-cacheable responses to POST requests are not cached', withServer, asyn const cache = new Map(); // POST requests with streams are not cached - const body1 = Readable.from(Buffer.from([1,2,3])); - const body2 = Readable.from(Buffer.from([1,2,3])); + const body1 = ReadableStream.from(Buffer.from([1,2,3])); + const body2 = ReadableStream.from(Buffer.from([1,2,3])); const firstResponseInt = Number((await got({method: "POST", body: body1, cache})).body); const secondResponseInt = Number((await got({method: "POST", body: body2, cache})).body); From 25563c02333d36429ef61265d2d11fb3031d5f6f Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Tue, 20 Sep 2022 10:44:17 +0000 Subject: [PATCH 4/4] fix lint --- test/cache.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/cache.ts b/test/cache.ts index 62c270b1a..4cf8e36d9 100644 --- a/test/cache.ts +++ b/test/cache.ts @@ -50,8 +50,8 @@ test('cacheable responses to POST requests are cached', withServer, async (t, se const cache = new Map(); - const firstResponse = await got({method: "POST", body: "test", cache}); - const secondResponse = await got({method: "POST", body: "test", cache}); + const firstResponse = await got({method: 'POST', body: 'test', cache}); + const secondResponse = await got({method: 'POST', body: 'test', cache}); t.is(cache.size, 1); t.is(firstResponse.body, secondResponse.body); @@ -63,11 +63,11 @@ test('non-cacheable responses to POST requests are not cached', withServer, asyn const cache = new Map(); // POST requests with streams are not cached - const body1 = ReadableStream.from(Buffer.from([1,2,3])); - const body2 = ReadableStream.from(Buffer.from([1,2,3])); + const body1 = ReadableStream.from(Buffer.from([1, 2, 3])); + const body2 = ReadableStream.from(Buffer.from([1, 2, 3])); - const firstResponseInt = Number((await got({method: "POST", body: body1, cache})).body); - const secondResponseInt = Number((await got({method: "POST", body: body2, cache})).body); + const firstResponseInt = Number((await got({method: 'POST', body: body1, cache})).body); + const secondResponseInt = Number((await got({method: 'POST', body: body2, cache})).body); t.is(cache.size, 0); t.true(firstResponseInt < secondResponseInt);