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

http-specs, test case on status code range #5577

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http-specs"
---

Add test case on status code range
82 changes: 58 additions & 24 deletions packages/http-specs/spec-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,40 @@ Expected request body:
</SimpleModel>
```

### Response_StatusCodeRange_errorResponseStatusCode404

- Endpoint: `get /response/status-code-range/error-response-status-code-404`

Test case for range of status code in error response.

Verify that the result of the API is an error/exception in client, and the error response can be de-serialized to NotFoundError model (instead of Standard4XXError model).

Expected status code 404 and response body:

```json
{
"code": "not-found",
"resourceId": "resource1"
}
```

### Response_StatusCodeRange_errorResponseStatusCodeInRange

- Endpoint: `get /response/status-code-range/error-response-status-code-in-range`

Test case for range of status code in error response.

Verify that the result of the API is an error/exception in client, and the error response can be de-serialized to ErrorInRange model (instead of DefaultError model).

Expected status code 494 and response body:

```json
{
"code": "request-header-too-large",
"message": "Request header too large"
}
```

### Routes_fixed

- Endpoint: `get /routes/fixed`
Expand Down Expand Up @@ -2118,143 +2152,143 @@ Expected path: /routes/path/explicit/a

Test label expansion with explode: true when passed an array value.
Param value: ["a","b"]
Expected path: /routes/label/explode/array.a.b
Expected path: /routes/path/label/explode/array.a.b

### Routes_PathParameters_LabelExpansion_Explode_primitive

- Endpoint: `get /routes/path/label/explode/primitive{.param*}`

Test label expansion with explode: true when passed a primitive value.
Param value: "a"
Expected path: /routes/label/explode/primitive.a
Expected path: /routes/path/label/explode/primitive.a

### Routes_PathParameters_LabelExpansion_Explode_record

- Endpoint: `get /routes/path/label/explode/record{.param*}`

Test label expansion with explode: true when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/label/explode/record.a=1.b=2
Expected path: /routes/path/label/explode/record.a=1.b=2

### Routes_PathParameters_LabelExpansion_Standard_array

- Endpoint: `get /routes/path/label/standard/array{.param}`

Test label expansion with explode: false when passed an array value.
Param value: ["a","b"]
Expected path: /routes/label/standard/array.a,b
Expected path: /routes/path/label/standard/array.a,b

### Routes_PathParameters_LabelExpansion_Standard_primitive

- Endpoint: `get /routes/path/label/standard/primitive{.param}`

Test label expansion with explode: false when passed a primitive value.
Param value: "a"
Expected path: /routes/label/standard/primitive.a
Expected path: /routes/path/label/standard/primitive.a

### Routes_PathParameters_LabelExpansion_Standard_record

- Endpoint: `get /routes/path/label/standard/record{.param}`

Test label expansion with explode: false when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/label/standard/record.a,1,b,2
Expected path: /routes/path/label/standard/record.a,1,b,2

### Routes_PathParameters_MatrixExpansion_Explode_array

- Endpoint: `get /routes/path/matrix/explode/array{;param*}`

Test matrix expansion with explode: true when passed an array value.
Param value: ["a","b"]
Expected path: /routes/matrix/explode/array;a.b
weidongxu-microsoft marked this conversation as resolved.
Show resolved Hide resolved
Expected path: /routes/path/matrix/explode/array;a.b

### Routes_PathParameters_MatrixExpansion_Explode_primitive

- Endpoint: `get /routes/path/matrix/explode/primitive{;param*}`

Test matrix expansion with explode: true when passed a primitive value.
Param value: "a"
Expected path: /routes/matrix/explode/primitive;a
Expected path: /routes/path/matrix/explode/primitive;a

### Routes_PathParameters_MatrixExpansion_Explode_record

- Endpoint: `get /routes/path/matrix/explode/record{;param*}`

Test matrix expansion with explode: true when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/matrix/explode/record;a=1;b=2
Expected path: /routes/path/matrix/explode/record;a=1;b=2

### Routes_PathParameters_MatrixExpansion_Standard_array

- Endpoint: `get /routes/path/matrix/standard/array{;param}`

Test matrix expansion with explode: false when passed an array value.
Param value: ["a","b"]
Expected path: /routes/matrix/standard/array;a,b
Expected path: /routes/path/matrix/standard/array;a,b

### Routes_PathParameters_MatrixExpansion_Standard_primitive

- Endpoint: `get /routes/path/matrix/standard/primitive{;param}`

Test matrix expansion with explode: false when passed a primitive value.
Param value: "a"
Expected path: /routes/matrix/standard/primitive;a
Expected path: /routes/path/matrix/standard/primitive;a

### Routes_PathParameters_MatrixExpansion_Standard_record

- Endpoint: `get /routes/path/matrix/standard/record{;param}`

Test matrix expansion with explode: false when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/matrix/standard/record;a,1,b,2
Expected path: /routes/path/matrix/standard/record;a,1,b,2

### Routes_PathParameters_PathExpansion_Explode_array

- Endpoint: `get /routes/path/path/explode/array{/param*}`

Test path expansion with explode: true when passed an array value.
Param value: ["a","b"]
Expected path: /routes/path/explode/array/a/b
Expected path: /routes/path/path/explode/array/a/b

### Routes_PathParameters_PathExpansion_Explode_primitive

- Endpoint: `get /routes/path/path/explode/primitive{/param*}`

Test path expansion with explode: true when passed a primitive value.
Param value: "a"
Expected path: /routes/path/explode/primitive/a
Expected path: /routes/path/path/explode/primitive/a

### Routes_PathParameters_PathExpansion_Explode_record

- Endpoint: `get /routes/path/path/explode/record{/param*}`

Test path expansion with explode: true when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/path/explode/record/a=1/b=2
Expected path: /routes/path/path/explode/record/a=1/b=2

### Routes_PathParameters_PathExpansion_Standard_array

- Endpoint: `get /routes/path/path/standard/array{/param}`

Test path expansion with explode: false when passed an array value.
Param value: ["a","b"]
Expected path: /routes/path/standard/array/a,b
Expected path: /routes/path/path/standard/array/a,b

### Routes_PathParameters_PathExpansion_Standard_primitive

- Endpoint: `get /routes/path/path/standard/primitive{/param}`

Test path expansion with explode: false when passed a primitive value.
Param value: "a"
Expected path: /routes/path/standard/primitive/a
Expected path: /routes/path/path/standard/primitive/a

### Routes_PathParameters_PathExpansion_Standard_record

- Endpoint: `get /routes/path/path/standard/record{/param}`

Test path expansion with explode: false when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/path/standard/record/a,1,b,2
Expected path: /routes/path/path/standard/record/a,1,b,2

### Routes_PathParameters_ReservedExpansion_annotation

Expand All @@ -2278,47 +2312,47 @@ Expected path: "/routes/path/reserved-expansion/template/foo/bar%20baz"

Test simple expansion with explode: true when passed an array value.
Param value: ["a","b"]
Expected path: /routes/simple/explode/arraya.b
Expected path: /routes/path/simple/explode/arraya.b

### Routes_PathParameters_SimpleExpansion_Explode_primitive

- Endpoint: `get /routes/path/simple/explode/primitive{param*}`

Test simple expansion with explode: true when passed a primitive value.
Param value: "a"
Expected path: /routes/simple/explode/primitivea
Expected path: /routes/path/simple/explode/primitivea

### Routes_PathParameters_SimpleExpansion_Explode_record

- Endpoint: `get /routes/path/simple/explode/record{param*}`

Test simple expansion with explode: true when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/simple/explode/recorda=1,b=2
Expected path: /routes/path/simple/explode/recorda=1,b=2

### Routes_PathParameters_SimpleExpansion_Standard_array

- Endpoint: `get /routes/path/simple/standard/array{param}`

Test simple expansion with explode: false when passed an array value.
Param value: ["a","b"]
Expected path: /routes/simple/standard/arraya,b
Expected path: /routes/path/simple/standard/arraya,b

### Routes_PathParameters_SimpleExpansion_Standard_primitive

- Endpoint: `get /routes/path/simple/standard/primitive{param}`

Test simple expansion with explode: false when passed a primitive value.
Param value: "a"
Expected path: /routes/simple/standard/primitivea
Expected path: /routes/path/simple/standard/primitivea

### Routes_PathParameters_SimpleExpansion_Standard_record

- Endpoint: `get /routes/path/simple/standard/record{param}`

Test simple expansion with explode: false when passed a record value.
Param value: {a: 1, b: 2}
Expected path: /routes/simple/standard/recorda,1,b,2
Expected path: /routes/path/simple/standard/recorda,1,b,2

### Routes_PathParameters_templateOnly

Expand Down
82 changes: 82 additions & 0 deletions packages/http-specs/specs/response/status-code-range/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import "@typespec/http";
import "@typespec/spector";

using Http;
using Spector;

/**
* Test for range of status code.
*/
@scenarioService("/response/status-code-range")
namespace Response.StatusCodeRange;
weidongxu-microsoft marked this conversation as resolved.
Show resolved Hide resolved

@scenario
@scenarioDoc("""
Test case for range of status code in error response.

Verify that the result of the API is an error/exception in client, and the error response can be de-serialized to ErrorInRange model (instead of DefaultError model).

Expected status code 494 and response body:
```json
{
"code": "request-header-too-large",
"message": "Request header too large"
}
```
""")
@route("/error-response-status-code-in-range")
@get
op errorResponseStatusCodeInRange(): 204 | ErrorInRange | DefaultError;

@scenario
@scenarioDoc("""
Test case for range of status code in error response.

Verify that the result of the API is an error/exception in client, and the error response can be de-serialized to NotFoundError model (instead of Standard4XXError model).

Expected status code 404 and response body:
```json
{
"code": "not-found",
"resourceId": "resource1"
}
```
""")
@route("/error-response-status-code-404")
@get
op errorResponseStatusCode404(): 204 | NotFoundError | Standard4XXError;

@error
model NotFoundError {
@statusCode
_: 404;

code: string;
resourceId: string;
}

@error
model ErrorInRange {
@minValue(494)
@maxValue(499)
@statusCode
_: int32;

code: string;
message: string;
}

@error
model Standard4XXError {
@minValue(400)
@maxValue(499)
@statusCode
_: int32;

code: string;
}

@error
model DefaultError {
code: string;
}
35 changes: 35 additions & 0 deletions packages/http-specs/specs/response/status-code-range/mockapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { json, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";

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

Scenarios.Response_StatusCodeRange_errorResponseStatusCodeInRange = passOnSuccess({
uri: "/response/status-code-range/error-response-status-code-in-range",
method: "get",
request: {
status: 494,
},
response: {
status: 494,
body: json({
code: "request-header-too-large",
message: "Request header too large",
}),
},
kind: "MockApiDefinition",
});

Scenarios.Response_StatusCodeRange_errorResponseStatusCode404 = passOnSuccess({
uri: "/response/status-code-range/error-response-status-code-404",
method: "get",
request: {
status: 404,
},
response: {
status: 404,
body: json({
code: "not-found",
resourceId: "resource1",
}),
},
kind: "MockApiDefinition",
});
Loading