Skip to content

Commit 1b0ff52

Browse files
committed
Add Content-Length header tests
1 parent 54fd671 commit 1b0ff52

File tree

2 files changed

+40
-0
lines changed
  • runtime/protocol/http-client-engines
    • http-client-engine-crt/jvm/test/aws/smithy/kotlin/runtime/http/engine/crt
    • http-client-engine-okhttp/jvm/test/aws/smithy/kotlin/runtime/http/engine/okhttp

2 files changed

+40
-0
lines changed

runtime/protocol/http-client-engines/http-client-engine-crt/jvm/test/aws/smithy/kotlin/runtime/http/engine/crt/RequestUtilTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
*/
55
package aws.smithy.kotlin.runtime.http.engine.crt
66

7+
import aws.smithy.kotlin.runtime.content.ByteStream
8+
import aws.smithy.kotlin.runtime.http.Headers
9+
import aws.smithy.kotlin.runtime.http.request.HttpRequest
10+
import aws.smithy.kotlin.runtime.http.HttpMethod
11+
import aws.smithy.kotlin.runtime.http.toHttpBody
12+
import aws.smithy.kotlin.runtime.net.url.Url
13+
import kotlinx.coroutines.test.runTest
714
import kotlin.test.Test
815
import kotlin.test.assertFalse
916
import kotlin.test.assertTrue
17+
import kotlin.test.assertEquals
1018

1119
class RequestUtilTest {
1220
@Test
@@ -42,4 +50,19 @@ class RequestUtilTest {
4250
assertFalse(isRetryable(code, name), "Expected $name to not be retryable!")
4351
}
4452
}
53+
54+
@Test
55+
fun testContentLengthHeader() = runTest {
56+
val data = "a".repeat(100)
57+
58+
val request = HttpRequest(
59+
HttpMethod.POST,
60+
url = Url.parse("https://notarealurl.com"),
61+
headers = Headers { set("Content-Length", data.length.toString()) },
62+
body = ByteStream.fromString(data).toHttpBody()
63+
)
64+
65+
val crtRequest = request.toCrtRequest(coroutineContext)
66+
assertEquals(listOf(data.length.toString()), crtRequest.headers.getAll("Content-Length"))
67+
}
4568
}

runtime/protocol/http-client-engines/http-client-engine-okhttp/jvm/test/aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpRequestTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package aws.smithy.kotlin.runtime.http.engine.okhttp
77

8+
import aws.smithy.kotlin.runtime.content.ByteStream
89
import aws.smithy.kotlin.runtime.http.*
910
import aws.smithy.kotlin.runtime.http.engine.internal.HttpClientMetrics
1011
import aws.smithy.kotlin.runtime.http.request.HttpRequest
@@ -71,6 +72,22 @@ class OkHttpRequestTest {
7172
assertEquals(listOf("bar", "baz"), actual.headers("FoO"))
7273
}
7374

75+
@Test
76+
fun itConvertsContentLengthHeader() {
77+
val data = "a".repeat(100)
78+
val url = Url.parse("https://aws.amazon.com")
79+
val headers = Headers {
80+
append("Content-Length", data.length.toString())
81+
}
82+
val request = HttpRequest(HttpMethod.POST, url, headers, ByteStream.fromString(data).toHttpBody())
83+
84+
val execContext = ExecutionContext()
85+
val actual = request.toOkHttpRequest(execContext, EmptyCoroutineContext, testMetrics)
86+
87+
assertTrue(actual.headers.size >= 1)
88+
assertEquals(listOf(data.length.toString()), actual.headers("Content-Length"))
89+
}
90+
7491
@Test
7592
fun itSupportsNonAsciiHeaderValues() {
7693
val url = Url.parse("https://aws.amazon.com")

0 commit comments

Comments
 (0)