Search in sources :

Example 1 with RequestBodyEnd

use of okhttp3.CallEvent.RequestBodyEnd in project okhttp by square.

the class EventListenerTest method assertBytesReadWritten.

private void assertBytesReadWritten(RecordingEventListener listener, @Nullable Matcher<Long> requestHeaderLength, @Nullable Matcher<Long> requestBodyBytes, @Nullable Matcher<Long> responseHeaderLength, @Nullable Matcher<Long> responseBodyBytes) {
    if (requestHeaderLength != null) {
        RequestHeadersEnd responseHeadersEnd = listener.removeUpToEvent(RequestHeadersEnd.class);
        MatcherAssert.assertThat("request header length", responseHeadersEnd.getHeaderLength(), requestHeaderLength);
    } else {
        assertThat(listener.recordedEventTypes()).doesNotContain("RequestHeadersEnd");
    }
    if (requestBodyBytes != null) {
        RequestBodyEnd responseBodyEnd = listener.removeUpToEvent(RequestBodyEnd.class);
        MatcherAssert.assertThat("request body bytes", responseBodyEnd.getBytesWritten(), requestBodyBytes);
    } else {
        assertThat(listener.recordedEventTypes()).doesNotContain("RequestBodyEnd");
    }
    if (responseHeaderLength != null) {
        ResponseHeadersEnd responseHeadersEnd = listener.removeUpToEvent(ResponseHeadersEnd.class);
        MatcherAssert.assertThat("response header length", responseHeadersEnd.getHeaderLength(), responseHeaderLength);
    } else {
        assertThat(listener.recordedEventTypes()).doesNotContain("ResponseHeadersEnd");
    }
    if (responseBodyBytes != null) {
        ResponseBodyEnd responseBodyEnd = listener.removeUpToEvent(ResponseBodyEnd.class);
        MatcherAssert.assertThat("response body bytes", responseBodyEnd.getBytesRead(), responseBodyBytes);
    } else {
        assertThat(listener.recordedEventTypes()).doesNotContain("ResponseBodyEnd");
    }
}
Also used : RequestBodyEnd(okhttp3.CallEvent.RequestBodyEnd) ResponseHeadersEnd(okhttp3.CallEvent.ResponseHeadersEnd) RequestHeadersEnd(okhttp3.CallEvent.RequestHeadersEnd) ResponseBodyEnd(okhttp3.CallEvent.ResponseBodyEnd)

Example 2 with RequestBodyEnd

use of okhttp3.CallEvent.RequestBodyEnd 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)

Aggregations

MockResponse (mockwebserver3.MockResponse)1 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)1 RequestBodyEnd (okhttp3.CallEvent.RequestBodyEnd)1 RequestHeadersEnd (okhttp3.CallEvent.RequestHeadersEnd)1 ResponseBodyEnd (okhttp3.CallEvent.ResponseBodyEnd)1 ResponseHeadersEnd (okhttp3.CallEvent.ResponseHeadersEnd)1 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)1 BufferedSink (okio.BufferedSink)1 BufferedSource (okio.BufferedSource)1 Test (org.junit.jupiter.api.Test)1