From 293759914c1b96d34b1377dfa038940b005ee3a1 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 20 Sep 2024 11:12:31 +0200 Subject: [PATCH] fixup! add test --- fixtures/nodejs-hybrid-app/src/index.ts | 44 ++++++++++++++----- .../nodejs-hybrid-app/tests/index.test.ts | 17 ++++++- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/fixtures/nodejs-hybrid-app/src/index.ts b/fixtures/nodejs-hybrid-app/src/index.ts index 0f42419e47a9..1006ec9c0b86 100644 --- a/fixtures/nodejs-hybrid-app/src/index.ts +++ b/fixtures/nodejs-hybrid-app/src/index.ts @@ -16,27 +16,51 @@ export default { ctx: ExecutionContext ): Promise { const url = new URL(request.url); - if (url.pathname === "/test-random") { - return testGetRandomValues(); - } - - if (url.pathname === "/test-process") { - return testProcessBehaviour(); - } - if (url.pathname === "/query") { - return testPostgresLibrary(env, ctx); + switch (url.pathname) { + case "/test-random": + return testGetRandomValues(); + case "/test-process": + return testProcessBehaviour(); + case "/query": + return testPostgresLibrary(env, ctx); + case "/test-x509-certificate": + return testX509Certificate(); } return new Response( 'Postgres query | ' + 'Test process global | ' + - 'Test getRandomValues() | ', + 'Test getRandomValues() | ' + + 'Test X509Certificate', { headers: { "Content-Type": "text/html; charset=utf-8" } } ); }, }; +function testX509Certificate() { + try { + new nodeCrypto.X509Certificate(`-----BEGIN CERTIFICATE----- +MIICZjCCAc+gAwIBAgIUOsv8Y+x40C+gdNuu40N50KpGUhEwDQYJKoZIhvcNAQEL +BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM +GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA5MjAwOTA4MTNaFw0yNTA5 +MjAwOTA4MTNaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw +HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEB +BQADgY0AMIGJAoGBALpJn3dUrNmZhZV02RbjZKTd5j3hpgTncF4lG4Y3sQA18k0l +7pt6xpZuXYSFH7v2zTAxYy+uYyYwX2NZur48dZc76FSzIeuQdoTCkT0NacwFRTR5 +fEEqPvvB85ozYuyk8Bl3vSsonivOH3WftEDp9mjkHROQzS4wAZbIj7Cp+is/AgMB +AAGjUzBRMB0GA1UdDgQWBBSzFJSiPAw2tJOg8oUXrFBdqWI6zDAfBgNVHSMEGDAW +gBSzFJSiPAw2tJOg8oUXrFBdqWI6zDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3 +DQEBCwUAA4GBACbto0+Ds40F7faRFFMwg5nPyh7gsiX+ZK3FYcrO3oxh5ejfzwow +DKOOje4Ncaw0rIkVpxacPyjg+wANuK2Nv/Z4CVAD3mneE4gwgRdn38q8IYN9AtSv +GzEf4UxiLBbUB6WRBgyVyquGfUMlKl/tnm4q0yeYQloYKSoHpGeHVJuN +-----END CERTIFICATE-----`); + return new Response(`"OK!"`); + } catch { + return new Response(`"KO!"`); + } +} + function testGetRandomValues() { assert( webcrypto.getRandomValues === getRandomValues, diff --git a/fixtures/nodejs-hybrid-app/tests/index.test.ts b/fixtures/nodejs-hybrid-app/tests/index.test.ts index 21caf8b5744a..43479998939f 100644 --- a/fixtures/nodejs-hybrid-app/tests/index.test.ts +++ b/fixtures/nodejs-hybrid-app/tests/index.test.ts @@ -1,6 +1,6 @@ import { resolve } from "node:path"; import { fetch } from "undici"; -import { describe, it } from "vitest"; +import { describe, it, test } from "vitest"; import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; describe("nodejs compat", () => { @@ -48,4 +48,19 @@ describe("nodejs compat", () => { await stop(); } }); + + test("crypto.X509Certificate is implemented", async ({ expect }) => { + const { ip, port, stop } = await runWranglerDev( + resolve(__dirname, "../src"), + ["--port=0", "--inspector-port=0"] + ); + try { + const response = await fetch( + `http://${ip}:${port}/test-x509-certificate` + ); + await expect(response.text()).resolves.toBe(`"OK!"`); + } finally { + await stop(); + } + }); });