Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/4855cecf-6a96-4622-b7ff-5d09e54f6564.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "4855cecf-6a96-4622-b7ff-5d09e54f6564",
"type": "bugfix",
"description": "Overwrite `Content-Length` header rather than appending in `CrtHttpEngine`"
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
kotlin-version = "2.2.0"
dokka-version = "2.0.0"

aws-kotlin-repo-tools-version = "0.4.55"
aws-kotlin-repo-tools-version = "0.4.56"

# libs
coroutines-version = "1.10.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal fun HttpRequest.toCrtRequest(callContext: CoroutineContext): aws.sdk.ko
}

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

return aws.sdk.kotlin.crt.http.HttpRequest(method.name, url.requestRelativePath, crtHeaders.build(), bodyStream)
Comment on lines 70 to 73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a new test for this? And also one that verifies the (presumably already correct?) behavior in OkHttp engine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
*/
package aws.smithy.kotlin.runtime.http.engine.crt

import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.http.Headers
import aws.smithy.kotlin.runtime.http.HttpMethod
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.http.toHttpBody
import aws.smithy.kotlin.runtime.net.url.Url
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

Expand Down Expand Up @@ -42,4 +50,19 @@ class RequestUtilTest {
assertFalse(isRetryable(code, name), "Expected $name to not be retryable!")
}
}

@Test
fun testContentLengthHeader() = runTest {
val data = "a".repeat(100)

val request = HttpRequest(
HttpMethod.POST,
url = Url.parse("https://notarealurl.com"),
headers = Headers { set("Content-Length", data.length.toString()) },
body = ByteStream.fromString(data).toHttpBody(),
)

val crtRequest = request.toCrtRequest(coroutineContext)
assertEquals(listOf(data.length.toString()), crtRequest.headers.getAll("Content-Length"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.http.*
import aws.smithy.kotlin.runtime.http.engine.internal.HttpClientMetrics
import aws.smithy.kotlin.runtime.http.request.HttpRequest
Expand Down Expand Up @@ -71,6 +72,22 @@ class OkHttpRequestTest {
assertEquals(listOf("bar", "baz"), actual.headers("FoO"))
}

@Test
fun itConvertsContentLengthHeader() {
val data = "a".repeat(100)
val url = Url.parse("https://aws.amazon.com")
val headers = Headers {
append("Content-Length", data.length.toString())
}
val request = HttpRequest(HttpMethod.POST, url, headers, ByteStream.fromString(data).toHttpBody())

val execContext = ExecutionContext()
val actual = request.toOkHttpRequest(execContext, EmptyCoroutineContext, testMetrics)

assertTrue(actual.headers.size >= 1)
assertEquals(listOf(data.length.toString()), actual.headers("Content-Length"))
}

@Test
fun itSupportsNonAsciiHeaderValues() {
val url = Url.parse("https://aws.amazon.com")
Expand Down
Loading