@@ -12,14 +12,12 @@ import aws.smithy.kotlin.runtime.http.HttpStatusCode
1212import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineBase
1313import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineConfig
1414import aws.smithy.kotlin.runtime.http.engine.callContext
15- import aws.smithy.kotlin.runtime.http.readAll
1615import aws.smithy.kotlin.runtime.http.request.HttpRequest
1716import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
1817import aws.smithy.kotlin.runtime.http.response.HttpResponse
1918import aws.smithy.kotlin.runtime.operation.ExecutionContext
2019import aws.smithy.kotlin.runtime.time.Instant
2120import kotlin.test.assertEquals
22- import kotlin.test.assertTrue
2321
2422/* *
2523 * An expected HttpRequest with the response that should be returned by the engine
@@ -31,23 +29,13 @@ public data class MockRoundTrip(public val expected: HttpRequest?, public val re
3129/* *
3230 * Actual and expected [HttpRequest] pair
3331 */
34- public data class CallAssertion (public val expected : HttpRequest ? , public val actual : HttpRequest ) {
32+ public data class RequestComparands (public val expected : HttpRequest ? , public val actual : HttpRequest ) {
3533 /* *
36- * Assert that all of the components set on [expected] are also the same on [actual]. The actual request
37- * may have additional headers, only the ones set in [expected] are compared.
34+ * Assert that [expected] matches [actual] according to [asserter].
35+ * @param msgPrefix The prefix to include in the message if an [AssertionError] is thrown
3836 */
39- internal suspend fun assertRequest (idx : Int ) {
40- if (expected == null ) return
41- assertEquals(expected.url.toString(), actual.url.toString(), " [request#$idx ]: URL mismatch" )
42- expected.headers.forEach { name, values ->
43- values.forEach {
44- assertTrue(actual.headers.contains(name, it), " [request#$idx ]: header `$name ` missing value `$it `" )
45- }
46- }
47-
48- val expectedBody = expected.body.readAll()?.decodeToString()
49- val actualBody = actual.body.readAll()?.decodeToString()
50- assertEquals(expectedBody, actualBody, " [request#$idx ]: body mismatch" )
37+ internal suspend fun assertRequest (msgPrefix : String , asserter : CallAsserter ) {
38+ expected?.let { asserter.assertEquals(msgPrefix, it, actual) }
5139 }
5240}
5341
@@ -67,7 +55,7 @@ public class TestConnection(private val expected: List<MockRoundTrip> = emptyLis
6755
6856 // expected is mutated in-flight, store original size
6957 private val iter = expected.iterator()
70- private var calls = mutableListOf<CallAssertion >()
58+ private var requests = mutableListOf<RequestComparands >()
7159
7260 override val config: HttpClientEngineConfig = HttpClientEngineConfig .Default
7361
@@ -121,7 +109,7 @@ public class TestConnection(private val expected: List<MockRoundTrip> = emptyLis
121109 override suspend fun roundTrip (context : ExecutionContext , request : HttpRequest ): HttpCall {
122110 check(iter.hasNext()) { " TestConnection has no remaining expected requests" }
123111 val next = iter.next()
124- calls .add(CallAssertion (next.expected, request))
112+ requests .add(RequestComparands (next.expected, request))
125113
126114 val response = next.respondWith ? : HttpResponse (HttpStatusCode .OK , Headers .Empty , HttpBody .Empty )
127115 val now = Instant .now()
@@ -131,15 +119,15 @@ public class TestConnection(private val expected: List<MockRoundTrip> = emptyLis
131119 /* *
132120 * Get the list of captured HTTP requests so far
133121 */
134- public fun requests (): List <CallAssertion > = calls
122+ public fun requests (): List <RequestComparands > = requests
135123
136124 /* *
137125 * Assert that each captured request matches the expected
138126 */
139- public suspend fun assertRequests () {
140- assertEquals(expected.size, calls .size)
141- calls .forEachIndexed { idx, captured ->
142- captured.assertRequest(idx)
127+ public suspend fun assertRequests (asserter : CallAsserter = CallAsserter . FullyMatching ) {
128+ assertEquals(expected.size, requests .size)
129+ requests .forEachIndexed { idx, captured ->
130+ captured.assertRequest(" [request# $ idx] " , asserter )
143131 }
144132 }
145133}
0 commit comments