From d4edbea7566b417991ad78571d1491db231f5320 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Mon, 11 Nov 2024 14:57:19 +0800 Subject: [PATCH] fix(mockapi): dotnet compatibility failure dotnet uses different but compatible datetime format, so the comparison should use `coercedBodyEquals` --- .changeset/modern-dodos-act.md | 5 +++++ .../http/authentication/api-key/mockapi.ts | 10 +++++++++- .../http/authentication/http/custom/mockapi.ts | 10 +++++++++- .../cadl-ranch-specs/http/type/dictionary/mockapi.ts | 6 ++++++ .../type/property/additional-properties/mockapi.ts | 9 ++++++++- .../http/type/property/nullable/mockapi.ts | 9 +++++++++ .../http/type/property/optionality/mockapi.ts | 6 ++++++ .../http/type/property/value-types/mockapi.ts | 6 ++++++ 8 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 .changeset/modern-dodos-act.md diff --git a/.changeset/modern-dodos-act.md b/.changeset/modern-dodos-act.md new file mode 100644 index 000000000..5590d9770 --- /dev/null +++ b/.changeset/modern-dodos-act.md @@ -0,0 +1,5 @@ +--- +"@azure-tools/cadl-ranch-specs": patch +--- + +Fix dotnet compatibility issues in MockApi diff --git a/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts b/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts index ec7d4344b..9986d6d75 100644 --- a/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts +++ b/packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts @@ -1,4 +1,4 @@ -import { json, passOnCode, passOnSuccess } from "@azure-tools/cadl-ranch-api"; +import { json, passOnCode, passOnSuccess, MockRequest } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -32,5 +32,13 @@ Scenarios.Authentication_ApiKey_invalid = passOnCode(403, { error: "invalid-api-key", }), }, + handler: (req: MockRequest) => { + return { + status: 403, + body: json({ + error: "invalid-api-key", + }), + }; + }, kind: "MockApiDefinition", }); diff --git a/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts b/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts index 22ca12bde..08b9855fe 100644 --- a/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts +++ b/packages/cadl-ranch-specs/http/authentication/http/custom/mockapi.ts @@ -1,4 +1,4 @@ -import { json, passOnSuccess, passOnCode } from "@azure-tools/cadl-ranch-api"; +import { json, passOnSuccess, passOnCode, MockRequest } from "@azure-tools/cadl-ranch-api"; import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -32,5 +32,13 @@ Scenarios.Authentication_Http_Custom_invalid = passOnCode(403, { error: "invalid-api-key", }), }, + handler: (req: MockRequest) => { + return { + status: 403, + body: json({ + error: "invalid-api-key", + }), + }; + }, kind: "MockApiDefinition", }); diff --git a/packages/cadl-ranch-specs/http/type/dictionary/mockapi.ts b/packages/cadl-ranch-specs/http/type/dictionary/mockapi.ts index 15637cb15..72c9ecb10 100644 --- a/packages/cadl-ranch-specs/http/type/dictionary/mockapi.ts +++ b/packages/cadl-ranch-specs/http/type/dictionary/mockapi.ts @@ -23,6 +23,12 @@ function createServerTests(uri: string, data: any) { response: { status: 204, }, + handler: (req) => { + req.expect.coercedBodyEquals(data); + return { + status: 204, + }; + }, kind: "MockApiDefinition", }), }; diff --git a/packages/cadl-ranch-specs/http/type/property/additional-properties/mockapi.ts b/packages/cadl-ranch-specs/http/type/property/additional-properties/mockapi.ts index 5951136ec..07d945bc8 100644 --- a/packages/cadl-ranch-specs/http/type/property/additional-properties/mockapi.ts +++ b/packages/cadl-ranch-specs/http/type/property/additional-properties/mockapi.ts @@ -1,4 +1,4 @@ -import { passOnSuccess, ScenarioMockApi, json } from "@azure-tools/cadl-ranch-api"; +import { passOnSuccess, ScenarioMockApi, json, MockRequest } from "@azure-tools/cadl-ranch-api"; export const Scenarios: Record = {}; @@ -119,6 +119,13 @@ function createServerTests(url: string, value: any) { response: { status: 204, }, + handler: (req: MockRequest) => { + const expectedBody = JSON.parse(JSON.stringify(value)); + req.expect.coercedBodyEquals(expectedBody); + return { + status: 204, + }; + }, kind: "MockApiDefinition", }), }; diff --git a/packages/cadl-ranch-specs/http/type/property/nullable/mockapi.ts b/packages/cadl-ranch-specs/http/type/property/nullable/mockapi.ts index dd77fe439..ba0a4be35 100644 --- a/packages/cadl-ranch-specs/http/type/property/nullable/mockapi.ts +++ b/packages/cadl-ranch-specs/http/type/property/nullable/mockapi.ts @@ -29,6 +29,15 @@ function createServerTests(url: string, value: unknown, patchNullableProperty?: response: { status: 204, }, + handler: (req) => { + req.expect.coercedBodyEquals({ + requiredProperty: "foo", + nullableProperty: patchNullableProperty || null, + }); + return { + status: 204, + }; + }, kind: "MockApiDefinition", }), }; diff --git a/packages/cadl-ranch-specs/http/type/property/optionality/mockapi.ts b/packages/cadl-ranch-specs/http/type/property/optionality/mockapi.ts index 01049584a..35ffad084 100644 --- a/packages/cadl-ranch-specs/http/type/property/optionality/mockapi.ts +++ b/packages/cadl-ranch-specs/http/type/property/optionality/mockapi.ts @@ -23,6 +23,12 @@ function createServerTests(url: string, value: unknown) { response: { status: 204, }, + handler: (req) => { + req.expect.coercedBodyEquals(value); + return { + status: 204, + }; + }, kind: "MockApiDefinition", }), }; diff --git a/packages/cadl-ranch-specs/http/type/property/value-types/mockapi.ts b/packages/cadl-ranch-specs/http/type/property/value-types/mockapi.ts index 893576cda..220608b79 100644 --- a/packages/cadl-ranch-specs/http/type/property/value-types/mockapi.ts +++ b/packages/cadl-ranch-specs/http/type/property/value-types/mockapi.ts @@ -23,6 +23,12 @@ function createServerTests(url: string, data: unknown, convertedToFn?: (_: any) response: { status: 204, }, + handler: (req) => { + req.expect.coercedBodyEquals(data); + return { + status: 204, + }; + }, kind: "MockApiDefinition", }), };