Skip to content

Commit

Permalink
[cadl-ranch-*] Backport the server testing changes from typespec repo…
Browse files Browse the repository at this point in the history
…sitory to cadl-ranch repository (#741)

* Initial Commit

* Format and Changeset addition
  • Loading branch information
sarangan12 authored Oct 10, 2024
1 parent 23279d1 commit 352934c
Show file tree
Hide file tree
Showing 85 changed files with 7,406 additions and 3,291 deletions.
7 changes: 7 additions & 0 deletions .changeset/fifty-cherries-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@azure-tools/cadl-ranch-specs": patch
"@azure-tools/cadl-ranch-api": patch
"@azure-tools/cadl-ranch": patch
---

Backport the changes from typespec repository to migrate the scenarios to new model
32 changes: 30 additions & 2 deletions packages/cadl-ranch-api/src/scenarios.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { KeyedMockApi, MockApi, PassByKeyScenario, PassOnCodeScenario, PassOnSuccessScenario } from "./types.js";
import {
KeyedMockApi,
KeyedMockApiDefinition,
KeyedMockRequestHandler,

Check warning on line 4 in packages/cadl-ranch-api/src/scenarios.ts

View workflow job for this annotation

GitHub Actions / Lint

'KeyedMockRequestHandler' is defined but never used. Allowed unused vars must match /^_/u
MockApi,
MockApiDefinition,
PassByKeyScenario,
PassByServiceKeyScenario,
PassOnCodeScenario,
PassOnSuccessScenario,
} from "./types.js";

/**
* Specify that the scenario should be a `pass` if all the endpoints are called and the API response with 2xx exit code.
* @param apis Endpoint or List of endpoints for this scenario
*/
export function passOnSuccess(apis: MockApi | readonly MockApi[]): PassOnSuccessScenario {
export function passOnSuccess(
apis: MockApi | readonly MockApi[] | MockApiDefinition | readonly MockApiDefinition[],
): PassOnSuccessScenario {
return {
passCondition: "response-success",
apis: Array.isArray(apis) ? apis : [apis],
Expand Down Expand Up @@ -42,3 +54,19 @@ export function withKeys<const K extends string>(keys: K[]): WithKeysScenarioExp
},
};
}

export interface WithServiceKeysScenarioExpect<K extends string> {
pass(api: KeyedMockApiDefinition<K> | KeyedMockApiDefinition<K>[]): PassByServiceKeyScenario<K>;
}

export function withServiceKeys<const K extends string>(keys: K[]): WithServiceKeysScenarioExpect<K> {
return {
pass: (api: KeyedMockApiDefinition<K> | KeyedMockApiDefinition<K>[]) => {
return {
passCondition: "by-key",
keys,
apis: Array.isArray(api) ? api : [api],
};
},
};
}
44 changes: 41 additions & 3 deletions packages/cadl-ranch-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ export type ScenarioPassCondition = "response-success" | "status-code";

export interface PassOnSuccessScenario {
passCondition: "response-success";
apis: MockApi[];
apis: MockApi[] | MockApiDefinition[];
}

export interface PassOnCodeScenario {
passCondition: "status-code";
code: number;
apis: MockApi[];
apis: MockApi[] | MockApiDefinition[];
}

export interface PassByKeyScenario<K extends string = string> {
passCondition: "by-key";
keys: K[];
apis: KeyedMockApi<K>[];
}

export type ScenarioMockApi = PassOnSuccessScenario | PassOnCodeScenario | PassByKeyScenario;
export interface PassByServiceKeyScenario<K extends string = string> {
passCondition: "by-key";
keys: K[];
apis: KeyedMockApiDefinition<K>[];
}

export type ScenarioMockApi = PassOnSuccessScenario | PassOnCodeScenario | PassByKeyScenario | PassByServiceKeyScenario;
export type MockRequestHandler = SimpleMockRequestHandler | KeyedMockRequestHandler;
export type SimpleMockRequestHandler = (req: MockRequest) => MockResponse | Promise<MockResponse>;
export type KeyedMockRequestHandler<T extends string = string> = (
Expand All @@ -49,12 +56,43 @@ export interface MockApi {
method: HttpMethod;
uri: string;
handler: MockRequestHandler;
kind: "MockApi";
}

export interface MockApiDefinition {
uri: string;
method: HttpMethod;
request: ServiceRequest;
response: MockResponse;
handler?: MockRequestHandler;
kind: "MockApiDefinition";
}

export interface ServiceRequest {
body?: any;
status?: number;
/**
* Query parameters to match to the request.
*/
params?: Record<string, unknown>;
headers?: Record<string, unknown>;
files?: ServiceRequestFile[];
}

export interface ServiceRequestFile {
fieldname: string;
originalname: string;
buffer: Buffer;
mimetype: string;
}

export const Fail = Symbol.for("Fail");
export interface KeyedMockApi<K extends string> extends MockApi {
handler: KeyedMockRequestHandler<K>;
}
export interface KeyedMockApiDefinition<K extends string> extends MockApiDefinition {
handler: KeyedMockRequestHandler<K>;
}

export interface MockResponse {
status: number;
Expand Down
51 changes: 41 additions & 10 deletions packages/cadl-ranch-specs/http/authentication/api-key/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import { MockRequest } from "@azure-tools/cadl-ranch-api";
import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";
import { getValidAndInvalidScenarios } from "../commonapi.js";

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

const validAndInvalidScenarios = getValidAndInvalidScenarios(
"api-key",
"invalid-api-key",
function addOptionalParamOldApiVersionNewClientValidate(req: MockRequest): void {
Scenarios.Authentication_ApiKey_valid = passOnSuccess({
uri: `/authentication/api-key/valid`,
method: `get`,
request: {
headers: {
"x-ms-api-key": "valid-key",
},
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.containsHeader("x-ms-api-key", "valid-key");
return { status: 204 };
},
);

Scenarios.Authentication_ApiKey_valid = validAndInvalidScenarios.valid;
kind: "MockApiDefinition",
});

Scenarios.Authentication_ApiKey_invalid = validAndInvalidScenarios.invalid;
Scenarios.Authentication_ApiKey_invalid = passOnSuccess({
uri: `/authentication/api-key/invalid`,
method: `get`,
request: {
headers: {
"x-ms-api-key": "valid-key",
},
status: 403,
},
response: {
status: 403,
body: json({
error: "invalid-api-key",
}),
},
handler: (req: MockRequest) => {
return {
status: 403,
body: json({
error: "invalid-api-key",
}),
};
},
kind: "MockApiDefinition",
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import { MockRequest } from "@azure-tools/cadl-ranch-api";
import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";
import { getValidAndInvalidScenarios } from "../../commonapi.js";

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

const validAndInvalidScenarios = getValidAndInvalidScenarios(
"http/custom",
"invalid-api-key",
function addOptionalParamOldApiVersionNewClientValidate(req: MockRequest): void {
Scenarios.Authentication_Http_Custom_valid = passOnSuccess({
uri: `/authentication/http/custom/valid`,
method: "get",
request: {
headers: {
authorization: "SharedAccessKey valid-key",
},
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.containsHeader("authorization", "SharedAccessKey valid-key");
return { status: 204 };
},
);

Scenarios.Authentication_Http_Custom_valid = validAndInvalidScenarios.valid;
kind: "MockApiDefinition",
});

Scenarios.Authentication_Http_Custom_invalid = validAndInvalidScenarios.invalid;
Scenarios.Authentication_Http_Custom_invalid = passOnSuccess({
uri: `/authentication/http/custom/invalid`,
method: "get",
request: {
headers: {
authorization: "SharedAccessKey valid-key",
},
status: 403,
},
response: {
status: 403,
body: json({
error: "invalid-api-key",
}),
},
handler: (req: MockRequest) => {
return {
status: 403,
body: json({
error: "invalid-api-key",
}),
};
},
kind: "MockApiDefinition",
});
48 changes: 38 additions & 10 deletions packages/cadl-ranch-specs/http/authentication/oauth2/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import { MockRequest } from "@azure-tools/cadl-ranch-api";
import { json, MockRequest, passOnSuccess } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";
import { getValidAndInvalidScenarios } from "../commonapi.js";

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

const validAndInvalidScenarios = getValidAndInvalidScenarios(
"oauth2",
"invalid-grant",
function addOptionalParamOldApiVersionNewClientValidate(req: MockRequest): void {
Scenarios.Authentication_OAuth2_valid = passOnSuccess({
uri: `/authentication/oauth2/valid`,
method: "get",
request: {
headers: {
authorization: "Bearer https://security.microsoft.com/.default",
},
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.containsHeader("authorization", "Bearer https://security.microsoft.com/.default");
return { status: 204 };
},
);

Scenarios.Authentication_OAuth2_valid = validAndInvalidScenarios.valid;
kind: "MockApiDefinition",
});

Scenarios.Authentication_OAuth2_invalid = validAndInvalidScenarios.invalid;
Scenarios.Authentication_OAuth2_invalid = passOnSuccess({
uri: `/authentication/oauth2/invalid`,
method: "get",
request: {
status: 403,
},
response: {
status: 403,
body: json({
error: "invalid-grant",
}),
},
handler: (req: MockRequest) => {
return {
status: 403,
body: json({
error: "invalid-grant",
}),
};
},
kind: "MockApiDefinition",
});
40 changes: 31 additions & 9 deletions packages/cadl-ranch-specs/http/authentication/union/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import { passOnSuccess, mockapi } from "@azure-tools/cadl-ranch-api";
import { passOnSuccess, MockRequest } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";

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

Scenarios.Authentication_Union_validKey = passOnSuccess(
mockapi.get("/authentication/union/validkey", (req) => {
Scenarios.Authentication_Union_validKey = passOnSuccess({
uri: `/authentication/union/validkey`,
method: "get",
request: {
headers: {
"x-ms-api-key": "valid-key",
},
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.containsHeader("x-ms-api-key", "valid-key");
return { status: 204 };
}),
);
},
kind: "MockApiDefinition",
});

Scenarios.Authentication_Union_validToken = passOnSuccess(
mockapi.get("/authentication/union/validtoken", (req) => {
Scenarios.Authentication_Union_validToken = passOnSuccess({
uri: `/authentication/union/validtoken`,
method: "get",
request: {
headers: {
authorization: "Bearer https://security.microsoft.com/.default",
},
},
response: {
status: 204,
},
handler: (req: MockRequest) => {
req.expect.containsHeader("authorization", "Bearer https://security.microsoft.com/.default");
return { status: 204 };
}),
);
},
kind: "MockApiDefinition",
});
Loading

0 comments on commit 352934c

Please sign in to comment.