Search in sources :

Example 6 with okhttp3

use of okhttp3 in project keywhiz by square.

the class StatusResourceIntegrationTest method isHealthy.

private boolean isHealthy() throws Exception {
    Request get = new Request.Builder().get().url(testUrl("/_status")).build();
    okhttp3.Response statusResponse = httpsClient.newCall(get).execute();
    return statusResponse.code() == 200;
}
Also used : okhttp3(okhttp3) Request(okhttp3.Request) Response(okhttp3.Response)

Example 7 with okhttp3

use of okhttp3 in project retrofit by square.

the class RetrofitTest method cloneSharesStatefulInstances.

@Test
public void cloneSharesStatefulInstances() {
    CallAdapter.Factory callAdapter = mock(CallAdapter.Factory.class);
    Converter.Factory converter = mock(Converter.Factory.class);
    HttpUrl baseUrl = server.url("/");
    Executor executor = mock(Executor.class);
    okhttp3.Call.Factory callFactory = mock(okhttp3.Call.Factory.class);
    Retrofit one = new Retrofit.Builder().addCallAdapterFactory(callAdapter).addConverterFactory(converter).baseUrl(baseUrl).callbackExecutor(executor).callFactory(callFactory).build();
    CallAdapter.Factory callAdapter2 = mock(CallAdapter.Factory.class);
    Converter.Factory converter2 = mock(Converter.Factory.class);
    Retrofit two = one.newBuilder().addCallAdapterFactory(callAdapter2).addConverterFactory(converter2).build();
    assertEquals(one.callAdapterFactories().size() + 1, two.callAdapterFactories().size());
    assertThat(two.callAdapterFactories()).contains(callAdapter, callAdapter2);
    assertEquals(one.converterFactories().size() + 1, two.converterFactories().size());
    assertThat(two.converterFactories()).contains(converter, converter2);
    assertSame(baseUrl, two.baseUrl());
    assertSame(executor, two.callbackExecutor());
    assertSame(callFactory, two.callFactory());
}
Also used : Executor(java.util.concurrent.Executor) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 8 with okhttp3

use of okhttp3 in project retrofit by square.

the class RequestBuilderTest method headerParamToString.

@Test
public void headerParamToString() {
    class Example {

        //
        @GET("/foo/bar/")
        Call<ResponseBody> method(@Header("kit") BigInteger kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, new BigInteger("1234"));
    assertThat(request.method()).isEqualTo("GET");
    okhttp3.Headers headers = request.headers();
    assertThat(headers.size()).isEqualTo(1);
    assertThat(headers.get("kit")).isEqualTo("1234");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Header(retrofit2.http.Header) Request(okhttp3.Request) BigInteger(java.math.BigInteger) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 9 with okhttp3

use of okhttp3 in project okhttp by square.

the class URLConnectionTest method disconnectDuringConnect_cookieJar.

@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
    final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
    class DisconnectingCookieJar implements CookieJar {

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            connectionHolder.get().disconnect();
            return Collections.emptyList();
        }
    }
    OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
    URL url = server.url("path that should never be accessed").url();
    HttpURLConnection connection = new OkHttpURLConnection(url, client);
    connectionHolder.set(connection);
    try {
        connection.getInputStream();
        fail("Connection should not be established");
    } catch (IOException expected) {
        assertEquals("Canceled", expected.getMessage());
    } finally {
        connection.disconnect();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with okhttp3

use of okhttp3 in project okhttp by square.

the class CallTest method asyncLeakedResponseBodyLogsStackTrace.

@Test
public void asyncLeakedResponseBodyLogsStackTrace() throws Exception {
    server.enqueue(new MockResponse().setBody("This gets leaked."));
    client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Level original = logger.getLevel();
    logger.setLevel(Level.FINE);
    logHandler.setFormatter(new SimpleFormatter());
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                fail();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // Ignore the response so it gets leaked then GC'd.
                latch.countDown();
            }
        });
        latch.await();
        // There's some flakiness when triggering a GC for objects in a separate thread. Adding a
        // small delay appears to ensure the objects will get GC'd.
        Thread.sleep(200);
        awaitGarbageCollection();
        String message = logHandler.take();
        assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
        assertTrue(message.contains("okhttp3.RealCall.enqueue("));
        assertTrue(message.contains("okhttp3.CallTest.asyncLeakedResponseBodyLogsStackTrace("));
    } finally {
        logger.setLevel(original);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(okhttp3.mockwebserver.MockResponse) Level(java.util.logging.Level) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)25 IOException (java.io.IOException)20 Test (org.junit.Test)16 ResponseBody (okhttp3.ResponseBody)12 OkHttpClient (okhttp3.OkHttpClient)10 Response (okhttp3.Response)9 RequestBody (okhttp3.RequestBody)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 Call (okhttp3.Call)5 Interceptor (okhttp3.Interceptor)5 Map (java.util.Map)4 Retrofit (retrofit2.Retrofit)4 Header (retrofit2.http.Header)4 List (java.util.List)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Response (retrofit2.Response)3 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)3 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Level (java.util.logging.Level)2