|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +package aws.smithy.kotlin.runtime.httptest |
| 7 | + |
| 8 | +import aws.smithy.kotlin.runtime.http.Headers |
| 9 | +import aws.smithy.kotlin.runtime.http.HttpBody |
| 10 | +import aws.smithy.kotlin.runtime.http.HttpStatusCode |
| 11 | +import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineBase |
| 12 | +import aws.smithy.kotlin.runtime.http.engine.callContext |
| 13 | +import aws.smithy.kotlin.runtime.http.readAll |
| 14 | +import aws.smithy.kotlin.runtime.http.request.HttpRequest |
| 15 | +import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder |
| 16 | +import aws.smithy.kotlin.runtime.http.response.HttpCall |
| 17 | +import aws.smithy.kotlin.runtime.http.response.HttpResponse |
| 18 | +import aws.smithy.kotlin.runtime.time.Instant |
| 19 | +import kotlin.test.assertEquals |
| 20 | +import kotlin.test.assertTrue |
| 21 | + |
| 22 | +/** |
| 23 | + * An expected HttpRequest with the response that should be returned by the engine |
| 24 | + * @param expected the expected request. If null no assertions are made on the request |
| 25 | + * @param respondWith the response to return for this request. If null it defaults to an empty 200-OK response |
| 26 | + */ |
| 27 | +data class MockRoundTrip(val expected: HttpRequest?, val respondWith: HttpResponse? = null) |
| 28 | + |
| 29 | +/** |
| 30 | + * Actual and expected [HttpRequest] pair |
| 31 | + */ |
| 32 | +data class CallAssertion(val expected: HttpRequest?, val actual: HttpRequest) { |
| 33 | + /** |
| 34 | + * Assert that all of the components set on [expected] are also the same on [actual]. The actual request |
| 35 | + * may have additional headers, only the ones set in [expected] are compared. |
| 36 | + */ |
| 37 | + internal suspend fun assertRequest(idx: Int) { |
| 38 | + if (expected == null) return |
| 39 | + assertEquals(expected.url.toString(), actual.url.toString(), "[request#$idx]: URL mismatch") |
| 40 | + expected.headers.forEach { name, values -> |
| 41 | + values.forEach { |
| 42 | + assertTrue(actual.headers.contains(name, it), "[request#$idx]: header `$name` missing value `$it`") |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + val expectedBody = expected.body.readAll()?.decodeToString() |
| 47 | + val actualBody = actual.body.readAll()?.decodeToString() |
| 48 | + assertEquals(expectedBody, actualBody, "[request#$idx]: body mismatch") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * TestConnection implements [aws.smithy.kotlin.runtime.http.engine.HttpClientEngine] with canned responses. |
| 54 | + * For each expected request it will capture the actual and respond with the pre-configured response (or a basic 200-OK |
| 55 | + * with an empty body if none was configured). |
| 56 | + * |
| 57 | + * After all requests/responses have been made use [assertRequests] to test that the actual requests captured match |
| 58 | + * the expected. |
| 59 | + * |
| 60 | + * NOTE: This engine is only capable of modeling request/response pairs. More complicated interactions such as duplex |
| 61 | + * streaming are not implemented. |
| 62 | + */ |
| 63 | +class TestConnection(private val expected: List<MockRoundTrip> = emptyList()) : HttpClientEngineBase("TestConnection") { |
| 64 | + // expected is mutated in-flight, store original size |
| 65 | + private val iter = expected.iterator() |
| 66 | + private var calls = mutableListOf<CallAssertion>() |
| 67 | + |
| 68 | + override suspend fun roundTrip(request: HttpRequest): HttpCall { |
| 69 | + check(iter.hasNext()) { "TestConnection has no remaining expected requests" } |
| 70 | + val next = iter.next() |
| 71 | + calls.add(CallAssertion(next.expected, request)) |
| 72 | + |
| 73 | + val response = next.respondWith ?: HttpResponse(HttpStatusCode.OK, Headers.Empty, HttpBody.Empty) |
| 74 | + val now = Instant.now() |
| 75 | + return HttpCall(request, response, now, now, callContext()) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the list of captured HTTP requests so far |
| 80 | + */ |
| 81 | + fun requests(): List<CallAssertion> = calls |
| 82 | + |
| 83 | + /** |
| 84 | + * Assert that each captured request matches the expected |
| 85 | + */ |
| 86 | + suspend fun assertRequests() { |
| 87 | + assertEquals(expected.size, calls.size) |
| 88 | + calls.forEachIndexed { idx, captured -> |
| 89 | + captured.assertRequest(idx) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * DSL builder for [TestConnection] |
| 96 | + */ |
| 97 | +class HttpTestConnectionBuilder { |
| 98 | + val requests = mutableListOf<MockRoundTrip>() |
| 99 | + |
| 100 | + class HttpRequestResponsePairBuilder { |
| 101 | + internal val requestBuilder = HttpRequestBuilder() |
| 102 | + var response: HttpResponse? = null |
| 103 | + fun request(block: HttpRequestBuilder.() -> Unit) = requestBuilder.apply(block) |
| 104 | + } |
| 105 | + |
| 106 | + fun expect(block: HttpRequestResponsePairBuilder.() -> Unit) { |
| 107 | + val builder = HttpRequestResponsePairBuilder().apply(block) |
| 108 | + requests.add(MockRoundTrip(builder.requestBuilder.build(), builder.response)) |
| 109 | + } |
| 110 | + |
| 111 | + fun expect(request: HttpRequest, response: HttpResponse? = null) { |
| 112 | + requests.add(MockRoundTrip(request, response)) |
| 113 | + } |
| 114 | + |
| 115 | + fun expect(response: HttpResponse? = null) { |
| 116 | + requests.add(MockRoundTrip(null, response)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Invoke [block] with the given builder and construct a new [TestConnection] |
| 122 | + * |
| 123 | + * Example: |
| 124 | + * ```kotlin |
| 125 | + * val testEngine = buildTestConnection { |
| 126 | + * expect { |
| 127 | + * request { |
| 128 | + * url.host = "myhost" |
| 129 | + * headers.append("x-foo", "bar") |
| 130 | + * } |
| 131 | + * response = HttpResponse(HttpStatusCode.OK, Headers.Empty, HttpBody.Empty) |
| 132 | + * } |
| 133 | + * } |
| 134 | + * ``` |
| 135 | + */ |
| 136 | +fun buildTestConnection(block: HttpTestConnectionBuilder.() -> Unit): TestConnection { |
| 137 | + val builder = HttpTestConnectionBuilder().apply(block) |
| 138 | + return TestConnection(builder.requests) |
| 139 | +} |
0 commit comments