From 84b8db34d5b4dfcbfc7d0e50d1e2c264ef91da36 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Thu, 16 Nov 2023 11:58:54 -0500 Subject: [PATCH] Fix a bug where we aren't checking the URL (#3211) ## Motivation and Context In #3059 there was an accidental deletion of a test ## Description Re-introduce checking the URL in protocol tests ## Checklist - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --- .../protocol/ProtocolTestGenerator.kt | 17 +++++++++++++++++ .../protocol/ProtocolTestGeneratorTest.kt | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt index f3ae7e782b..133b0dddf7 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGenerator.kt @@ -233,6 +233,7 @@ class DefaultProtocolTestGenerator( checkHeaders(this, "http_request.headers()", httpRequestTestCase.headers) checkForbidHeaders(this, "http_request.headers()", httpRequestTestCase.forbidHeaders) checkRequiredHeaders(this, "http_request.headers()", httpRequestTestCase.requireHeaders) + if (protocolSupport.requestBodySerialization) { // "If no request body is defined, then no assertions are made about the body of the message." httpRequestTestCase.body.orNull()?.also { body -> @@ -248,6 +249,22 @@ class DefaultProtocolTestGenerator( if (!httpRequestTestCase.vendorParams.isEmpty) { logger.warning("Test case provided vendorParams but these were ignored") } + + rustTemplate( + """ + let uri: #{Uri} = http_request.uri().parse().expect("invalid URI sent"); + #{AssertEq}(http_request.method(), ${method.dq()}, "method was incorrect"); + #{AssertEq}(uri.path(), ${uri.dq()}, "path was incorrect"); + """, + *codegenScope, + ) + + resolvedHost.orNull()?.also { host -> + rustTemplate( + """#{AssertEq}(uri.host().expect("host should be set"), ${host.dq()});""", + *codegenScope, + ) + } } } diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt index a9714a8a98..401e7b8007 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/protocol/ProtocolTestGeneratorTest.kt @@ -327,6 +327,23 @@ class ProtocolTestGeneratorTest { err.message shouldContain "required query param missing" } + @Test + fun `test invalid path`() { + val err = assertThrows { + testService( + """ + .uri("/incorrect-path?required&Hi=Hello%20there") + .header("X-Greeting", "Hi") + .method("POST") + """, + ) + } + + // Verify the test actually ran + err.message shouldContain "say_hello_request ... FAILED" + err.message shouldContain "path was incorrect" + } + @Test fun `invalid header`() { val err = assertThrows {