Search in sources :

Example 16 with Call

use of zipkin2.Call in project okhttp by square.

the class OkHttpAsync method prepare.

@Override
public void prepare(final Benchmark benchmark) {
    concurrencyLevel = benchmark.concurrencyLevel;
    targetBacklog = benchmark.targetBacklog;
    client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
    if (benchmark.tls) {
        SslClient sslClient = SslClient.localhost();
        SSLSocketFactory socketFactory = sslClient.socketFactory;
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession session) {
                return true;
            }
        };
        client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    }
    callback = new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("Failed: " + e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            ResponseBody body = response.body();
            long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
            long finish = System.nanoTime();
            if (VERBOSE) {
                long start = (Long) response.request().tag();
                System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
            }
            requestsInFlight.decrementAndGet();
        }
    };
}
Also used : Call(okhttp3.Call) SslClient(okhttp3.internal.tls.SslClient) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) HostnameVerifier(javax.net.ssl.HostnameVerifier) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 17 with Call

use of zipkin2.Call in project okhttp by square.

the class HttpOverHttp2Test method receiveResponseCookies.

@Test
public void receiveResponseCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse().addHeader("set-cookie: a=b"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    cookieJar.assertResponseCookies("a=b; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordingCookieJar(okhttp3.RecordingCookieJar) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 18 with Call

use of zipkin2.Call in project okhttp by square.

the class HttpOverHttp2Test method connectionTimeout.

@Test
public void connectionTimeout() throws Exception {
    server.enqueue(new MockResponse().setBody("A").setBodyDelay(1, SECONDS));
    OkHttpClient client1 = client.newBuilder().readTimeout(2000, MILLISECONDS).build();
    Call call1 = client1.newCall(new Request.Builder().url(server.url("/")).build());
    OkHttpClient client2 = client.newBuilder().readTimeout(200, MILLISECONDS).build();
    Call call2 = client2.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    assertEquals("A", response1.body().string());
    try {
        call2.execute();
        fail();
    } catch (IOException expected) {
    }
    // Confirm that the connection was reused.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with Call

use of zipkin2.Call in project okhttp by square.

the class HttpOverHttp2Test method redirect.

@Test
public void redirect() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the new location!"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("This is the new location!", response.body().string());
    RecordedRequest request1 = server.takeRequest();
    assertEquals("/", request1.getPath());
    RecordedRequest request2 = server.takeRequest();
    assertEquals("/foo", request2.getPath());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 20 with Call

use of zipkin2.Call in project okhttp by square.

the class HttpOverHttp2Test method closeAfterFlush.

@Test
public void closeAfterFlush() throws Exception {
    final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
    server.enqueue(new MockResponse().setBody("ABCDE"));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain; charset=utf-8");
        }

        @Override
        public long contentLength() throws IOException {
            return postBytes.length;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            // push bytes into the stream's buffer
            sink.write(postBytes);
            // Http2Connection.writeData subject to write window
            sink.flush();
            // Http2Connection.writeData empty frame
            sink.close();
        }
    }).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    RecordedRequest request = server.takeRequest();
    assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
    assertArrayEquals(postBytes, request.getBody().readByteArray());
    assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Aggregations

Call (okhttp3.Call)409 Response (okhttp3.Response)309 Request (okhttp3.Request)282 IOException (java.io.IOException)232 Call (retrofit2.Call)134 Callback (okhttp3.Callback)133 OkHttpClient (okhttp3.OkHttpClient)98 Test (org.junit.Test)88 ResponseBody (okhttp3.ResponseBody)76 RequestBody (okhttp3.RequestBody)58 Retrofit (retrofit2.Retrofit)48 Gson (com.google.gson.Gson)47 Response (retrofit2.Response)47 File (java.io.File)44 Headers (okhttp3.Headers)41 Callback (retrofit2.Callback)41 GsonBuilder (com.google.gson.GsonBuilder)40 JSONObject (org.json.JSONObject)39 MockResponse (okhttp3.mockwebserver.MockResponse)38 List (java.util.List)35