Skip to content

Commit

Permalink
Merge pull request #34 from Zegnat/cache-request-verbs-separately
Browse files Browse the repository at this point in the history
Cache request verbs separately
  • Loading branch information
zachleat authored May 13, 2024
2 parents 47ef973 + f8a40ef commit d6ee43a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ npm run test
```

- We use the [ava JavaScript test runner](https://github.com/avajs/ava) ([Assertions documentation](https://github.com/avajs/ava/blob/master/docs/03-assertions.md))
- Running tests requires Node 14.18+
- ℹ️ To keep tests fast, thou shalt try to avoid writing files in tests.

<!--
Expand Down
12 changes: 11 additions & 1 deletion src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ class RemoteAssetCache extends AssetCache {
if (options.removeUrlQueryParams) {
cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl);
}
super(cleanUrl, cacheDirectory, options);
let cacheKey = [cleanUrl];
if(options.fetchOptions) {
if(options.fetchOptions.method && options.fetchOptions.method !== "GET") {
cacheKey.push(options.fetchOptions.method);
}
if(options.fetchOptions.body) {
cacheKey.push(options.fetchOptions.body);
}
}
cacheKey = cacheKey.length > 1 ? JSON.stringify(cacheKey) : cleanUrl;
super(cacheKey, cacheDirectory, options);

this.url = url;
this.options = options;
Expand Down
33 changes: 33 additions & 0 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ test("Unique hashes for URLs", async (t) => {
t.not(cachePath1, cachePath2);
});

test("Same hashes for implicit and explicit HTTP GET", async t => {
let sameURL = 'https://example.com/';
let cachePath1 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { method: "GET" }
}).cachePath;
let cachePath2 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { }
}).cachePath;
t.is(cachePath1, cachePath2);
});

test("Unique hashes for different HTTP methods", async t => {
let sameURL = 'https://example.com/';
let cachePath1 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { method: "POST" }
}).cachePath;
let cachePath2 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { method: "DELETE" }
}).cachePath;
t.not(cachePath1, cachePath2);
});

test("Unique hashes for different HTTP bodies", async t => {
let sameURL = 'https://example.com/';
let cachePath1 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { body: "123" }
}).cachePath;
let cachePath2 = new RemoteAssetCache(sameURL, ".cache", {
fetchOptions: { body: "456" }
}).cachePath;
t.not(cachePath1, cachePath2);
});

test("Fetching!", async (t) => {
let pngUrl = "https://www.zachleat.com/img/avatar-2017-big.png";
let ac = new RemoteAssetCache(pngUrl);
Expand Down

0 comments on commit d6ee43a

Please sign in to comment.