Skip to content

Commit

Permalink
Add failing tests for #2170 (#2171)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Sigill and sindresorhus authored Mar 3, 2023
1 parent 13a68d3 commit e4460f7
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions test/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {promisify} from 'util';
import {Readable as ReadableStream} from 'stream';
import {Agent} from 'http';
import {gzip} from 'zlib';
import process from 'process';
import test from 'ava';
import {pEvent} from 'p-event';
import getStream from 'get-stream';
Expand Down Expand Up @@ -471,3 +472,146 @@ test('response.complete is true when using keepalive agent', withServer, async (

t.true(first.complete);
});

test.failing('revalidated uncompressed responses are retrieved from cache', withServer, async (t, server, got) => {
let revalidated = false;

const payload = JSON.stringify([1]);

server.get('/', (request, response) => {
if (request.headers['if-none-match'] === 'asdf') {
revalidated = true;
response.writeHead(304, {etag: 'asdf'});
response.end();
} else {
response.writeHead(200, {
etag: 'asdf',
'cache-control': 'public, max-age=1, s-maxage=1',
'content-type': 'application/json',
});
response.write(payload);
response.end();
}
});

t.timeout(5000);

const client = got.extend({cache: new Map(), responseType: 'json'});

await client('').then(response => {
t.false(revalidated);
t.deepEqual(response.body, [1]);
t.true(response.complete);
});

// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve, 3000));

console.log('max-age has expired, performing second request');

await client('').then(response => {
t.true(revalidated);
t.deepEqual(response.body, [1]);
t.true(response.complete); // Fails here.
});
});

test.failing('revalidated compressed responses are retrieved from cache', withServer, async (t, server, got) => {
let revalidated = false;

const payload = JSON.stringify([1]);
const compressed = await promisify(gzip)(payload);

server.get('/', (request, response) => {
if (request.headers['if-none-match'] === 'asdf') {
revalidated = true;
response.writeHead(304, {etag: 'asdf'});
response.end();
} else {
response.writeHead(200, {
etag: 'asdf',
'cache-control': 'public, max-age=1, s-maxage=1',
'content-type': 'application/json',
'content-encoding': 'gzip',
});
response.write(compressed);
response.end();
}
});

t.timeout(5000);

const client = got.extend({cache: new Map(), responseType: 'json'});

await client('').then(response => {
t.false(revalidated);
t.deepEqual(response.body, [1]);
t.true(response.complete);
});

// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve, 3000));

console.log('max-age has expired, performing second request (but it will actually hang)');

await client('').then(response => {
t.true(revalidated);
t.deepEqual(response.body, [1]);
t.true(response.complete);
});
});

test.failing('revalidated uncompressed responses from github are retrieved from cache', async t => {
const client = got.extend({
cache: new Map(),
cacheOptions: {shared: false},
responseType: 'json',
headers: {
'accept-encoding': 'identity',
...(process.env.GITHUB_TOKEN ? {authorization: `token ${process.env.GITHUB_TOKEN}`} : {}),
},
});

t.timeout(70_000);

await client('https://api.github.com/repos/octocat/Spoon-Knife').then(response => {
t.is((response.body as any).name, 'Spoon-Knife');
t.true(response.complete);
});

// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve, 65_000));

console.log('max-age has expired, performing second request');

await client('https://api.github.com/repos/octocat/Spoon-Knife').then(response => {
t.is((response.body as any).name, 'Spoon-Knife');
t.true(response.complete); // Fails here.
});
});

test.failing('revalidated compressed responses from github are retrieved from cache', async t => {
const client = got.extend({
cache: new Map(),
cacheOptions: {shared: false},
responseType: 'json',
headers: process.env.GITHUB_TOKEN ? {authorization: `token ${process.env.GITHUB_TOKEN}`} : {},
});

t.timeout(70_000);

await client('https://api.github.com/repos/octocat/Spoon-Knife').then(response => {
t.is((response.body as any).name, 'Spoon-Knife');
t.true(response.complete);
});

// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve, 65_000));

console.log('max-age has expired, performing second request (but it will actually hang)');

await client('https://api.github.com/repos/octocat/Spoon-Knife').then(response => {
t.is((response.body as any).name, 'Spoon-Knife');
t.true(response.complete);
});
});

0 comments on commit e4460f7

Please sign in to comment.