Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@typespec/http-specs] - Modify code to handle http-client-* packages tests #4799

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/http-specs/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 8 additions & 1 deletion packages/http-specs/specs/encode/bytes/mockapi.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -71,6 +77,7 @@ function createPropertyServerTests(uri: string, data: any, value: any) {
},
response: {
status: 200,
body: json({ value: value }),
},
kind: "MockApiDefinition",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ export const expectedCreateBody = {
};

export const expectedUpdateBody = {
name: "Madge",
description: null,
map: {
key: {
name: "InnerMadge",
description: null,
},
key2: null,
Expand Down
9 changes: 8 additions & 1 deletion packages/http-specs/specs/payload/xml/mockapi.ts
Original file line number Diff line number Diff line change
@@ -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<string, ScenarioMockApi> = {};

Expand Down Expand Up @@ -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,
},
Expand Down
35 changes: 29 additions & 6 deletions packages/spector/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
`<?xml version='1.0' encoding='UTF-8'?>`,
"",
),
);
} 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 {
Expand All @@ -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));
}
}
});
}
Expand Down
Loading