Search in sources :

Example 56 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_rejectsSuffixRange.

@Test
public void rangeRequestsSupported_lengthUnknown_rejectsSuffixRange() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).addHeader("Range", "bytes=-5").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(416);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 57 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class WebServerDispatcherTest method rangeRequestsSupported_truncatesBoundedRangeToLength.

@Test
public void rangeRequestsSupported_truncatesBoundedRangeToLength() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Range", "bytes=5-25").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(206);
        assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
        assertThat(response.header("Content-Length")).isEqualTo("15");
        assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/20");
        assertThat(response.body().bytes()).isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20));
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 58 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class WebServerDispatcherTest method gzipDisabled_acceptEncodingHeaderAllowsAnyCoding_identityResponse.

@Test
public void gzipDisabled_acceptEncodingHeaderAllowsAnyCoding_identityResponse() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Accept-Encoding", "*").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(200);
        assertThat(response.header("Content-Encoding")).isEqualTo("identity");
        assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(RANGE_SUPPORTED_DATA.length));
        assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 59 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class WebServerDispatcherTest method gzipEnabled_acceptEncodingHeaderPrefersIdentity_responseNotGzipped.

@Test
public void gzipEnabled_acceptEncodingHeaderPrefersIdentity_responseNotGzipped() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(GZIP_ENABLED_PATH)).addHeader("Accept-Encoding", "identity;q=0.8, gzip;q=0.2").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(200);
        assertThat(response.header("Content-Encoding")).isEqualTo("identity");
        assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length));
        assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 60 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_startOnly.

@Test
public void rangeRequestsSupported_lengthUnknown_startOnly() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).addHeader("Range", "bytes=5-").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(206);
        assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
        assertThat(response.header("Content-Length")).isNull();
        assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/*");
        assertThat(response.body().contentLength()).isEqualTo(-1);
        // Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we
        // read exactly the number of bytes we expect.
        byte[] actualBytes = new byte[15];
        response.body().byteStream().read(actualBytes);
        assertThat(actualBytes).isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 20));
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85