@@ -13,7 +13,7 @@ import retrofit2.*
1313import java.io.IOException
1414
1515internal class NetworkResponseCallTest {
16- private val errorConverter = Converter <ResponseBody , String > { it.toString () }
16+ private val errorConverter = Converter <ResponseBody , String > { it.string () }
1717 private val backingCall = CompletableCall <String >()
1818 private val networkResponseCall = NetworkResponseCall <String , String >(backingCall, errorConverter)
1919
@@ -75,6 +75,28 @@ internal class NetworkResponseCallTest {
7575 backingCall.completeWithException(HttpException (Response .error<String >(404 , " Server Error" .toResponseBody())))
7676 }
7777
78+ @Test
79+ fun `should parse error body correctly when ServerError occurs` () {
80+
81+ val errorBody = " An error occurred"
82+ val responseCode = 404
83+
84+ networkResponseCall.enqueue(object : Callback <NetworkResponse <String , String >> {
85+ override fun onResponse (call : Call <NetworkResponse <String , String >>, response : Response <NetworkResponse <String , String >>) {
86+ with (response.body()) {
87+ this as NetworkResponse .ServerError <String >
88+ body shouldBe errorBody
89+ code shouldBe responseCode
90+ }
91+ }
92+
93+ override fun onFailure (call : Call <NetworkResponse <String , String >>, t : Throwable ) {
94+ throw IllegalStateException ()
95+ }
96+ })
97+ backingCall.complete(Response .error(responseCode, errorBody.toResponseBody()))
98+ }
99+
78100 @Test
79101 fun `should parse unsuccessful call with IOException as NetworkResponse NetworkError` () {
80102 networkResponseCall.enqueue(object : Callback <NetworkResponse <String , String >> {
0 commit comments