handle fs read streams
This commit is contained in:
parent
8a338f5f5d
commit
3b5adecd7f
8
index.js
8
index.js
@ -11,13 +11,17 @@ function md5(str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBodyCacheKeyJson(body) {
|
function getBodyCacheKeyJson(body) {
|
||||||
if (typeof body === 'string') {
|
if (!body) {
|
||||||
|
return body;
|
||||||
|
} if (typeof body === 'string') {
|
||||||
return body;
|
return body;
|
||||||
} if (body instanceof URLSearchParams) {
|
} if (body instanceof URLSearchParams) {
|
||||||
return body.toString();
|
return body.toString();
|
||||||
|
} if (body instanceof fs.ReadStream) {
|
||||||
|
return body.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return body;
|
throw new Error('Unsupported body type');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCacheKey(requestArguments) {
|
function getCacheKey(requestArguments) {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
const fs = require('fs');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const rimraf = require('rimraf');
|
const rimraf = require('rimraf');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@ -141,6 +142,27 @@ describe('Cache tests', function() {
|
|||||||
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
|
res = await fetch(TWO_HUNDRED_URL, post(new URLSearchParams('a=a')));
|
||||||
assert.strictEqual(res.fromCache, true);
|
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);
|
}).timeout(10000);
|
||||||
|
|
||||||
describe('Data tests', function() {
|
describe('Data tests', function() {
|
||||||
|
Reference in New Issue
Block a user