Search in sources :

Example 66 with OkHttpClient

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

the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_truncatesBoundedRangeToLength.

@Test
public void rangeRequestsSupported_lengthUnknown_truncatesBoundedRangeToLength() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_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/*");
        assertThat(response.body().bytes()).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)

Example 67 with OkHttpClient

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

the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_handlesConventionalRequest.

@Test
public void rangeRequestsSupported_lengthUnknown_handlesConventionalRequest() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.code()).isEqualTo(200);
        assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
        assertThat(response.header("Content-Length")).isNull();
        assertThat(response.header("Content-Range")).isNull();
        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[20];
        response.body().byteStream().read(actualBytes);
        assertThat(actualBytes).isEqualTo(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 68 with OkHttpClient

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

the class WebServerDispatcherTest method gzipForced_acceptEncodingHeaderExcludesGzip_responseNotGzipped.

@Test
public void gzipForced_acceptEncodingHeaderExcludesGzip_responseNotGzipped() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(mockWebServer.url(GZIP_FORCED_PATH)).addHeader("Accept-Encoding", "identity").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_FORCED_DATA.length));
        assertThat(response.body().bytes()).isEqualTo(GZIP_FORCED_DATA);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Test(org.junit.Test)

Example 69 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project dash-wallet by dashevo.

the class DynamicFeeLoader method fetchDynamicFees.

private static void fetchDynamicFees(final HttpUrl url, final File tempFile, final File targetFile, final String userAgent) {
    final Stopwatch watch = Stopwatch.createStarted();
    final Request.Builder requestBuilder = new Request.Builder().url(url).header("User-Agent", userAgent);
    if (targetFile.exists())
        requestBuilder.header("If-Modified-Since", HttpDate.format(new Date(targetFile.lastModified())));
    final OkHttpClient httpClient = Constants.HTTP_CLIENT.newBuilder().connectTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();
    final Call call = httpClient.newCall(requestBuilder.build());
    try {
        final Response response = call.execute();
        final int status = response.code();
        if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
            log.info("Dynamic fees not modified at {}, took {}", url, watch);
        } else if (status == HttpURLConnection.HTTP_OK) {
            final ResponseBody body = response.body();
            final FileOutputStream os = new FileOutputStream(tempFile);
            Io.copy(body.byteStream(), os);
            os.close();
            final Date lastModified = response.headers().getDate("Last-Modified");
            if (lastModified != null)
                tempFile.setLastModified(lastModified.getTime());
            body.close();
            if (!tempFile.renameTo(targetFile))
                throw new IllegalStateException("Cannot rename " + tempFile + " to " + targetFile);
            watch.stop();
            log.info("Dynamic fees fetched from {}, took {}", url, watch);
        } else {
            log.warn("HTTP status {} when fetching dynamic fees from {}", response.code(), url);
        }
    } catch (final Exception x) {
        log.warn("Problem when fetching dynamic fees rates from " + url, x);
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Stopwatch(com.google.common.base.Stopwatch) Request(okhttp3.Request) Date(java.util.Date) HttpDate(com.squareup.okhttp.internal.http.HttpDate) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) FileOutputStream(java.io.FileOutputStream)

Example 70 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project java-sdk by dapr.

the class DaprHttpClientTest method setUp.

@Before
public void setUp() throws Exception {
    mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
    okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockInterceptor(okhttp3.mock.MockInterceptor) Before(org.junit.Before)

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