mirror of https://github.com/lukechilds/got.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
import test from 'ava';
|
|
import got from '../';
|
|
import {createServer} from './helpers/server';
|
|
|
|
let s;
|
|
|
|
test.before('setup', async () => {
|
|
s = await createServer();
|
|
await s.listen(s.port);
|
|
});
|
|
|
|
test('Non cacheable requests are not cached', async t => {
|
|
const endpoint = '/no-cache';
|
|
let noCacheIndex = 0;
|
|
s.on(endpoint, (req, res) => {
|
|
noCacheIndex++;
|
|
res.end(noCacheIndex.toString());
|
|
});
|
|
|
|
const cache = new Map();
|
|
|
|
const firstResponse = parseInt((await got(s.url + endpoint, {cache})).body, 10);
|
|
const secondResponse = parseInt((await got(s.url + endpoint, {cache})).body, 10);
|
|
|
|
t.is(secondResponse, (firstResponse + 1));
|
|
});
|
|
|
|
test('Cacheable requests are cached', async t => {
|
|
const endpoint = '/cache';
|
|
let cacheIndex = 0;
|
|
s.on(endpoint, (req, res) => {
|
|
cacheIndex++;
|
|
res.setHeader('Cache-Control', 'public, max-age=60');
|
|
res.end(cacheIndex.toString());
|
|
});
|
|
|
|
const cache = new Map();
|
|
|
|
const firstResponse = await got(s.url + endpoint, {cache});
|
|
const secondResponse = await got(s.url + endpoint, {cache});
|
|
|
|
t.is(firstResponse.body, secondResponse.body);
|
|
});
|
|
|
|
test.after('cleanup', async () => {
|
|
await s.close();
|
|
});
|
|
|