Skip to content

Commit

Permalink
[@typespec/http-specs] - Modify code to handle http-client-* packages…
Browse files Browse the repository at this point in the history
… tests (#4799)

While working with China Team in
Azure/cadl-ranch#755, I have found that a few
changes are required in the specs to handle the test success in
`http-client-java`, `http-client-net` and `http-client-python` packages.
This PR handles those changes. The scenarios are also validated using
the `test:e2e` script.

Please review and approve the PR. Thanks
  • Loading branch information
sarangan12 authored Oct 21, 2024
1 parent bb7d035 commit bf632a7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
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
2 changes: 0 additions & 2 deletions packages/http-specs/specs/payload/json-merge-patch/mockapi.ts
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

0 comments on commit bf632a7

Please sign in to comment.