|
1 | 1 | import {Buffer} from 'buffer';
|
2 | 2 | import {promisify} from 'util';
|
| 3 | +import {Readable as ReadableStream} from 'stream'; |
3 | 4 | import {Agent} from 'http';
|
4 | 5 | import {gzip} from 'zlib';
|
5 | 6 | import test from 'ava';
|
@@ -44,6 +45,34 @@ test('cacheable responses are cached', withServer, async (t, server, got) => {
|
44 | 45 | t.is(firstResponse.body, secondResponse.body);
|
45 | 46 | });
|
46 | 47 |
|
| 48 | +test('cacheable responses to POST requests are cached', withServer, async (t, server, got) => { |
| 49 | + server.post('/', cacheEndpoint); |
| 50 | + |
| 51 | + const cache = new Map(); |
| 52 | + |
| 53 | + const firstResponse = await got({method: 'POST', body: 'test', cache}); |
| 54 | + const secondResponse = await got({method: 'POST', body: 'test', cache}); |
| 55 | + |
| 56 | + t.is(cache.size, 1); |
| 57 | + t.is(firstResponse.body, secondResponse.body); |
| 58 | +}); |
| 59 | + |
| 60 | +test('non-cacheable responses to POST requests are not cached', withServer, async (t, server, got) => { |
| 61 | + server.post('/', cacheEndpoint); |
| 62 | + |
| 63 | + const cache = new Map(); |
| 64 | + |
| 65 | + // POST requests with streams are not cached |
| 66 | + const body1 = ReadableStream.from(Buffer.from([1, 2, 3])); |
| 67 | + const body2 = ReadableStream.from(Buffer.from([1, 2, 3])); |
| 68 | + |
| 69 | + const firstResponseInt = Number((await got({method: 'POST', body: body1, cache})).body); |
| 70 | + const secondResponseInt = Number((await got({method: 'POST', body: body2, cache})).body); |
| 71 | + |
| 72 | + t.is(cache.size, 0); |
| 73 | + t.true(firstResponseInt < secondResponseInt); |
| 74 | +}); |
| 75 | + |
47 | 76 | test('cached response is re-encoded to current encoding option', withServer, async (t, server, got) => {
|
48 | 77 | server.get('/', cacheEndpoint);
|
49 | 78 |
|
|
0 commit comments