Skip to content

Commit 54b49f3

Browse files
JordonPhillipssrchase
authored andcommitted
Adjust ssdk protocol tests to use promises
1 parent 1406f81 commit 54b49f3

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,17 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest
281281
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
282282
Symbol serviceSymbol = serverSymbolProvider.toSymbol(service);
283283
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
284-
Symbol inputType = operationSymbol.expectProperty("inputType", Symbol.class);
285-
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);
286284

287285
// Create a mock function to set in place of the server operation function so we can capture
288286
// input and other information.
289287
writer.write("let testFunction = jest.fn();");
290-
writer.write("testFunction.mockReturnValue({});");
288+
writer.write("testFunction.mockReturnValue(Promise.resolve({}));");
291289

292290
// We use a partial here so that we don't have to define the entire service, but still get the advantages
293291
// the type checker, including excess property checking. Later on we'll use `as` to cast this to the
294292
// full service so that we can actually use it.
295293
writer.openBlock("const testService: Partial<$T> = {", "};", serviceSymbol, () -> {
296-
writer.addImport("Operation", "__Operation", "@aws-smithy/server-common");
297-
writer.write("$L: testFunction as __Operation<$T, $T>,",
298-
operationSymbol.getName(), inputType, outputType);
294+
writer.write("$L: testFunction as $T,", operationSymbol.getName(), operationSymbol);
299295
});
300296

301297
String getHandlerName = "get" + handlerSymbol.getName();
@@ -322,7 +318,6 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest
322318

323319
// Capture the input. We need to cast this to any so we can index into it.
324320
writer.write("let r: any = testFunction.mock.calls[0][0];").write("");
325-
326321
writeRequestParamAssertions(operation, testCase);
327322
});
328323
}
@@ -492,16 +487,16 @@ public void generateServerResponseTest(OperationShape operation, HttpResponseTes
492487
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
493488
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);
494489
writer.openBlock("class TestService implements Partial<$T> {", "}", serviceSymbol, () -> {
495-
writer.openBlock("$L(input: any, request: HttpRequest): $T {", "}",
490+
writer.openBlock("$L(input: any, request: HttpRequest): Promise<$T> {", "}",
496491
operationSymbol.getName(), outputType, () -> {
497492
Optional<ShapeId> outputOptional = operation.getOutput();
498493
if (outputOptional.isPresent()) {
499494
StructureShape outputShape = model.expectShape(outputOptional.get(), StructureShape.class);
500495
writer.writeInline("let response = ");
501496
testCase.getParams().accept(new CommandInputNodeVisitor(outputShape, true));
502-
writer.write("return { ...response, '$$metadata': {} };");
497+
writer.write("return Promise.resolve({ ...response, '$$metadata': {} });");
503498
} else {
504-
writer.write("return { '$$metadata': {} };");
499+
writer.write("return Promise.resolve({ '$$metadata': {} });");
505500
}
506501
});
507502
});
@@ -547,7 +542,7 @@ private void generateServerErrorResponseTest(
547542
// but using the partial in the meantime will give us proper type checking on the
548543
// operation we want.
549544
writer.openBlock("class TestService implements Partial<$T> {", "}", serviceSymbol, () -> {
550-
writer.openBlock("$L(input: any, request: HttpRequest): $T {", "}",
545+
writer.openBlock("$L(input: any, request: HttpRequest): Promise<$T> {", "}",
551546
operationSymbol.getName(), outputType, () -> {
552547
// Write out an object according to what's defined in the test case.
553548
writer.writeInline("const response = ");
@@ -608,7 +603,9 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC
608603
writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T, $1T, __SmithyException> = (op) =>"
609604
+ " { return new TestSerializer(); };", serviceOperationsSymbol, serviceSymbol);
610605

611-
writer.write("const handler = new $T(service, testMux, serFn);", handlerSymbol);
606+
writer.addImport("serializeFrameworkException", null,
607+
"./protocols/" + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
608+
writer.write("const handler = new $T(service, testMux, serFn, serializeFrameworkException);", handlerSymbol);
612609
writer.write("let r = await handler.handle(request)").write("");
613610
writeHttpResponseAssertions(testCase);
614611
}

0 commit comments

Comments
 (0)