1

I am using retrofit and okhttp3.mockwebserver.MockWebServer for Android Test. I am getting this error while running a test.

Gradle:

 androidTestImplementation "com.squareup.okhttp3:mockwebserver:3.14.9"
def retrofit_version = "2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Android Test class has:

mockServer = MockWebServer()
mockServer.start()

//In some other class
Retrofit.Builder()
            .baseUrl(mockServer.url("/").toString())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
            .build()


mockServer.enqueue(
        MockResponse()
            .setResponseCode(200)
            .setBody(getJson(fileName)))

I don't know what is missing tried adding header but still getting same error. Please help.

snehal
  • 181
  • 1
  • 10
  • java.io.IOException: unexpected end of stream on http://localhost:35263/... at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:236) at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94) – snehal Mar 01 '21 at 10:53
  • Caused by: java.io.EOFException: \n not found: limit=0 content=… at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:348) at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242) at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213) ... 20 more – snehal Mar 01 '21 at 10:57

1 Answers1

0

Corrected sequence of calling the mock api before the progress bar is displayed and the error is gone.

snehal
  • 181
  • 1
  • 10