Search in sources :

Example 6 with MockDuplexResponseBody

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

the class DuplexTest method duplexWithRewriteInterceptors.

@Test
public void duplexWithRewriteInterceptors() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().receiveRequest("REQUEST A\n").sendResponse("response B\n").exhaustRequest().exhaustResponse());
    client = client.newBuilder().addInterceptor(new UppercaseRequestInterceptor()).addInterceptor(new UppercaseResponseInterceptor()).build();
    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();
        requestBody.writeUtf8("request A\n");
        requestBody.flush();
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("RESPONSE B");
        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 7 with MockDuplexResponseBody

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

the class DuplexTest method requestBodyEndsAfterResponseBody.

@Test
public void requestBodyEndsAfterResponseBody() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().exhaustResponse().receiveRequest("request A\n").exhaustRequest());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSource responseBody = response.body().source();
        assertTrue(responseBody.exhausted());
        BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
        requestBody.writeUtf8("request A\n");
        requestBody.close();
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart", "SecureConnectEnd", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "RequestBodyStart", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "RequestBodyEnd", "ConnectionReleased", "CallEnd");
}
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 8 with MockDuplexResponseBody

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

the class DuplexTest method trueDuplexClientWritesFirst.

@Test
public void trueDuplexClientWritesFirst() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().receiveRequest("request A\n").sendResponse("response B\n").receiveRequest("request C\n").sendResponse("response D\n").receiveRequest("request E\n").sendResponse("response F\n").exhaustRequest().exhaustResponse());
    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();
        requestBody.writeUtf8("request A\n");
        requestBody.flush();
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response B");
        requestBody.writeUtf8("request C\n");
        requestBody.flush();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response D");
        requestBody.writeUtf8("request E\n");
        requestBody.flush();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response F");
        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 9 with MockDuplexResponseBody

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

the class DuplexTest method duplexWithAuthChallenge.

/**
 * Auth requires follow-ups. Unlike redirects, the auth follow-up also has a request body. This
 * test makes a single call with two duplex requests!
 */
@Test
public void duplexWithAuthChallenge() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential, null)).build();
    MockDuplexResponseBody mockResponseBody1 = enqueueResponseWithBody(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED), new MockDuplexResponseBody().sendResponse("please authenticate!\n").requestIOException().exhaustResponse());
    MockDuplexResponseBody mockResponseBody2 = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("response body\n").exhaustResponse().receiveRequest("request body\n").exhaustRequest());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    Response response2 = call.execute();
    // First duplex request is detached with violence.
    BufferedSink requestBody1 = ((AsyncRequestBody) call.request().body()).takeSink();
    try {
        requestBody1.writeUtf8("not authenticated\n");
        requestBody1.flush();
        fail();
    } catch (IOException expected) {
        assertThat(expected.getMessage()).isEqualTo("stream was reset: CANCEL");
    }
    mockResponseBody1.awaitSuccess();
    // Second duplex request proceeds normally.
    BufferedSink requestBody2 = ((AsyncRequestBody) call.request().body()).takeSink();
    requestBody2.writeUtf8("request body\n");
    requestBody2.close();
    BufferedSource responseBody2 = response2.body().source();
    assertThat(responseBody2.readUtf8Line()).isEqualTo("response body");
    assertTrue(responseBody2.exhausted());
    mockResponseBody2.awaitSuccess();
    // No more requests attempted!
    ((AsyncRequestBody) call.request().body()).assertNoMoreSinks();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 10 with MockDuplexResponseBody

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

the class DuplexTest method serverReadsHeadersData.

@Test
public void serverReadsHeadersData() throws Exception {
    TestUtil.assumeNotWindows();
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().addHeader("h1", "v1").addHeader("h2", "v2"), new MockDuplexResponseBody().exhaustResponse().receiveRequest("hey\n").receiveRequest("whats going on\n").exhaustRequest());
    Request request = new Request.Builder().url(server.url("/")).method("POST", new AsyncRequestBody()).build();
    Call call = client.newCall(request);
    try (Response response = call.execute()) {
        BufferedSink sink = ((AsyncRequestBody) request.body()).takeSink();
        sink.writeUtf8("hey\n");
        sink.writeUtf8("whats going on\n");
        sink.close();
    }
    mockDuplexResponseBody.awaitSuccess();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) 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