diff options
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/cache_api_test.ts | 96 | ||||
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 1 | ||||
-rw-r--r-- | cli/tests/unit/test_util.ts | 1 |
3 files changed, 97 insertions, 1 deletions
diff --git a/cli/tests/unit/cache_api_test.ts b/cli/tests/unit/cache_api_test.ts new file mode 100644 index 000000000..4d7c6511b --- /dev/null +++ b/cli/tests/unit/cache_api_test.ts @@ -0,0 +1,96 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +import { + assert, + assertEquals, + assertFalse, + assertRejects, +} from "./test_util.ts"; + +Deno.test(async function cacheStorage() { + const cacheName = "cache-v1"; + const _cache = await caches.open(cacheName); + assert(await caches.has(cacheName)); + assert(await caches.delete(cacheName)); + assertFalse(await caches.has(cacheName)); +}); + +Deno.test(async function cacheApi() { + const cacheName = "cache-v1"; + const cache = await caches.open(cacheName); + // Test cache.put() with url string as key. + { + const req = "https://deno.com"; + await cache.put(req, new Response("deno.com - key is string")); + const res = await cache.match(req); + assertEquals(await res?.text(), "deno.com - key is string"); + assert(await cache.delete(req)); + } + // Test cache.put() with url instance as key. + { + const req = new URL("https://deno.com"); + await cache.put(req, new Response("deno.com - key is URL")); + const res = await cache.match(req); + assertEquals(await res?.text(), "deno.com - key is URL"); + assert(await cache.delete(req)); + } + // Test cache.put() with request instance as key. + { + const req = new Request("https://deno.com"); + await cache.put(req, new Response("deno.com - key is Request")); + const res = await cache.match(req); + assertEquals(await res?.text(), "deno.com - key is Request"); + assert(await cache.delete(req)); + } + + // Test cache.put() throws with response Vary header set to *. + { + const req = new Request("https://deno.com"); + assertRejects( + async () => { + await cache.put( + req, + new Response("deno.com - key is Request", { + headers: { Vary: "*" }, + }), + ); + }, + TypeError, + "Vary header must not contain '*'", + ); + } + + // Test cache.match() with same url but different values for Vary header. + { + await cache.put( + new Request("https://example.com/", { + headers: { + "Accept": "application/json", + }, + }), + Response.json({ msg: "hello world" }, { + headers: { + "Content-Type": "application/json", + "Vary": "Accept", + }, + }), + ); + const res = await cache.match("https://example.com/"); + assertEquals(res, undefined); + const res2 = await cache.match( + new Request("https://example.com/", { + headers: { "Accept": "text/html" }, + }), + ); + assertEquals(res2, undefined); + + const res3 = await cache.match( + new Request("https://example.com/", { + headers: { "Accept": "application/json" }, + }), + ); + assertEquals(await res3?.json(), { msg: "hello world" }); + } + + assert(await caches.delete(cacheName)); + assertFalse(await caches.has(cacheName)); +}); diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 7a531392d..36c1926f2 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -1782,7 +1782,6 @@ Deno.test( const blob = new Blob(["ok"], { type: "text/plain" }); const url = URL.createObjectURL(blob); const res = await fetch(url); - console.log(res); assert(res.url.startsWith("blob:http://js-unit-tests/")); assertEquals(res.status, 200); assertEquals(res.headers.get("content-length"), "2"); diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts index 4ad4b2575..19cad092d 100644 --- a/cli/tests/unit/test_util.ts +++ b/cli/tests/unit/test_util.ts @@ -6,6 +6,7 @@ import { resolve } from "../../../test_util/std/path/mod.ts"; export { assert, assertEquals, + assertFalse, assertMatch, assertNotEquals, assertRejects, |