Search in sources :

Example 11 with okhttp3

use of okhttp3 in project okhttp by square.

the class CallTest method leakedResponseBodyLogsStackTrace.

@Test
public void leakedResponseBodyLogsStackTrace() 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 {
        // Ignore the response so it gets leaked then GC'd.
        client.newCall(request).execute();
        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.execute("));
        assertTrue(message.contains("okhttp3.CallTest.leakedResponseBodyLogsStackTrace("));
    } finally {
        logger.setLevel(original);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Level(java.util.logging.Level) Test(org.junit.Test)

Example 12 with okhttp3

use of okhttp3 in project okhttp by square.

the class CallTest method canceledBeforeIOSignalsOnFailure.

/**
   * This test puts a request in front of one that is to be canceled, so that it is canceled before
   * I/O takes place.
   */
@Test
public void canceledBeforeIOSignalsOnFailure() throws Exception {
    // Force requests to be executed serially.
    okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(client.dispatcher().executorService());
    dispatcher.setMaxRequests(1);
    client = client.newBuilder().dispatcher(dispatcher).build();
    Request requestA = new Request.Builder().url(server.url("/a")).build();
    Request requestB = new Request.Builder().url(server.url("/b")).build();
    final Call callA = client.newCall(requestA);
    final Call callB = client.newCall(requestB);
    server.setDispatcher(new Dispatcher() {

        char nextResponse = 'A';

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            callB.cancel();
            return new MockResponse().setBody(Character.toString(nextResponse++));
        }
    });
    callA.enqueue(callback);
    callB.enqueue(callback);
    assertEquals("/a", server.takeRequest().getPath());
    callback.await(requestA.url()).assertBody("A");
    // At this point we know the callback is ready, and that it will receive a cancel failure.
    callback.await(requestB.url()).assertFailure("Canceled", "Socket closed");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 13 with okhttp3

use of okhttp3 in project picasso by square.

the class OkHttp3DownloaderTest method works.

@Test
public void works() throws Exception {
    server.enqueue(new MockResponse().setBody("Hi"));
    okhttp3.Request request = new Request.Builder().url(server.url("/")).build();
    Response response = downloader.load(request);
    assertThat(response.body().string()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) okhttp3(okhttp3) Test(org.junit.Test)

Example 14 with okhttp3

use of okhttp3 in project MovieGuide by esoxjem.

the class NetworkModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    return new okhttp3.OkHttpClient.Builder().connectTimeout(CONNECT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS).addInterceptor(loggingInterceptor).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 15 with okhttp3

use of okhttp3 in project feign by OpenFeign.

the class OkHttpClient method execute.

@Override
public feign.Response execute(feign.Request input, feign.Request.Options options) throws IOException {
    okhttp3.OkHttpClient requestScoped;
    if (delegate.connectTimeoutMillis() != options.connectTimeoutMillis() || delegate.readTimeoutMillis() != options.readTimeoutMillis()) {
        requestScoped = delegate.newBuilder().connectTimeout(options.connectTimeoutMillis(), TimeUnit.MILLISECONDS).readTimeout(options.readTimeoutMillis(), TimeUnit.MILLISECONDS).build();
    } else {
        requestScoped = delegate;
    }
    Request request = toOkHttpRequest(input);
    Response response = requestScoped.newCall(request).execute();
    return toFeignResponse(response).toBuilder().request(input).build();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

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