Skip to content

Commit

Permalink
add a test cse for cookie auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Jan 13, 2025
1 parent 24ce55f commit 2c69493
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/http-client-csharp/emitter/test/Unit/auth.test.ts
Original file line number Diff line number Diff line change
@@ -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<ApiKeyLocation.cookie, "api-key-name">)`,
}
);
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
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface TypeSpecCompileOptions {
IsAzureCoreNeeded?: boolean;
IsTCGCNeeded?: boolean;
IsXmlNeeded?: boolean;
AuthDecorator?: string;
}

export async function typeSpecCompile(
Expand All @@ -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<ApiKeyLocation.header, "api-key">)`;
const namespace = `
@versioned(Versions)
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "api-key">)
${authDecorator}
@service({
title: "Azure Csharp emitter Testing",
})
Expand Down

0 comments on commit 2c69493

Please sign in to comment.