Search in sources :

Example 61 with OkHttpClient

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

the class WebServerDispatcherTest method rangeRequestsUnsupported_conventionalRequestWorksAsExpected.

@Test
public void rangeRequestsUnsupported_conventionalRequestWorksAsExpected() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_UNSUPPORTED_PATH)).build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(200);
        assertThat(response.header("Accept-Ranges")).isNull();
        assertThat(response.header("Content-Length")).isEqualTo("20");
        assertThat(response.header("Content-Range")).isNull();
        assertThat(response.body().bytes()).isEqualTo(RANGE_UNSUPPORTED_DATA);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 62 with OkHttpClient

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

the class WebServerDispatcherTest method rangeRequestsSupported_rejectsRangeWithEndBeforeStart.

@Test
public void rangeRequestsSupported_rejectsRangeWithEndBeforeStart() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Range", "bytes=15-10").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(416);
        assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
        assertThat(response.header("Content-Range")).isEqualTo("bytes */20");
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 63 with OkHttpClient

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

the class WebServerDispatcherTest method gzipDisabled_acceptEncodingHeaderRequiresGzip_406Response.

@Test
public void gzipDisabled_acceptEncodingHeaderRequiresGzip_406Response() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Accept-Encoding", "gzip;q=1.0").addHeader("Accept-Encoding", "identity;q=0").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(406);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 64 with OkHttpClient

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

the class WebServerDispatcherTest method gzipEnabled_acceptEncodingHeaderAllowsGzip_responseGzipped.

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

Example 65 with OkHttpClient

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

the class WebServerDispatcherTest method gzipForced_acceptEncodingHeaderAllowsGzip_responseGzipped.

@Test
public void gzipForced_acceptEncodingHeaderAllowsGzip_responseGzipped() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(GZIP_FORCED_PATH)).addHeader("Accept-Encoding", "gzip").build();
    try (Response response = client.newCall(request).execute()) {
        byte[] expectedData = Util.gzip(GZIP_FORCED_DATA);
        assertThat(response.code()).isEqualTo(200);
        assertThat(response.header("Content-Encoding")).isEqualTo("gzip");
        assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(expectedData.length));
        assertThat(response.body().bytes()).isEqualTo(expectedData);
    }
}
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