handle fs read streams

This commit is contained in:
Randall Schmidt 2020-11-28 11:25:23 -05:00
parent 8a338f5f5d
commit 3b5adecd7f
2 changed files with 28 additions and 2 deletions

@ -11,13 +11,17 @@ function md5(str) {
}
function getBodyCacheKeyJson(body) {
if (typeof body === 'string') {
if (!body) {
return body;
} if (typeof body === 'string') {
return body;
} if (body instanceof URLSearchParams) {
return body.toString();
} if (body instanceof fs.ReadStream) {
return body.path;
}
return body;
throw new Error('Unsupported body type');
}
function getCacheKey(requestArguments) {

@ -1,3 +1,4 @@
const fs = require('fs');
const assert = require('assert');
const rimraf = require('rimraf');
const path = require('path');
@ -141,6 +142,27 @@ describe('Cache tests', function() {
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
assert.strictEqual(res.fromCache, true);
});
it('Gives different read streams different cache keys', async function() {
const s1 = fs.createReadStream(__filename);
const s2 = fs.createReadStream(path.join(__dirname, '..', 'index.js'));
res = await fetch(TWO_HUNDRED_URL, post(s1));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post(s2));
assert.strictEqual(res.fromCache, false);
});
it('Gives the same read streams the same cache key', async function() {
const s1 = fs.createReadStream(__filename);
res = await fetch(TWO_HUNDRED_URL, post(s1));
assert.strictEqual(res.fromCache, false);
res = await fetch(TWO_HUNDRED_URL, post(s1));
assert.strictEqual(res.fromCache, true);
});
}).timeout(10000);
describe('Data tests', function() {