Skip to content

Commit

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

This PR is the `typespec-azure` counterpart of the PR
microsoft/typespec#4799. These changes are
required in the specs to handle the test success in `http-client-java`,
`http-client-net` and `http-client-python` packages. The scenarios are
also validated using the `test:e2e` script.

Please review and approve the PR. Thanks
  • Loading branch information
sarangan12 authored and markcowl committed Dec 5, 2024
1 parent 6cf7d4a commit f007306
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
12 changes: 6 additions & 6 deletions packages/azure-http-specs/spec-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
- `get /azure/client-generator-core/access/internalOperation/publicDecoratorInInternal`

This scenario contains internal operations. All should be generated but not exposed.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:

```json
{
"name": <any string>
"name": "sample"
}
```

Expand All @@ -24,12 +24,12 @@ Expected response body:
- `get /azure/client-generator-core/access/publicOperation/publicDecoratorInPublic`

This scenario contains public operations. It should be generated and exported.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:

```json
{
"name": <any string>
"name": "sample"
}
```

Expand All @@ -48,12 +48,12 @@ This scenario contains internal operations. All should be generated but not expo
- `get /azure/client-generator-core/access/sharedModelInOperation/internal`

This scenario contains two operations, one public, another internal. The public one should be generated and exported while the internal one should be generated but not exposed.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:

```json
{
"name": <any string>
"name": "sample"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace _Specs_.Azure.ClientGenerator.Core.Access;
@scenario
@scenarioDoc("""
This scenario contains public operations. It should be generated and exported.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:
```json
{
"name": <any string>
"name": "sample"
}
```
""")
Expand Down Expand Up @@ -51,11 +51,11 @@ namespace PublicOperation {
@scenario
@scenarioDoc("""
This scenario contains internal operations. All should be generated but not exposed.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:
```json
{
"name": <any string>
"name": "sample"
}
```
""")
Expand Down Expand Up @@ -98,11 +98,11 @@ namespace InternalOperation {
@scenario
@scenarioDoc("""
This scenario contains two operations, one public, another internal. The public one should be generated and exported while the internal one should be generated but not exposed.
Expected query parameter: name=<any string>
Expected query parameter: name="sample"
Expected response body:
```json
{
"name": <any string>
"name": "sample"
}
```
""")
Expand Down Expand Up @@ -157,14 +157,14 @@ namespace RelativeModelInOperation {
}

@doc("""
Expected query parameter: name=<any string>
Expected query parameter: name="Madge"
Expected response body:
```json
{
"name": <any string>,
"name": "Madge",
"inner":
{
"name": <any string>
"name": "Madge"
}
}
```
Expand All @@ -175,11 +175,11 @@ namespace RelativeModelInOperation {
op operation(@query name: string): OuterModel;

@doc("""
Expected query parameter: kind=<any string>
Expected query parameter: kind="real"
Expected response body:
```json
{
"name": <any string>,
"name": "real",
"kind": "real"
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ModelInOperation {
Expected body parameter:
```json
{
"name": <any string>
"name": "Madge"
}
```
""")
Expand All @@ -48,7 +48,7 @@ namespace ModelInOperation {
Expected response body:
```json
{
"name": <any string>
"name": "Madge"
}
```
""")
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace ModelInOperation {
```json
{
"result": {
"name": <any string>
"name": "Madge"
}
}
```
Expand Down
72 changes: 71 additions & 1 deletion packages/azure-http-specs/specs/azure/core/traits/mockapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { json, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
import {
json,
MockRequest,
passOnSuccess,
ScenarioMockApi,
validateValueFormat,
ValidationError,
} from "@typespec/spec-api";

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

Expand Down Expand Up @@ -32,6 +39,39 @@ Scenarios.Azure_Core_Traits_smokeTest = passOnSuccess({
"x-ms-client-request-id": "86aede1f-96fa-4e7f-b1e1-bf8a947cb804",
},
},
handler: (req: MockRequest) => {
if (!("x-ms-client-request-id" in req.headers)) {
throw new ValidationError(
"Should submit header x-ms-client-request-id",
"any uuid",
undefined,
);
}
if (req.params.id !== "1") {
throw new ValidationError("Expected path param id=1", "1", req.params.id);
}
req.expect.containsHeader("foo", "123");
const if_none_match = req.headers["if-none-match"];
const if_match = req.headers["if-match"];
if (if_none_match !== '"invalid"' && if_match !== '"valid"') {
throw new ValidationError(
`Expected header "if-none-match" equals "invalid" but got ${if_none_match} or "if-match" equals "valid" but got ${if_match}`,
`"if-match": "valid" or "if-none-match": "invalid"`,
`"if-match": ${if_match} or "if-none-match": ${if_none_match}`,
);
}
req.expect.containsHeader("if-unmodified-since", "Fri, 26 Aug 2022 14:38:00 GMT");
req.expect.containsHeader("if-modified-since", "Thu, 26 Aug 2021 14:38:00 GMT");
return {
status: 200,
body: json(validUser),
headers: {
bar: "456",
etag: "11bdc430-65e8-45ad-81d9-8ffa60d55b59",
"x-ms-client-request-id": req.headers["x-ms-client-request-id"],
},
};
},
kind: "MockApiDefinition",
});

Expand All @@ -57,5 +97,35 @@ Scenarios.Azure_Core_Traits_repeatableAction = passOnSuccess({
"repeatability-result": "accepted",
},
},
handler: (req: MockRequest) => {
if (req.params.id !== "1") {
throw new ValidationError("Expected path param id=1", "1", req.params.id);
}

if (!("repeatability-request-id" in req.headers)) {
throw new ValidationError("Repeatability-Request-ID is missing", "A UUID string", undefined);
}
if (!("repeatability-first-sent" in req.headers)) {
throw new ValidationError(
"Repeatability-First-Sent is missing",
"A date-time in headers format",
undefined,
);
}

validateValueFormat(req.headers["repeatability-request-id"], "uuid");
validateValueFormat(req.headers["repeatability-first-sent"], "rfc7231");

const validBody = { userActionValue: "test" };
req.expect.bodyEquals(validBody);

return {
status: 200,
body: json({ userActionResult: "test" }),
headers: {
"repeatability-result": "accepted",
},
};
},
kind: "MockApiDefinition",
});

0 comments on commit f007306

Please sign in to comment.