Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache request verbs separately #34

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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