diff --git a/packages/http-specs/package.json b/packages/http-specs/package.json index 988f9f9849..bc52c7a785 100644 --- a/packages/http-specs/package.json +++ b/packages/http-specs/package.json @@ -1,7 +1,7 @@ { "name": "@typespec/http-specs", "private": true, - "version": "0.37.2", + "version": "0.1.0", "description": "Spec scenarios and mock apis", "main": "dist/index.js", "type": "module", diff --git a/packages/http-specs/specs/encode/bytes/mockapi.ts b/packages/http-specs/specs/encode/bytes/mockapi.ts index f267462203..52da7ad126 100644 --- a/packages/http-specs/specs/encode/bytes/mockapi.ts +++ b/packages/http-specs/specs/encode/bytes/mockapi.ts @@ -1,5 +1,11 @@ import { resolvePath } from "@typespec/compiler"; -import { CollectionFormat, MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; +import { + CollectionFormat, + json, + MockRequest, + passOnSuccess, + ScenarioMockApi, +} from "@typespec/spec-api"; import { readFileSync } from "fs"; import { fileURLToPath } from "url"; @@ -71,6 +77,7 @@ function createPropertyServerTests(uri: string, data: any, value: any) { }, response: { status: 200, + body: json({ value: value }), }, kind: "MockApiDefinition", }); diff --git a/packages/http-specs/specs/payload/json-merge-patch/mockapi.ts b/packages/http-specs/specs/payload/json-merge-patch/mockapi.ts index 6fa60ba349..707cd4c302 100644 --- a/packages/http-specs/specs/payload/json-merge-patch/mockapi.ts +++ b/packages/http-specs/specs/payload/json-merge-patch/mockapi.ts @@ -27,11 +27,9 @@ export const expectedCreateBody = { }; export const expectedUpdateBody = { - name: "Madge", description: null, map: { key: { - name: "InnerMadge", description: null, }, key2: null, diff --git a/packages/http-specs/specs/payload/xml/mockapi.ts b/packages/http-specs/specs/payload/xml/mockapi.ts index d6273feec8..b97a07bdb5 100644 --- a/packages/http-specs/specs/payload/xml/mockapi.ts +++ b/packages/http-specs/specs/payload/xml/mockapi.ts @@ -1,4 +1,4 @@ -import { passOnSuccess, ScenarioMockApi, xml } from "@typespec/spec-api"; +import { MockRequest, passOnSuccess, ScenarioMockApi, xml } from "@typespec/spec-api"; export const Scenarios: Record = {}; @@ -144,6 +144,13 @@ function createServerTests(uri: string, data?: any) { "content-type": "application/xml", }, }, + handler: (req: MockRequest) => { + req.expect.containsHeader("content-type", "application/xml"); + req.expect.xmlBodyEquals(data); + return { + status: 204, + }; + }, response: { status: 204, }, diff --git a/packages/spector/src/app/app.ts b/packages/spector/src/app/app.ts index dc5072a867..7430a33f21 100644 --- a/packages/spector/src/app/app.ts +++ b/packages/spector/src/app/app.ts @@ -76,6 +76,10 @@ export class MockApiApp { } } +function isObject(value: any): boolean { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + function createHandler(apiDefinition: MockApiDefinition) { return (req: MockRequest) => { // Validate body if present in the request @@ -84,16 +88,27 @@ function createHandler(apiDefinition: MockApiDefinition) { apiDefinition.request.headers && apiDefinition.request.headers["Content-Type"] === "application/xml" ) { - req.expect.xmlBodyEquals(apiDefinition.request.body); + req.expect.xmlBodyEquals( + apiDefinition.request.body.rawContent.replace( + ``, + "", + ), + ); } else { - req.expect.coercedBodyEquals(apiDefinition.request.body); + if (isObject(apiDefinition.request.body)) { + Object.entries(apiDefinition.request.body).forEach(([key, value]) => { + req.expect.deepEqual(req.body[key], value); + }); + } else { + req.expect.coercedBodyEquals(apiDefinition.request.body); + } } } // Validate headers if present in the request if (apiDefinition.request.headers) { Object.entries(apiDefinition.request.headers).forEach(([key, value]) => { - if (key !== "Content-Type") { + if (key.toLowerCase() !== "content-type") { if (Array.isArray(value)) { req.expect.deepEqual(req.headers[key], value); } else { @@ -106,10 +121,18 @@ function createHandler(apiDefinition: MockApiDefinition) { // Validate query params if present in the request if (apiDefinition.request.params) { Object.entries(apiDefinition.request.params).forEach(([key, value]) => { - if (Array.isArray(value)) { - req.expect.deepEqual(req.query[key], value); + if (!req.query[key]) { + if (Array.isArray(value)) { + req.expect.deepEqual(req.params[key], value); + } else { + req.expect.deepEqual(req.params[key], String(value)); + } } else { - req.expect.containsQueryParam(key, String(value)); + if (Array.isArray(value)) { + req.expect.deepEqual(req.query[key], value); + } else { + req.expect.containsQueryParam(key, String(value)); + } } }); }