Skip to content

Commit 3f52467

Browse files
authored
chore: pass httpbindingresolver to protocol codegen tests for http method (#379)
1 parent 204c7db commit 3f52467

File tree

11 files changed

+47
-3
lines changed

11 files changed

+47
-3
lines changed

Packages/SmithyTestUtil/Sources/RequestTestUtil/HttpRequestTestBase.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ open class HttpRequestTestBase: XCTestCase {
3535
host: String,
3636
resolvedHost: String?) -> ExpectedSdkHttpRequest {
3737
let builder = ExpectedSdkHttpRequestBuilder()
38+
builder.withMethod(method)
3839

3940
if let deconflictedHost = deconflictHost(host: host, resolvedHost: resolvedHost) {
4041
builder.withHost(deconflictedHost)
@@ -217,7 +218,7 @@ open class HttpRequestTestBase: XCTestCase {
217218

218219
XCTAssertEqual(expected.endpoint.path, actual.endpoint.path)
219220
XCTAssertEqual(expected.endpoint.host, actual.endpoint.host)
220-
221+
XCTAssertEqual(expected.method, actual.method)
221222
assertForbiddenQueryItems(expected.forbiddenQueryItems, actual.queryItems)
222223

223224
assertRequiredQueryItems(expected.requiredQueryItems, actual.queryItems)

Packages/SmithyTestUtil/Tests/RequestTestUtilTests/HttpRequestTestBaseTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class HttpRequestTestBaseTests: HttpRequestTestBase {
191191
var operationStack = OperationStack<SayHelloInput, MockOutput, MockMiddlewareError>(id: "SayHelloInputRequest")
192192
operationStack.initializeStep.intercept(position: .before, middleware: SayHelloInputURLHostMiddleware(host: HttpRequestTestBaseTests.host))
193193
operationStack.buildStep.intercept(position: .after, id: "RequestTestEndpointResolver") { (context, input, next) -> Swift.Result<ClientRuntime.OperationOutput<MockOutput>, ClientRuntime.SdkError<MockMiddlewareError>> in
194+
input.withMethod(context.getMethod())
194195
let host = "\(context.getHostPrefix() ?? "")\(context.getHost() ?? "")"
195196
input.withHost(host)
196197
return next.handle(context: context, input: input)
@@ -248,7 +249,10 @@ class HttpRequestTestBaseTests: HttpRequestTestBase {
248249
return .success(output)
249250
})
250251

251-
let context = HttpContextBuilder().withEncoder(value: JSONEncoder()).build()
252+
let context = HttpContextBuilder()
253+
.withEncoder(value: JSONEncoder())
254+
.withMethod(value: .post)
255+
.build()
252256
_ = operationStack.handleMiddleware(context: context, input: input, next: MockHandler { (_, _) in
253257
XCTFail("Deserialize was mocked out, this should fail")
254258
let httpResponse = HttpResponse(body: .none, statusCode: .badRequest)

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolTestGenerator.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class HttpProtocolTestGenerator(
3535
private val errorTestBuilder: HttpProtocolUnitTestErrorGenerator.Builder,
3636
private val httpProtocolCustomizable: HttpProtocolCustomizable,
3737
private val operationMiddleware: OperationMiddleware,
38+
private val httpBindingResolver: HttpBindingResolver,
3839
private val serdeContext: HttpProtocolUnitTestGenerator.SerdeContext,
3940
private val imports: List<String> = listOf(),
4041
// list of test IDs to ignore/skip
@@ -116,6 +117,7 @@ class HttpProtocolTestGenerator(
116117
.testCases(requestTestCases)
117118
.httpProtocolCustomizable(httpProtocolCustomizable)
118119
.operationMiddleware(operationMiddleware)
120+
.httpBindingResolver(httpBindingResolver)
119121
.serdeContext(serdeContext)
120122
.build()
121123
.renderTestClass(testClassName)
@@ -150,6 +152,7 @@ class HttpProtocolTestGenerator(
150152
.testCases(responseTestCases)
151153
.httpProtocolCustomizable(httpProtocolCustomizable)
152154
.operationMiddleware(operationMiddleware)
155+
.httpBindingResolver(httpBindingResolver)
153156
.serdeContext(serdeContext)
154157
.build()
155158
.renderTestClass(testClassName)
@@ -192,6 +195,7 @@ class HttpProtocolTestGenerator(
192195
.testCases(testCases)
193196
.httpProtocolCustomizable(httpProtocolCustomizable)
194197
.operationMiddleware(operationMiddleware)
198+
.httpBindingResolver(httpBindingResolver)
195199
.serdeContext(serdeContext)
196200
.build()
197201
.renderTestClass(testClassName)

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolUnitTestGenerator.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected constructor(builder: Builder<T>) {
2727
protected val writer: SwiftWriter = builder.writer!!
2828
protected val httpProtocolCustomizable = builder.httpProtocolCustomizable!!
2929
protected val operationMiddleware = builder.operationMiddleware!!
30+
protected val httpBindingResolver = builder.httpBindingResolver!!
3031
protected val serdeContext = builder.serdeContext!!
3132
protected val serviceName: String = builder.serviceName!!
3233
abstract val baseTestClassName: String
@@ -78,6 +79,7 @@ protected constructor(builder: Builder<T>) {
7879
var serviceName: String? = null
7980
var httpProtocolCustomizable: HttpProtocolCustomizable? = null
8081
var operationMiddleware: OperationMiddleware? = null
82+
var httpBindingResolver: HttpBindingResolver? = null
8183
var serdeContext: SerdeContext? = null
8284

8385
fun symbolProvider(provider: SymbolProvider): Builder<T> = apply { this.symbolProvider = provider }
@@ -88,6 +90,7 @@ protected constructor(builder: Builder<T>) {
8890
fun serviceName(serviceName: String): Builder<T> = apply { this.serviceName = serviceName }
8991
fun httpProtocolCustomizable(httpProtocolCustomizable: HttpProtocolCustomizable): Builder<T> = apply { this.httpProtocolCustomizable = httpProtocolCustomizable }
9092
fun operationMiddleware(operationMiddleware: OperationMiddleware): Builder<T> = apply { this.operationMiddleware = operationMiddleware }
93+
fun httpBindingResolver(httpBindingResolver: HttpBindingResolver): Builder<T> = apply { this.httpBindingResolver = httpBindingResolver }
9194
fun serdeContext(serdeContext: SerdeContext): Builder<T> = apply { this.serdeContext = serdeContext }
9295
abstract fun build(): HttpProtocolUnitTestGenerator<T>
9396
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolUnitTestRequestGenerator.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package software.amazon.smithy.swift.codegen.integration
66

77
import software.amazon.smithy.codegen.core.Symbol
8+
import software.amazon.smithy.model.shapes.OperationShape
89
import software.amazon.smithy.model.shapes.Shape
910
import software.amazon.smithy.model.shapes.ShapeType
1011
import software.amazon.smithy.model.traits.HttpHeaderTrait
@@ -88,8 +89,10 @@ open class HttpProtocolUnitTestRequestGenerator protected constructor(builder: B
8889
writer.write("let context = HttpContextBuilder()")
8990
val idempotentMember = inputShape.members().firstOrNull() { it.hasTrait(IdempotencyTokenTrait::class.java) }
9091
val hasIdempotencyTokenTrait = idempotentMember != null
92+
val httpMethod = resolveHttpMethod(operation)
9193
writer.swiftFunctionParameterIndent {
9294
writer.write(" .withEncoder(value: encoder)")
95+
writer.write(" .withMethod(value: .$httpMethod)")
9396
if (hasIdempotencyTokenTrait) {
9497
writer.write(" .withIdempotencyTokenGenerator(value: QueryIdempotencyTestTokenGenerator())")
9598
}
@@ -116,6 +119,11 @@ open class HttpProtocolUnitTestRequestGenerator protected constructor(builder: B
116119
}
117120
}
118121

122+
private fun resolveHttpMethod(op: OperationShape): String {
123+
val httpTrait = httpBindingResolver.httpTrait(op)
124+
return httpTrait.method.toLowerCase()
125+
}
126+
119127
private fun renderMockDeserializeMiddleware(
120128
test: HttpRequestTestCase,
121129
operationStack: String,

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/RequestTestEndpointResolverMiddleware.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class RequestTestEndpointResolverMiddleware(private val model: Model, private va
2525
ClientRuntimeTypes.Middleware.OperationOutput,
2626
ClientRuntimeTypes.Core.SdkError
2727
) {
28+
writer.write("input.withMethod(context.getMethod())")
2829
writer.write("input.withPath(context.getPath())")
2930
writer.write("let host = \"\\(context.getHostPrefix() ?? \"\")\\(context.getHost() ?? \"\")\"")
3031
writer.write("input.withHost(host)")

0 commit comments

Comments
 (0)