Search in sources :

Example 1 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.

the class HttpLoggingInterceptorTest method duplexRequestsAreNotLogged.

@Test
public void duplexRequestsAreNotLogged() throws Exception {
    platform.assumeHttp2Support();
    platform.assumeNotBouncyCastle();
    // HTTP/2
    server.useHttps(handshakeCertificates.sslSocketFactory(), false);
    url = server.url("/");
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setBody("Hello response!"));
    RequestBody asyncRequestBody = new RequestBody() {

        @Override
        @Nullable
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8("Hello request!");
            sink.close();
        }

        @Override
        public boolean isDuplex() {
            return true;
        }
    };
    Request request = request().post(asyncRequestBody).build();
    Response response = client.newCall(request).execute();
    assumeTrue(response.protocol().equals(Protocol.HTTP_2));
    assertThat(response.body().string()).isEqualTo("Hello response!");
    applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("--> END POST (duplex request body omitted)").assertLogMatch("<-- 200 " + url + " \\(\\d+ms\\)").assertLogEqual("content-length: 15").assertLogEqual("").assertLogEqual("Hello response!").assertLogEqual("<-- END HTTP (15-byte body)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody) Test(org.junit.jupiter.api.Test)

Example 2 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.

the class HttpLoggingInterceptorTest method oneShotRequestsAreNotLogged.

@Test
public void oneShotRequestsAreNotLogged() throws Exception {
    url = server.url("/");
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setBody("Hello response!"));
    RequestBody asyncRequestBody = new RequestBody() {

        @Override
        @Nullable
        public MediaType contentType() {
            return null;
        }

        int counter = 0;

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            counter++;
            assertThat(counter).isLessThanOrEqualTo(1);
            sink.writeUtf8("Hello request!");
            sink.close();
        }

        @Override
        public boolean isOneShot() {
            return true;
        }
    };
    Request request = request().post(asyncRequestBody).build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().string()).isEqualTo("Hello response!");
    applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("--> END POST (one-shot body omitted)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 15").assertLogEqual("").assertLogEqual("Hello response!").assertLogEqual("<-- END HTTP (15-byte body)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.

the class DuplexTest method trueDuplexServerWritesFirst.

@Test
public void trueDuplexServerWritesFirst() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("response A\n").receiveRequest("request B\n").sendResponse("response C\n").receiveRequest("request D\n").sendResponse("response E\n").receiveRequest("request F\n").exhaustResponse().exhaustRequest());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response A");
        requestBody.writeUtf8("request B\n");
        requestBody.flush();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response C");
        requestBody.writeUtf8("request D\n");
        requestBody.flush();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response E");
        requestBody.writeUtf8("request F\n");
        requestBody.flush();
        assertThat(responseBody.readUtf8Line()).isNull();
        requestBody.close();
    }
    mockDuplexResponseBody.awaitSuccess();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 4 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.

the class DuplexTest method duplexWith100Continue.

@Test
public void duplexWith100Continue() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().setSocketPolicy(SocketPolicy.EXPECT_CONTINUE), new MockDuplexResponseBody().receiveRequest("request body\n").sendResponse("response body\n").exhaustRequest());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
        requestBody.writeUtf8("request body\n");
        requestBody.flush();
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response body");
        requestBody.close();
        assertThat(responseBody.readUtf8Line()).isNull();
    }
    mockDuplexResponseBody.awaitSuccess();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 5 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.

the class DuplexTest method duplexWithRedirect.

/**
 * Duplex calls that have follow-ups are weird. By the time we know there's a follow-up we've
 * already split off another thread to stream the request body. Because we permit at most one
 * exchange at a time we break the request stream out from under that writer.
 */
@Test
public void duplexWithRedirect() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    CountDownLatch duplexResponseSent = new CountDownLatch(1);
    listener = new RecordingEventListener() {

        @Override
        public void responseHeadersEnd(Call call, Response response) {
            try {
                // Wait for the server to send the duplex response before acting on the 301 response
                // and resetting the stream.
                duplexResponseSent.await();
            } catch (InterruptedException e) {
                throw new AssertionError();
            }
            super.responseHeadersEnd(call, response);
        }
    };
    client = client.newBuilder().eventListener(listener).build();
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /b"), new MockDuplexResponseBody().sendResponse("/a has moved!\n", duplexResponseSent).requestIOException().exhaustResponse());
    server.enqueue(new MockResponse().setBody("this is /b"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("this is /b");
    }
    BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
    try {
        requestBody.writeUtf8("request body\n");
        requestBody.flush();
        fail();
    } catch (IOException expected) {
        assertThat(expected.getMessage()).isEqualTo("stream was reset: CANCEL");
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart", "SecureConnectEnd", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "RequestBodyStart", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd", "RequestFailed");
}
Also used : MockResponse(mockwebserver3.MockResponse) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 MockResponse (mockwebserver3.MockResponse)11 BufferedSink (okio.BufferedSink)11 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)10 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)9 BufferedSource (okio.BufferedSource)8 IOException (java.io.IOException)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2 Response (okhttp3.Response)2 ProtocolException (java.net.ProtocolException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)1