Search in sources :

Example 1 with MockDuplexResponseBody

use of mockwebserver3.internal.duplex.MockDuplexResponseBody in project okhttp by square.

the class DuplexTest method serverCancelsRequestBodyAndSendsResponseBody.

/**
 * OkHttp currently doesn't implement failing the request body stream independently of failing the
 * corresponding response body stream. This is necessary if we want servers to be able to stop
 * inbound data and send an early 400 before the request body completes.
 *
 * This test sends a slow request that is canceled by the server. It expects the response to still
 * be readable after the request stream is canceled.
 */
@Disabled
@Test
public void serverCancelsRequestBodyAndSendsResponseBody() throws Exception {
    client = client.newBuilder().retryOnConnectionFailure(false).build();
    BlockingQueue<String> log = new LinkedBlockingQueue<>();
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("success!").exhaustResponse().cancelStream(ErrorCode.NO_ERROR));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new RequestBody() {

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

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            try {
                for (int i = 0; i < 10; i++) {
                    sink.writeUtf8(".");
                    sink.flush();
                    Thread.sleep(100);
                }
            } catch (IOException e) {
                log.add(e.toString());
                throw e;
            } catch (Exception e) {
                log.add(e.toString());
            }
        }
    }).build());
    try (Response response = call.execute()) {
        assertThat(response.body().string()).isEqualTo("success!");
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(log.take()).contains("StreamResetException: stream was reset: NO_ERROR");
}
Also used : MockResponse(mockwebserver3.MockResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) Nullable(org.jetbrains.annotations.Nullable) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with MockDuplexResponseBody

use of mockwebserver3.internal.duplex.MockDuplexResponseBody in project okhttp by square.

the class DuplexTest method clientReadsHeadersDataTrailers.

@Test
public void clientReadsHeadersDataTrailers() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().addHeader("h1", "v1").addHeader("h2", "v2").setTrailers(Headers.of("trailers", "boom")), new MockDuplexResponseBody().sendResponse("ok").exhaustResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    try (Response response = call.execute()) {
        assertThat(response.headers()).isEqualTo(Headers.of("h1", "v1", "h2", "v2"));
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8(2)).isEqualTo("ok");
        assertTrue(responseBody.exhausted());
        assertThat(response.trailers()).isEqualTo(Headers.of("trailers", "boom"));
    }
    mockDuplexResponseBody.awaitSuccess();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 3 with MockDuplexResponseBody

use of mockwebserver3.internal.duplex.MockDuplexResponseBody 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 MockDuplexResponseBody

use of mockwebserver3.internal.duplex.MockDuplexResponseBody 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 MockDuplexResponseBody

use of mockwebserver3.internal.duplex.MockDuplexResponseBody 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

MockResponse (mockwebserver3.MockResponse)11 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)11 Test (org.junit.jupiter.api.Test)11 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)10 BufferedSink (okio.BufferedSink)10 BufferedSource (okio.BufferedSource)9 IOException (java.io.IOException)3 ProtocolException (java.net.ProtocolException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)1 Nullable (org.jetbrains.annotations.Nullable)1 Disabled (org.junit.jupiter.api.Disabled)1