summaryrefslogtreecommitdiff
path: root/cli/tests/unit/fetch_test.ts
diff options
context:
space:
mode:
authorSean Michael Wykes <sean.wykes@nascent.com.br>2021-08-25 09:25:12 -0300
committerGitHub <noreply@github.com>2021-08-25 14:25:12 +0200
commitdccf4cbe36d66140f9e35a6ee755c3c440d745f9 (patch)
treeaf3114696f1649d77474f69cd3361d58aea34275 /cli/tests/unit/fetch_test.ts
parent5d814a4c244d489b4ae51002a0cf1d3c2fe16058 (diff)
feat(fetch): mTLS client certificates for fetch() (#11721)
This commit adds support for specifying client certificates when using fetch, by means of `Deno.createHttpClient`.
Diffstat (limited to 'cli/tests/unit/fetch_test.ts')
-rw-r--r--cli/tests/unit/fetch_test.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts
index 6e2b1a5d6..ed384dd4f 100644
--- a/cli/tests/unit/fetch_test.ts
+++ b/cli/tests/unit/fetch_test.ts
@@ -1211,3 +1211,83 @@ unitTest(
assertEquals(res.body, null);
},
);
+
+unitTest(
+ { perms: { read: true, net: true } },
+ async function fetchClientCertWrongPrivateKey(): Promise<void> {
+ await assertThrowsAsync(async () => {
+ const client = Deno.createHttpClient({
+ certChain: "bad data",
+ privateKey: await Deno.readTextFile(
+ "cli/tests/testdata/tls/localhost.key",
+ ),
+ });
+ await fetch("https://localhost:5552/fixture.json", {
+ client,
+ });
+ }, Deno.errors.InvalidData);
+ },
+);
+
+unitTest(
+ { perms: { read: true, net: true } },
+ async function fetchClientCertBadPrivateKey(): Promise<void> {
+ await assertThrowsAsync(async () => {
+ const client = Deno.createHttpClient({
+ certChain: await Deno.readTextFile(
+ "cli/tests/testdata/tls/localhost.crt",
+ ),
+ privateKey: "bad data",
+ });
+ await fetch("https://localhost:5552/fixture.json", {
+ client,
+ });
+ }, Deno.errors.InvalidData);
+ },
+);
+
+unitTest(
+ { perms: { read: true, net: true } },
+ async function fetchClientCertNotPrivateKey(): Promise<void> {
+ await assertThrowsAsync(async () => {
+ const client = Deno.createHttpClient({
+ certChain: await Deno.readTextFile(
+ "cli/tests/testdata/tls/localhost.crt",
+ ),
+ privateKey: "",
+ });
+ await fetch("https://localhost:5552/fixture.json", {
+ client,
+ });
+ }, Deno.errors.InvalidData);
+ },
+);
+
+unitTest(
+ { perms: { read: true, net: true } },
+ async function fetchCustomClientPrivateKey(): Promise<
+ void
+ > {
+ const data = "Hello World";
+ const client = Deno.createHttpClient({
+ certChain: await Deno.readTextFile(
+ "cli/tests/testdata/tls/localhost.crt",
+ ),
+ privateKey: await Deno.readTextFile(
+ "cli/tests/testdata/tls/localhost.key",
+ ),
+ caData: await Deno.readTextFile("cli/tests/testdata/tls/RootCA.crt"),
+ });
+ const response = await fetch("https://localhost:5552/echo_server", {
+ client,
+ method: "POST",
+ body: new TextEncoder().encode(data),
+ });
+ assertEquals(
+ response.headers.get("user-agent"),
+ `Deno/${Deno.version.deno}`,
+ );
+ await response.text();
+ client.close();
+ },
+);