Support json with UTF-8 bom in VersionInterceptor.

Previously it was peeking at response body and put it into intermediate
string that later used in json parsing. This preserves bom character
and leads to parse fail. Now it passes peek body input stream to
json parser that handles bom internally.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-05-15 22:04:09 +02:00 committed by Yahor Berdnikau
parent 6f4c8baeb5
commit 1c7e5bb0f2
3 changed files with 15 additions and 2 deletions

View File

@ -44,6 +44,16 @@ class VersionInterceptorTest : BaseInterceptorTest() {
.protocolVersion `should equal` SubsonicAPIVersions.V1_13_0
}
@Test
fun `Should update version from response with utf-8 bom`() {
mockWebServerRule.enqueueResponse("ping_ok_utf8_bom.json")
client.newCall(createRequest {}).execute()
(interceptor as VersionInterceptor)
.protocolVersion `should equal` SubsonicAPIVersions.V1_16_0
}
@Test
fun `Should not update version if response json doesn't contain version`() {
mockWebServerRule.enqueueResponse("non_subsonic_response.json")

View File

@ -0,0 +1,4 @@
{"subsonic-response":{
"status": "ok",
"version": "1.16.0"
}}

View File

@ -48,8 +48,7 @@ internal class VersionInterceptor(
}
private fun tryUpdateProtocolVersion(response: Response) {
val content = response.peekBody(DEFAULT_PEEK_BYTE_COUNT)
.byteStream().bufferedReader().readText()
val content = response.peekBody(DEFAULT_PEEK_BYTE_COUNT).byteStream()
try {
val jsonReader = jsonFactory.createParser(content)