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); } diff --git a/test/cache.ts b/test/cache.ts index a6059cbf9..4cf8e36d9 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'; @@ -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 = 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); + + 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);