Skip to content

Commit c93e78a

Browse files
authored
misc: replace httpbin.org with a mock server (#68)
1 parent f11e365 commit c93e78a

File tree

4 files changed

+133
-120
lines changed

4 files changed

+133
-120
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ kotlin {
9696

9797
val kotlinVersion: String by project
9898
val coroutinesVersion: String by project
99+
val mockServerVersion: String by project
99100

100101
sourceSets {
101102
val commonMain by getting {
@@ -127,6 +128,7 @@ kotlin {
127128
api("org.jetbrains.kotlin:kotlin-test-junit5:$kotlinVersion")
128129
implementation("org.junit.jupiter:junit-jupiter:$junitVersion")
129130
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion")
131+
implementation("org.mock-server:mockserver-netty:$mockServerVersion")
130132
}
131133
}
132134

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ coroutinesVersion=1.6.4
1313
junitVersion=5.9.2
1414
ktlintVersion=0.48.1
1515
kotestVersion=5.5.4
16+
mockServerVersion=5.15.0
1617

1718
# elasticurl only
1819
kotlinxCliVersion=0.3.2

src/common/test/aws/sdk/kotlin/crt/http/HttpRequestResponseTest.kt

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)