From 2c694939b7c23a5f2155108c6c4ad621ba099264 Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Mon, 13 Jan 2025 15:13:24 +0800 Subject: [PATCH] add a test cse for cookie auth --- .../emitter/test/Unit/auth.test.ts | 39 +++++++++++++++++++ .../emitter/test/Unit/utils/test-util.ts | 4 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/http-client-csharp/emitter/test/Unit/auth.test.ts diff --git a/packages/http-client-csharp/emitter/test/Unit/auth.test.ts b/packages/http-client-csharp/emitter/test/Unit/auth.test.ts new file mode 100644 index 0000000000..375aab5349 --- /dev/null +++ b/packages/http-client-csharp/emitter/test/Unit/auth.test.ts @@ -0,0 +1,39 @@ +import { TestHost } from "@typespec/compiler/testing"; +import { ok, strictEqual } from "assert"; +import { beforeEach, describe, it } from "vitest"; +import { createModel } from "../../src/lib/client-model-builder.js"; +import { + createEmitterContext, + createEmitterTestHost, + createNetSdkContext, + typeSpecCompile, +} from "./utils/test-util.js"; + +describe("Test auth", () => { + let runner: TestHost; + + beforeEach(async () => { + runner = await createEmitterTestHost(); + }); + + it("cookie header is not supported", async () => { + const program = await typeSpecCompile( + ` + op test(): NoContentResponse; + `, + runner, + { + AuthDecorator: `@useAuth(ApiKeyAuth)`, + } + ); + const context = createEmitterContext(program); + const sdkContext = await createNetSdkContext(context); + const root = createModel(sdkContext); + const diagnostics = context.program.diagnostics; + + const noAuthDiagnostic = diagnostics.find((d) => d.code === "@typespec/http-client-csharp/unsupported-auth"); + ok(noAuthDiagnostic); + strictEqual(noAuthDiagnostic.message, "Only header is supported for ApiKey authentication. cookie is not supported."); + strictEqual(root.Auth, undefined); // we do not support it therefore it falls back to undefined + }); +}); diff --git a/packages/http-client-csharp/emitter/test/Unit/utils/test-util.ts b/packages/http-client-csharp/emitter/test/Unit/utils/test-util.ts index 1e3b657c49..fa7e32788f 100644 --- a/packages/http-client-csharp/emitter/test/Unit/utils/test-util.ts +++ b/packages/http-client-csharp/emitter/test/Unit/utils/test-util.ts @@ -43,6 +43,7 @@ export interface TypeSpecCompileOptions { IsAzureCoreNeeded?: boolean; IsTCGCNeeded?: boolean; IsXmlNeeded?: boolean; + AuthDecorator?: string; } export async function typeSpecCompile( @@ -54,9 +55,10 @@ export async function typeSpecCompile( const needAzureCore = options?.IsAzureCoreNeeded ?? false; const needTCGC = options?.IsTCGCNeeded ?? false; const needXml = options?.IsXmlNeeded ?? false; + const authDecorator = options?.AuthDecorator ?? `@useAuth(ApiKeyAuth)`; const namespace = ` @versioned(Versions) - @useAuth(ApiKeyAuth) + ${authDecorator} @service({ title: "Azure Csharp emitter Testing", })