Skip to content

Commit

Permalink
Make sure cached body is decompressed
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Apr 14, 2020
1 parent bf0b0cd commit 6ca17e2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {promisify} from 'util';
import {gzip} from 'zlib';
import test from 'ava';
import pEvent = require('p-event');
import getStream = require('get-stream');
Expand Down Expand Up @@ -227,3 +229,33 @@ test('does not break POST requests', withServer, async (t, server, got) => {

t.is(headers['content-length'], '0');
});

test('decompresses cached responses', withServer, async (t, server, got) => {
const etag = 'foobar';

const payload = JSON.stringify({foo: 'bar'});
const compressed = await promisify(gzip)(payload);

server.get('/', (request, response) => {
if (request.headers['if-none-match'] === etag) {
response.statusCode = 304;
response.end();
} else {
response.setHeader('content-encoding', 'gzip');
response.setHeader('cache-control', 'public, max-age: 60');
response.setHeader('etag', 'foobar');
response.end(compressed);
}
});

const cache = new Map();

for (let i = 0; i < 2; i++) {
await t.notThrowsAsync(got({
cache,
responseType: 'json',
decompress: true,
retry: 2
}));
}
});

0 comments on commit 6ca17e2

Please sign in to comment.