Skip to content

Commit 8952f33

Browse files
kstichsrchase
authored andcommitted
Add support for generating HTTP protocol tests
This commit adds support for generating protocol tests based on the `smithy.test#httpRequestTests` and `smithy.test#httpResponseTests` traits. Any service that has these traits will have a file generated in ./tests/functional named after the protocol that is being tested. These generated tests use the `jest` library and its expect/fail methods, along with custom mock handlers for testing the requests and responses. A custom node/shape visitor is needed to handle TS specific output typing in the generated code. This handles properly generating Date types, booleans, sets, and more. Individual body comparators are included as stubs to be added when the specific `bodyMediaType` they support are encountered. Some new dependencies are added when handling query, form-urlencoded, or XML components.
1 parent b181eda commit 8952f33

File tree

15 files changed

+760
-7
lines changed

15 files changed

+760
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ are as follows:
1818

1919
```kotlin
2020
plugins {
21-
id("software.amazon.smithy").version("0.3.0")
21+
id("software.amazon.smithy").version("0.4.3")
2222
}
2323

2424
dependencies {

smithy-typescript-codegen-test/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extra["moduleName"] = "software.amazon.smithy.typescript.codegen.test"
1919
tasks["jar"].enabled = false
2020

2121
plugins {
22-
id("software.amazon.smithy").version("0.4.1")
22+
id("software.amazon.smithy").version("0.4.3")
2323
}
2424

2525
repositories {
@@ -29,4 +29,5 @@ repositories {
2929

3030
dependencies {
3131
implementation(project(":smithy-typescript-codegen"))
32+
implementation("software.amazon.smithy:smithy-protocol-test-traits:0.9.7")
3233
}

smithy-typescript-codegen-test/model/main.smithy

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
$version: "0.4.0"
22
namespace example.weather
33

4+
use smithy.test#httpRequestTests
5+
use smithy.test#httpResponseTests
6+
47
/// Provides weather forecasts.
58
@protocols([{name: "aws.rest-json-1.1"}])
69
@paginated(inputToken: "nextToken", outputToken: "nextToken", pageSize: "pageSize")
@@ -39,6 +42,58 @@ operation GetCity {
3942
errors: [NoSuchResource]
4043
}
4144

45+
// Tests that HTTP protocol tests are generated.
46+
apply GetCity @httpRequestTests([
47+
{
48+
id: "WriteGetCityAssertions",
49+
documentation: "Does something",
50+
protocol: "aws.rest-json-1.1",
51+
method: "GET",
52+
uri: "/cities/123",
53+
body: "",
54+
params: {
55+
cityId: "123"
56+
}
57+
}
58+
])
59+
60+
apply GetCity @httpResponseTests([
61+
{
62+
id: "WriteGetCityResponseAssertions",
63+
documentation: "Does something",
64+
protocol: "aws.rest-json-1.1",
65+
code: 200,
66+
body: """
67+
{
68+
"name": "Seattle",
69+
"coordinates": {
70+
"latitude": 12.34,
71+
"longitude": -56.78
72+
},
73+
"city": {
74+
"cityId": "123",
75+
"name": "Seattle",
76+
"number": "One",
77+
"case": "Upper"
78+
}
79+
}""",
80+
bodyMediaType: "application/json",
81+
params: {
82+
name: "Seattle",
83+
coordinates: {
84+
latitude: 12.34,
85+
longitude: -56.78
86+
},
87+
city: {
88+
cityId: "123",
89+
name: "Seattle",
90+
number: "One",
91+
case: "Upper"
92+
}
93+
}
94+
}
95+
])
96+
4297
/// The input used to get a city.
4398
structure GetCityInput {
4499
// "cityId" provides the identifier for the resource and
@@ -80,6 +135,25 @@ structure NoSuchResource {
80135
message: String,
81136
}
82137

138+
apply NoSuchResource @httpResponseTests([
139+
{
140+
id: "WriteNoSuchResourceAssertions",
141+
documentation: "Does something",
142+
protocol: "aws.rest-json-1.1",
143+
code: 404,
144+
body: """
145+
{
146+
"resourceType": "City",
147+
"message": "Your custom message"
148+
}""",
149+
bodyMediaType: "application/json",
150+
params: {
151+
resourceType: "City",
152+
message: "Your custom message"
153+
}
154+
}
155+
])
156+
83157
// The paginated trait indicates that the operation may
84158
// return truncated results.
85159
@readonly
@@ -90,6 +164,22 @@ operation ListCities {
90164
output: ListCitiesOutput
91165
}
92166

167+
apply ListCities @httpRequestTests([
168+
{
169+
id: "WriteListCitiesAssertions",
170+
documentation: "Does something",
171+
protocol: "aws.rest-json-1.1",
172+
method: "GET",
173+
uri: "/cities",
174+
body: "",
175+
queryParams: ["pageSize=50"],
176+
forbidQueryParams: ["nextToken"],
177+
params: {
178+
pageSize: 50
179+
}
180+
}
181+
])
182+
93183
structure ListCitiesInput {
94184
@httpQuery("nextToken")
95185
nextToken: String,

smithy-typescript-codegen-test/smithy-build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"targetNamespace": "Weather",
77
"package": "weather",
88
"packageVersion": "0.0.1",
9+
"protocol": "aws.rest-json-1.1",
910
"packageJson": {
1011
"license": "Apache-2.0"
1112
}

smithy-typescript-codegen/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ extra["moduleName"] = "software.amazon.smithy.typescript.codegen"
1919

2020
dependencies {
2121
api("software.amazon.smithy:smithy-codegen-core:0.9.7")
22+
implementation("software.amazon.smithy:smithy-protocol-test-traits:0.9.7")
2223
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ void execute() {
171171
}
172172

173173
// Generate index for client.
174-
IndexGenerator.writeIndex(
175-
settings, model, symbolProvider, fileManifest);
174+
IndexGenerator.writeIndex(settings, model, symbolProvider, fileManifest);
175+
176+
// Generate protocol tests IFF found in the model.
177+
new HttpProtocolTestGenerator(settings, model, symbolProvider, writers).run();
176178

177179
// Write each pending writer.
178180
LOGGER.fine("Flushing TypeScript writers");

0 commit comments

Comments
 (0)