Search in sources :

Example 11 with AsyncRequestBody

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

Example 12 with AsyncRequestBody

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

the class DuplexTest method fullCallTimeoutDoesNotApplyOnceConnected.

@Test
public void fullCallTimeoutDoesNotApplyOnceConnected() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("response A\n").sleep(750, TimeUnit.MILLISECONDS).sendResponse("response B\n").receiveRequest("request C\n").exhaustResponse().exhaustRequest());
    Request request = new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build();
    Call call = client.newCall(request);
    // Long enough for the first TLS handshake.
    call.timeout().timeout(500, TimeUnit.MILLISECONDS);
    try (Response response = call.execute()) {
        BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("response A");
        assertThat(responseBody.readUtf8Line()).isEqualTo("response B");
        requestBody.writeUtf8("request C\n");
        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)

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