|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package aws.sdk.kotlin.crt.http |
| 7 | + |
| 8 | +import aws.sdk.kotlin.crt.runSuspendTest |
| 9 | +import aws.sdk.kotlin.crt.util.Digest |
| 10 | +import aws.sdk.kotlin.crt.util.encodeToHex |
| 11 | +import org.junit.jupiter.api.AfterAll |
| 12 | +import org.junit.jupiter.api.BeforeAll |
| 13 | +import org.junit.jupiter.api.TestInstance |
| 14 | +import org.mockserver.client.MockServerClient |
| 15 | +import org.mockserver.integration.ClientAndServer |
| 16 | +import org.mockserver.model.HttpRequest.request |
| 17 | +import org.mockserver.model.HttpResponse.response |
| 18 | +import kotlin.test.* |
| 19 | + |
| 20 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 21 | +class HttpRequestResponseTest : HttpClientTest() { |
| 22 | + private val TEST_DOC_LINE = |
| 23 | + "This is a sample to prove that http downloads and uploads work. It doesn't really matter what's in here, we mainly just need to verify the downloads and uploads work." |
| 24 | + private val TEST_DOC_SHA256 = "c7fdb5314b9742467b16bd5ea2f8012190b5e2c44a005f7984f89aab58219534" |
| 25 | + |
| 26 | + lateinit var mockServer: MockServerClient |
| 27 | + lateinit var url: String |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + fun setup() { |
| 31 | + mockServer = ClientAndServer.startClientAndServer(0) |
| 32 | + url = "http://localhost:${mockServer.port}" |
| 33 | + } |
| 34 | + |
| 35 | + @AfterAll |
| 36 | + fun tearDown() { |
| 37 | + mockServer.close() |
| 38 | + } |
| 39 | + |
| 40 | + // no body request |
| 41 | + private suspend fun testSimpleRequest(verb: String, path: String, expectedStatus: Int) { |
| 42 | + val expectedRequest = request().withMethod(verb).withPath(path) |
| 43 | + val unhandledRequest = request() |
| 44 | + |
| 45 | + // Set up the expected case |
| 46 | + mockServer.`when`(expectedRequest) |
| 47 | + .respond(response().withStatusCode(expectedStatus)) |
| 48 | + |
| 49 | + // Unhandled requests |
| 50 | + mockServer.`when`(unhandledRequest).respond(response().withStatusCode(500)) |
| 51 | + |
| 52 | + try { |
| 53 | + val response = roundTrip(url = url + path, verb = verb) |
| 54 | + assertEquals( |
| 55 | + expectedStatus, |
| 56 | + response.statusCode, |
| 57 | + "[$url]: expected http status ($expectedStatus) does not match", |
| 58 | + ) |
| 59 | + } catch (ex: Exception) { |
| 60 | + fail("[$url]: failed to round trip request: $ex") |
| 61 | + } finally { |
| 62 | + // Clean up |
| 63 | + mockServer.clear(expectedRequest) |
| 64 | + mockServer.clear(unhandledRequest) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun testHttpGet() = runSuspendTest { |
| 70 | + testSimpleRequest("GET", "/get", 200) |
| 71 | + testSimpleRequest("GET", "/post", 405) |
| 72 | + testSimpleRequest("GET", "/put", 405) |
| 73 | + testSimpleRequest("GET", "/delete", 405) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun testHttpPost() = runSuspendTest { |
| 78 | + testSimpleRequest("POST", "/get", 405) |
| 79 | + testSimpleRequest("POST", "/post", 200) |
| 80 | + testSimpleRequest("POST", "/put", 405) |
| 81 | + testSimpleRequest("POST", "/delete", 405) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun testHttpPut() = runSuspendTest { |
| 86 | + testSimpleRequest("PUT", "/get", 405) |
| 87 | + testSimpleRequest("PUT", "/post", 405) |
| 88 | + testSimpleRequest("PUT", "/put", 200) |
| 89 | + testSimpleRequest("PUT", "/delete", 405) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun testHttpDelete() = runSuspendTest { |
| 94 | + testSimpleRequest("DELETE", "/get", 405) |
| 95 | + testSimpleRequest("DELETE", "/post", 405) |
| 96 | + testSimpleRequest("DELETE", "/put", 405) |
| 97 | + testSimpleRequest("DELETE", "/delete", 200) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun testHttpDownload() = runSuspendTest { |
| 102 | + val response = roundTrip(url = "https://aws-crt-test-stuff.s3.amazonaws.com/http_test_doc.txt", verb = "GET") |
| 103 | + assertEquals(200, response.statusCode, "expected http status does not match") |
| 104 | + assertNotNull(response.body) |
| 105 | + |
| 106 | + assertEquals(TEST_DOC_SHA256, Digest.sha256(response.body).encodeToHex()) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun testHttpUpload() = runSuspendTest { |
| 111 | + val bodyToSend = TEST_DOC_LINE |
| 112 | + |
| 113 | + // Set up mock server |
| 114 | + val expectedRequest = request().withMethod("PUT").withPath("/anything") |
| 115 | + mockServer.`when`(expectedRequest) |
| 116 | + .respond(response().withStatusCode(200).withBody(bodyToSend)) |
| 117 | + |
| 118 | + val response = try { |
| 119 | + roundTrip(url = "$url/anything", verb = "PUT", body = bodyToSend) |
| 120 | + } finally { |
| 121 | + mockServer.clear(expectedRequest) |
| 122 | + } |
| 123 | + |
| 124 | + assertEquals(200, response.statusCode, "expected http status does not match") |
| 125 | + assertNotNull(response.body, "expected a response body for http upload") |
| 126 | + |
| 127 | + val bodyText = response.body.decodeToString() |
| 128 | + assertEquals(bodyToSend, bodyText) |
| 129 | + } |
| 130 | +} |
0 commit comments