Skip to content

Commit

Permalink
🐛 Charset issue fix (#718) (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaabr authored Jun 6, 2020
1 parent 3323c96 commit 37fd529
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun Body.representationOfBytes(contentType: String?): String {
var charset = Charsets.UTF_8
val charsetGroup = TEXT_CONTENT_TYPE.find(actualContentType)!!.groupValues[1]
if (charsetGroup.isNotEmpty()) {
val charsetName = charsetGroup.substringAfter('=').toUpperCase()
val charsetName = charsetGroup.substringAfter('=').substringBefore(';').toUpperCase()
charset = Charset.forName(charsetName)
}
return String(toByteArray(), charset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ class BodyRepresentationTest : MockHttpTestCase() {
}
}

@Test
fun textRepresentationOfJsonWithUtf8AndOtherParameters() {
val contentTypes = listOf(
"application/json;charset=utf-8;api-version=5.1",
"application/json; charset=utf-8; api-version=5.1",
"application/json;charset=utf-8;api-version=5.1;test=true",
"application/json; charset=utf-8; api-version=5.1; test=true"
)
val content = "{ \"foo\": 42 }"

contentTypes.forEach { contentType ->
assertThat(
DefaultBody
.from({ ByteArrayInputStream(content.toByteArray()) }, { content.length.toLong() })
.asString(contentType),
equalTo(content)
)
}
}

@Test
fun textRepresentationOfCsv() {
val contentTypes = listOf("text/csv")
Expand Down

0 comments on commit 37fd529

Please sign in to comment.