add tests for string body and URLSearchParams bodies

This commit is contained in:
Randall Schmidt 2020-11-28 10:59:56 -05:00
parent 1df05c2584
commit 077b92ebaa

@ -2,6 +2,7 @@ const assert = require('assert');
const rimraf = require('rimraf'); const rimraf = require('rimraf');
const path = require('path'); const path = require('path');
const FetchCache = require('../index.js'); const FetchCache = require('../index.js');
const { URLSearchParams } = require('url');
const CACHE_PATH = path.join(__dirname, '..', '.cache'); const CACHE_PATH = path.join(__dirname, '..', '.cache');
@ -15,6 +16,10 @@ let fetch;
let res; let res;
let body; let body;
function post(body) {
return { method: 'POST', body };
}
beforeEach(async function() { beforeEach(async function() {
rimraf.sync(CACHE_PATH); rimraf.sync(CACHE_PATH);
fetch = FetchCache(CACHE_PATH); fetch = FetchCache(CACHE_PATH);
@ -104,6 +109,38 @@ describe('Cache tests', function() {
await res.ejectFromCache(); await res.ejectFromCache();
await res.ejectFromCache(); await res.ejectFromCache();
}); });
it('Gives different string bodies different cache keys', async function() {
res = await fetch(TWO_HUNDRED_URL, post('a'));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post('b'));
assert.strictEqual(res.fromCache, false);
});
it('Gives same string bodies same cache keys', async function() {
res = await fetch(TWO_HUNDRED_URL, post('a'));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post('a'));
assert.strictEqual(res.fromCache, true);
});
it('Gives different URLSearchParams different cache keys', async function() {
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=b')));
assert.strictEqual(res.fromCache, false);
});
it('Gives same URLSearchParams same cache keys', async function() {
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
assert.strictEqual(res.fromCache, true);
});
}).timeout(10000); }).timeout(10000);
describe('Data tests', function() { describe('Data tests', function() {