From 077b92ebaaacb5d9fbf11a25f5b9a7978de677d8 Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Sat, 28 Nov 2020 10:59:56 -0500 Subject: [PATCH] add tests for string body and URLSearchParams bodies --- test/tests.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/tests.js b/test/tests.js index 798bb01..7e882d3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -2,6 +2,7 @@ const assert = require('assert'); const rimraf = require('rimraf'); const path = require('path'); const FetchCache = require('../index.js'); +const { URLSearchParams } = require('url'); const CACHE_PATH = path.join(__dirname, '..', '.cache'); @@ -15,6 +16,10 @@ let fetch; let res; let body; +function post(body) { + return { method: 'POST', body }; +} + beforeEach(async function() { rimraf.sync(CACHE_PATH); fetch = FetchCache(CACHE_PATH); @@ -104,6 +109,38 @@ describe('Cache tests', function() { 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); describe('Data tests', function() {