Skip to content

Commit 94b2959

Browse files
authored
fix: overwrite Content-Length rather than appending (#1407)
1 parent 03a25a4 commit 94b2959

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "4855cecf-6a96-4622-b7ff-5d09e54f6564",
3+
"type": "bugfix",
4+
"description": "Overwrite `Content-Length` header rather than appending in `CrtHttpEngine`"
5+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
kotlin-version = "2.2.0"
33
dokka-version = "2.0.0"
44

5-
aws-kotlin-repo-tools-version = "0.4.55"
5+
aws-kotlin-repo-tools-version = "0.4.56"
66

77
# libs
88
coroutines-version = "1.10.2"

runtime/protocol/http-client-engines/http-client-engine-crt/jvm/src/aws/smithy/kotlin/runtime/http/engine/crt/RequestUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal fun HttpRequest.toCrtRequest(callContext: CoroutineContext): aws.sdk.ko
6868
}
6969

7070
val contentLength = body.contentLength?.takeIf { it >= 0 }?.toString() ?: headers[CONTENT_LENGTH_HEADER]
71-
contentLength?.let { crtHeaders.append(CONTENT_LENGTH_HEADER, it) }
71+
contentLength?.let { crtHeaders[CONTENT_LENGTH_HEADER] = it }
7272

7373
return aws.sdk.kotlin.crt.http.HttpRequest(method.name, url.requestRelativePath, crtHeaders.build(), bodyStream)
7474
}

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,7 +4,15 @@
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.HttpMethod
10+
import aws.smithy.kotlin.runtime.http.request.HttpRequest
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
15+
import kotlin.test.assertEquals
816
import kotlin.test.assertFalse
917
import kotlin.test.assertTrue
1018

@@ -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)