Skip to content

Commit 67b2a0d

Browse files
Look for proper tag on server error tests
1 parent 3c34bc5 commit 67b2a0d

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private void generateServerOperationTests(OperationShape operation, OperationInd
185185
});
186186
// 3. Generate test cases for each error on each operation.
187187
for (StructureShape error : operationIndex.getErrors(operation)) {
188-
if (!error.hasTag("server-only")) {
188+
if (!error.hasTag("client-only")) {
189189
error.getTrait(HttpResponseTestsTrait.class).ifPresent(trait -> {
190190
for (HttpResponseTestCase testCase : trait.getTestCasesFor(AppliesTo.SERVER)) {
191191
onlyIfProtocolMatches(testCase,
@@ -537,14 +537,25 @@ private void generateServerErrorResponseTest(
537537
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);
538538
Symbol errorSymbol = serverSymbolProvider.toSymbol(error);
539539
ErrorTrait errorTrait = error.expectTrait(ErrorTrait.class);
540+
540541
testCase.getDocumentation().ifPresent(writer::writeDocs);
541542
String testName = testCase.getId() + ":ServerErrorResponse";
542543
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
544+
545+
// Generates a Partial implementation of the service type that only includes
546+
// the specific operation under test. Later we'll have to "cast" this with an "as",
547+
// but using the partial in the meantime will give us proper type checking on the
548+
// operation we want.
543549
writer.openBlock("class TestService implements Partial<$T> {", "}", serviceSymbol, () -> {
544550
writer.openBlock("$L(input: any, request: HttpRequest): $T {", "}",
545551
operationSymbol.getName(), outputType, () -> {
552+
// Write out an object according to what's defined in the test case.
546553
writer.writeInline("const response = ");
547554
testCase.getParams().accept(new CommandInputNodeVisitor(error, true));
555+
556+
// Add in the necessary wrapping information to make the error satisfy its interface.
557+
// TODO: having proper constructors for these errors would be really nice so we don't
558+
// have to do this.
548559
writer.openBlock("const error: $T = {", "};", errorSymbol, () -> {
549560
writer.write("...response,");
550561
writer.write("name: $S,", error.getId().getName());
@@ -579,14 +590,19 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC
579590
});
580591
});
581592

582-
writer.write("const request = new HttpRequest({method: 'POST', hostname: 'example.com'});");
583-
593+
// Extend the existing serializer and replace the deserialize with a noop so we don't have to
594+
// worry about trying to construct something that matches.
584595
writer.openBlock("class TestSerializer extends $T {", "}", serializerSymbol, () -> {
585596
writer.openBlock("deserialize = (output: any, context: any): Promise<any> => {", "};", () -> {
586597
writer.write("return Promise.resolve({});");
587598
});
588599
});
589600

601+
// Since we aren't going through the deserializer, we don't have to put much in the fake request.
602+
// Just enough to get it through our test mux.
603+
writer.write("const request = new HttpRequest({method: 'POST', hostname: 'example.com'});");
604+
605+
// Create a new serializer factory that always returns our test serializer.
590606
writer.addImport("SmithyException", "__SmithyException", "@aws-sdk/smithy-client");
591607
writer.addImport("OperationSerializer", "__OperationSerializer", "@aws-smithy/server-common");
592608
writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T, $1T, __SmithyException> = (op) =>"

0 commit comments

Comments
 (0)