Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into HandleRemovalI
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangan12 committed Oct 14, 2024
2 parents f73d1ce + 224d71a commit 22518e0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/http-specs/specs/encode/bytes/mockapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ Scenarios.Encode_Bytes_RequestBody_base64 = createRequestBodyServerTests(
);
Scenarios.Encode_Bytes_RequestBody_base64url = createRequestBodyServerTests(
"/encode/bytes/body/request/base64url",
'"dGVzdA=="',
'"dGVzdA"',
{
"Content-Type": "application/json",
},
'"dGVzdA=="',
'"dGVzdA"',
);

Scenarios.Encode_Bytes_RequestBody_customContentType = createRequestBodyServerTests(
Expand Down
12 changes: 6 additions & 6 deletions packages/http-specs/specs/parameters/body-optionality/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
import { MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";

export const Scenarios: Record<string, ScenarioMockApi> = {};
function createServerTests(uri: string, data: any) {
Expand Down Expand Up @@ -39,14 +39,14 @@ Scenarios.Parameters_BodyOptionality_OptionalExplicit = passOnSuccess([
{
uri: "/parameters/body-optionality/optional-explicit/omit",
method: "post",
request: {
body: {
name: "foo",
},
},
request: {},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.rawBodyEquals(undefined);
return { status: 204 };
},
kind: "MockApiDefinition",
},
]);
Expand Down
43 changes: 35 additions & 8 deletions packages/http-specs/specs/routes/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
import { passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
import { MockRequest, passOnSuccess, ScenarioMockApi, ValidationError } from "@typespec/spec-api";

export const Scenarios: Record<string, ScenarioMockApi> = {};

function createTests(uri: string) {
const url = new URL("http://example.com" + uri);
const searchParams = url.searchParams;
const params: Record<string, any> = {};
for (const [key, value] of searchParams) {
params[key] = value;
const queryMap = new Map<string, string | string[]>();
for (const [key, value] of url.searchParams.entries()) {
if (queryMap.has(key)) {
const existing = queryMap.get(key)!;
if (Array.isArray(existing)) {
existing.push(value);
} else {
queryMap.set(key, [existing, value]);
}
} else {
queryMap.set(key, value);
}
}
return passOnSuccess({
uri: url.pathname,
method: "get",
request: {
params,
params: Object.fromEntries(queryMap),
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
for (const [key, value] of queryMap.entries()) {
if (Array.isArray(value)) {
req.expect.containsQueryParam(key, value, "multi");
} else {
req.expect.containsQueryParam(key, value);
}
}
for (const param of Object.keys(req.query)) {
if (!url.searchParams.has(param)) {
throw new ValidationError(
`Unexpected query parameter ${param}`,
undefined,
req.query[param],
);
}
}
return { status: 204 };
},
kind: "MockApiDefinition",
});
}
Expand Down Expand Up @@ -123,7 +150,7 @@ Scenarios.Routes_QueryParameters_QueryExpansion_Explode_primitive = createTests(
"/routes/query/query-expansion/explode/primitive?param=a",
);
Scenarios.Routes_QueryParameters_QueryExpansion_Explode_array = createTests(
"/routes/query/query-expansion/explode/array?param=a,b",
"/routes/query/query-expansion/explode/array?param=a&param=b",
);
Scenarios.Routes_QueryParameters_QueryExpansion_Explode_record = createTests(
"/routes/query/query-expansion/explode/record?a=1&b=2",
Expand All @@ -141,7 +168,7 @@ Scenarios.Routes_QueryParameters_QueryContinuation_Explode_primitive = createTes
"/routes/query/query-continuation/explode/primitive?fixed=true&param=a",
);
Scenarios.Routes_QueryParameters_QueryContinuation_Explode_array = createTests(
"/routes/query/query-continuation/explode/array?fixed=true&param=a,b",
"/routes/query/query-continuation/explode/array?fixed=true&param=a&param=b",
);
Scenarios.Routes_QueryParameters_QueryContinuation_Explode_record = createTests(
"/routes/query/query-continuation/explode/record?fixed=true&a=1&b=2",
Expand Down

0 comments on commit 22518e0

Please sign in to comment.