Search in sources :

Example 1 with Streaming

use of retrofit2.http.Streaming in project retrofit by square.

the class CallTest method responseBodyStreams.

@Test
public void responseBodyStreams() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("1234").setSocketPolicy(DISCONNECT_DURING_RESPONSE_BODY));
    Response<ResponseBody> response = example.getStreamingBody().execute();
    ResponseBody streamedBody = response.body();
    // When streaming we only detect socket problems as the ResponseBody is read.
    try {
        streamedBody.string();
        fail();
    } catch (IOException e) {
        assertThat(e).hasMessage("unexpected end of stream");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.junit.Test)1 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)1