Skip to content

Commit c8593bc

Browse files
authored
fix content-length header not being sent when messages have empty bodies since streaming mode was used in these cases (#755)
1 parent e0da2a7 commit c8593bc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

fuel/src/main/kotlin/com/github/kittinunf/fuel/toolbox/HttpClient.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ class HttpClient(
234234

235235
private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {
236236
val body = request.body
237-
if (!connection.doOutput || body.isEmpty()) {
237+
if (!connection.doOutput) {
238+
return
239+
}
240+
if (body.isEmpty()) {
241+
connection.setFixedLengthStreamingMode(0L)
238242
return
239243
}
240244

fuel/src/test/kotlin/com/github/kittinunf/fuel/toolbox/HttpClientTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,19 @@ class HttpClientTest : MockHttpTestCase() {
5656
}
5757

5858
@Test
59-
fun setsContentLengthIfKnown() {
59+
fun setsContentLengthIfKnownZero() {
60+
val request = reflectedRequest(Method.POST, "content-length-test")
61+
.body("")
62+
63+
val (_, _, result) = request.responseObject(MockReflected.Deserializer())
64+
val (data, error) = result
65+
66+
assertThat("Expected data, actual error $error", data, notNullValue())
67+
assertThat(data!![Headers.CONTENT_LENGTH].firstOrNull(), equalTo("0"))
68+
}
69+
70+
@Test
71+
fun setsContentLengthIfKnownNonZero() {
6072
val request = reflectedRequest(Method.POST, "content-length-test")
6173
.body("my-body")
6274

0 commit comments

Comments
 (0)