summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/tests/integration/cache_tests.rs54
-rw-r--r--cli/tests/unit/cache_api_test.ts17
2 files changed, 71 insertions, 0 deletions
diff --git a/cli/tests/integration/cache_tests.rs b/cli/tests/integration/cache_tests.rs
index 32508a95a..7975cbf19 100644
--- a/cli/tests/integration/cache_tests.rs
+++ b/cli/tests/integration/cache_tests.rs
@@ -127,3 +127,57 @@ fn cache_matching_package_json_dep_should_not_install_all() {
"Initialize @types/node@18.8.2\n",
));
}
+
+// Regression test for https://github.com/denoland/deno/issues/17299
+#[test]
+fn cache_put_overwrite() {
+ let test_context = TestContextBuilder::new().use_temp_cwd().build();
+ let temp_dir = test_context.temp_dir();
+
+ let part_one = r#"
+ const req = new Request('http://localhost/abc');
+ const res1 = new Response('res1');
+ const res2 = new Response('res2');
+
+ const cache = await caches.open('test');
+
+ await cache.put(req, res1);
+ await cache.put(req, res2);
+
+ const res = await cache.match(req).then((res) => res?.text());
+ console.log(res);
+ "#;
+
+ let part_two = r#"
+ const req = new Request("http://localhost/abc");
+ const res1 = new Response("res1");
+ const res2 = new Response("res2");
+
+ const cache = await caches.open("test");
+
+ // Swap the order of put() calls.
+ await cache.put(req, res2);
+ await cache.put(req, res1);
+
+ const res = await cache.match(req).then((res) => res?.text());
+ console.log(res);
+ "#;
+
+ temp_dir.write("cache_put.js", part_one);
+
+ let run_command =
+ test_context.new_command().args_vec(["run", "cache_put.js"]);
+
+ let output = run_command.run();
+ output.assert_matches_text("res2\n");
+ output.assert_exit_code(0);
+
+ // The wait will surface the bug as we check last written time
+ // when we overwrite a response.
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ temp_dir.write("cache_put.js", part_two);
+ let output = run_command.run();
+ output.assert_matches_text("res1\n");
+ output.assert_exit_code(0);
+}
diff --git a/cli/tests/unit/cache_api_test.ts b/cli/tests/unit/cache_api_test.ts
index b2682349a..2f807de44 100644
--- a/cli/tests/unit/cache_api_test.ts
+++ b/cli/tests/unit/cache_api_test.ts
@@ -173,3 +173,20 @@ Deno.test(async function cachePutFailedBody() {
// if it fails to read the body, the cache should be empty
assertEquals(response, undefined);
});
+
+Deno.test(async function cachePutOverwrite() {
+ const cacheName = "cache-v1";
+ const cache = await caches.open(cacheName);
+
+ const request = new Request("https://example.com/overwrite");
+ const res1 = new Response("res1");
+ const res2 = new Response("res2");
+
+ await cache.put(request, res1);
+ const res = await cache.match(request);
+ assertEquals(await res?.text(), "res1");
+
+ await cache.put(request, res2);
+ const res_ = await cache.match(request);
+ assertEquals(await res_?.text(), "res2");
+});