Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm MultipartBody and MultipartReader can work together #5999

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions okhttp/src/test/java/okhttp3/MultipartReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.io.EOFException
import java.net.ProtocolException
import okhttp3.Headers.Companion.headersOf
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okio.Buffer
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -499,4 +500,42 @@ class MultipartReaderTest {
} catch (expected: EOFException) {
}
}

/** Confirm that [MultipartBody] and [MultipartReader] can work together. */
@Test fun `multipart round trip`() {
val body = MultipartBody.Builder("boundary")
.setType(MultipartBody.PARALLEL)
.addPart("Quick".toRequestBody("text/plain".toMediaType()))
.addFormDataPart("color", "Brown")
.addFormDataPart("animal", "fox.txt", "Fox".toRequestBody())
.build()

val bodyContent = Buffer()
body.writeTo(bodyContent)

val reader = MultipartReader(bodyContent, "boundary")

val quickPart = reader.nextPart()!!
assertThat(quickPart.headers).isEqualTo(headersOf(
"Content-Type", "text/plain; charset=utf-8",
"Content-Length", "5"
))
assertThat(quickPart.body.readUtf8()).isEqualTo("Quick")

val brownPart = reader.nextPart()!!
assertThat(brownPart.headers).isEqualTo(headersOf(
"Content-Disposition", "form-data; name=\"color\"",
"Content-Length", "5"
))
assertThat(brownPart.body.readUtf8()).isEqualTo("Brown")

val foxPart = reader.nextPart()!!
assertThat(foxPart.headers).isEqualTo(headersOf(
"Content-Disposition", "form-data; name=\"animal\"; filename=\"fox.txt\"",
"Content-Length", "3"
))
assertThat(foxPart.body.readUtf8()).isEqualTo("Fox")

assertThat(reader.nextPart()).isNull()
}
}