Search in sources :

Example 41 with Call

use of retrofit2.Call in project okhttp by square.

the class HttpOverHttp2Test method concurrentHttp2ConnectionsDeduplicated.

/**
   * We don't know if the connection will support HTTP/2 until after we've connected. When multiple
   * connections are requested concurrently OkHttp will pessimistically connect multiple times, then
   * close any unnecessary connections. This test confirms that behavior works as intended.
   *
   * <p>This test uses proxy tunnels to get a hook while a connection is being established.
   */
@Test
public void concurrentHttp2ConnectionsDeduplicated() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    // Force a fresh connection pool for the test.
    client.connectionPool().evictAll();
    final QueueDispatcher queueDispatcher = new QueueDispatcher();
    queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    queueDispatcher.enqueueResponse(new MockResponse().setBody("call2 response"));
    queueDispatcher.enqueueResponse(new MockResponse().setBody("call1 response"));
    // We use a re-entrant dispatcher to initiate one HTTPS connection while the other is in flight.
    server.setDispatcher(new Dispatcher() {

        int requestCount;

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            MockResponse result = queueDispatcher.dispatch(request);
            requestCount++;
            if (requestCount == 1) {
                // Before handling call1's CONNECT we do all of call2. This part re-entrant!
                try {
                    Call call2 = client.newCall(new Request.Builder().url("https://android.com/call2").build());
                    Response response2 = call2.execute();
                    assertEquals("call2 response", response2.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return result;
        }

        @Override
        public MockResponse peek() {
            return queueDispatcher.peek();
        }

        @Override
        public void shutdown() {
            queueDispatcher.shutdown();
        }
    });
    client = client.newBuilder().proxy(server.toProxyAddress()).build();
    Call call1 = client.newCall(new Request.Builder().url("https://android.com/call1").build());
    Response response2 = call1.execute();
    assertEquals("call1 response", response2.body().string());
    RecordedRequest call1Connect = server.takeRequest();
    assertEquals("CONNECT", call1Connect.getMethod());
    assertEquals(0, call1Connect.getSequenceNumber());
    RecordedRequest call2Connect = server.takeRequest();
    assertEquals("CONNECT", call2Connect.getMethod());
    assertEquals(0, call2Connect.getSequenceNumber());
    RecordedRequest call2Get = server.takeRequest();
    assertEquals("GET", call2Get.getMethod());
    assertEquals("/call2", call2Get.getPath());
    assertEquals(0, call2Get.getSequenceNumber());
    RecordedRequest call1Get = server.takeRequest();
    assertEquals("GET", call1Get.getMethod());
    assertEquals("/call1", call1Get.getPath());
    assertEquals(1, call1Get.getSequenceNumber());
    assertEquals(1, client.connectionPool().connectionCount());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) QueueDispatcher(okhttp3.mockwebserver.QueueDispatcher) IOException(java.io.IOException) QueueDispatcher(okhttp3.mockwebserver.QueueDispatcher) Dispatcher(okhttp3.mockwebserver.Dispatcher) MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) Test(org.junit.Test)

Example 42 with Call

use of retrofit2.Call in project okhttp by square.

the class HttpOverHttp2Test method userSuppliedContentLengthHeader.

@Test
public void userSuppliedContentLengthHeader() 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 {
            sink.write(postBytes);
        }
    }).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)

Example 43 with Call

use of retrofit2.Call in project okhttp by square.

the class HttpOverHttp2Test method recoverFromOneInternalErrorRequiresNewConnection.

@Test
public void recoverFromOneInternalErrorRequiresNewConnection() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.INTERNAL_ERROR.httpCode));
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().dns(new DoubleInetAddressDns()).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("abc", response.body().string());
    // New connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // New connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 44 with Call

use of retrofit2.Call in project okhttp by square.

the class HttpOverHttp2Test method emptyDataFrameSentWithEmptyBody.

@Test
public void emptyDataFrameSentWithEmptyBody() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC"));
    SocketRecorder socketRecorder = new SocketRecorder();
    client = client.newBuilder().sslSocketFactory(socketRecorder.sslSocketFactory(sslClient.socketFactory), sslClient.trustManager).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).method("DELETE", Util.EMPTY_REQUEST).build());
    Response response = call.execute();
    assertEquals("ABC", response.body().string());
    // Replay the bytes written by the client to confirm an empty data frame was sent.
    SocketRecorder.RecordedSocket recordedSocket = socketRecorder.takeSocket();
    Buffer buffer = new Buffer();
    buffer.write(recordedSocket.bytesWritten());
    RecordingHandler handler = new RecordingHandler();
    Http2Reader reader = new Http2Reader(buffer, false);
    reader.readConnectionPreface(null);
    while (reader.nextFrame(false, handler)) {
    }
    assertEquals(1, handler.headerFrameCount);
    assertEquals(Collections.singletonList(0), handler.dataFrames);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) SocketRecorder(okhttp3.internal.SocketRecorder) Test(org.junit.Test)

Example 45 with Call

use of retrofit2.Call in project okhttp by square.

the class HttpOverHttp2Test method get.

@Test
public void get() throws Exception {
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet"));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
}
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)

Aggregations

ResponseBody (okhttp3.ResponseBody)189 Request (okhttp3.Request)176 Test (org.junit.Test)155 Call (okhttp3.Call)127 Response (retrofit2.Response)107 Response (okhttp3.Response)105 Observable (rx.Observable)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 IOException (java.io.IOException)72 MockResponse (okhttp3.mockwebserver.MockResponse)67 RequestBody (okhttp3.RequestBody)51 Callback (okhttp3.Callback)44 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 OkHttpClient (okhttp3.OkHttpClient)33 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)27 Buffer (okio.Buffer)19 Call (retrofit2.Call)19 MultipartBody (okhttp3.MultipartBody)17 HttpUrl (okhttp3.HttpUrl)15 Callback (retrofit2.Callback)15