Search in sources :

Example 6 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody 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 AsyncRequestBody

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

the class DuplexTest method http1DoesntSupportDuplex.

@Test
public void http1DoesntSupportDuplex() throws IOException {
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try {
        call.execute();
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : ProtocolException(java.net.ProtocolException) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) Test(org.junit.jupiter.api.Test)

Example 8 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody 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 9 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody 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 10 with AsyncRequestBody

use of okhttp3.internal.duplex.AsyncRequestBody 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)

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