From 1c7e5bb0f236dba0e8d476130b846a35208a3404 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Tue, 15 May 2018 22:04:09 +0200 Subject: [PATCH] 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 --- .../subsonic/interceptors/VersionInterceptorTest.kt | 10 ++++++++++ .../integrationTest/resources/ping_ok_utf8_bom.json | 4 ++++ .../api/subsonic/interceptors/VersionInterceptor.kt | 3 +-- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 subsonic-api/src/integrationTest/resources/ping_ok_utf8_bom.json diff --git a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptorTest.kt b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptorTest.kt index 9b543fe9..3c1eccb4 100644 --- a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptorTest.kt +++ b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptorTest.kt @@ -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") diff --git a/subsonic-api/src/integrationTest/resources/ping_ok_utf8_bom.json b/subsonic-api/src/integrationTest/resources/ping_ok_utf8_bom.json new file mode 100644 index 00000000..e73dbbe6 --- /dev/null +++ b/subsonic-api/src/integrationTest/resources/ping_ok_utf8_bom.json @@ -0,0 +1,4 @@ +{"subsonic-response":{ + "status": "ok", + "version": "1.16.0" +}} \ No newline at end of file diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptor.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptor.kt index ae700f5a..e217c46b 100644 --- a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptor.kt +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/VersionInterceptor.kt @@ -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)