use of okhttp3.CallEvent.ResponseHeadersEnd in project okhttp by square.
the class EventListenerTest method failedDribbledCallEventSequence.
@Test
public void failedDribbledCallEventSequence() throws IOException {
server.enqueue(new MockResponse().setBody("0123456789").throttleBody(2, 100, TimeUnit.MILLISECONDS).setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY));
client = client.newBuilder().protocols(Collections.singletonList(Protocol.HTTP_1_1)).readTimeout(Duration.ofMillis(250)).build();
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
try {
response.body().string();
fail();
} catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo("unexpected end of stream");
}
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseFailed", "ConnectionReleased", "CallFailed");
ResponseFailed responseFailed = listener.removeUpToEvent(ResponseFailed.class);
assertThat(responseFailed.getIoe().getMessage()).isEqualTo("unexpected end of stream");
}
use of okhttp3.CallEvent.ResponseHeadersEnd in project okhttp by square.
the class EventListenerTest method successfulCallEventSequenceWithListener.
@Test
public void successfulCallEventSequenceWithListener() throws IOException {
server.enqueue(new MockResponse().setBody("abc"));
client = client.newBuilder().addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isEqualTo("abc");
response.body().close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
use of okhttp3.CallEvent.ResponseHeadersEnd 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");
}
}
use of okhttp3.CallEvent.ResponseHeadersEnd in project okhttp by square.
the class LoggingEventListenerTest method get.
@Test
public void get() throws Exception {
TestUtil.assumeNotWindows();
server.enqueue(new MockResponse().setBody("Hello!").setHeader("Content-Type", PLAIN));
Response response = client.newCall(request().build()).execute();
assertThat(response.body()).isNotNull();
response.body().bytes();
logRecorder.assertLogMatch("callStart: Request\\{method=GET, url=" + url + "\\}").assertLogMatch("proxySelectStart: " + url).assertLogMatch("proxySelectEnd: \\[DIRECT\\]").assertLogMatch("dnsStart: " + url.host()).assertLogMatch("dnsEnd: \\[.+\\]").assertLogMatch("connectStart: " + url.host() + "/.+ DIRECT").assertLogMatch("connectEnd: http/1.1").assertLogMatch("connectionAcquired: Connection\\{" + url.host() + ":\\d+, proxy=DIRECT hostAddress=" + url.host() + "/.+ cipherSuite=none protocol=http/1\\.1\\}").assertLogMatch("requestHeadersStart").assertLogMatch("requestHeadersEnd").assertLogMatch("responseHeadersStart").assertLogMatch("responseHeadersEnd: Response\\{protocol=http/1\\.1, code=200, message=OK, url=" + url + "\\}").assertLogMatch("responseBodyStart").assertLogMatch("responseBodyEnd: byteCount=6").assertLogMatch("connectionReleased").assertLogMatch("callEnd").assertNoMoreLogs();
}
use of okhttp3.CallEvent.ResponseHeadersEnd in project okhttp by square.
the class LoggingEventListenerTest method secureGet.
@Test
public void secureGet() throws Exception {
TestUtil.assumeNotWindows();
platform.assumeNotBouncyCastle();
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
url = server.url("/");
server.enqueue(new MockResponse());
Response response = client.newCall(request().build()).execute();
assertThat(response.body()).isNotNull();
response.body().bytes();
platform.assumeHttp2Support();
logRecorder.assertLogMatch("callStart: Request\\{method=GET, url=" + url + "\\}").assertLogMatch("proxySelectStart: " + url).assertLogMatch("proxySelectEnd: \\[DIRECT\\]").assertLogMatch("dnsStart: " + url.host()).assertLogMatch("dnsEnd: \\[.+\\]").assertLogMatch("connectStart: " + url.host() + "/.+ DIRECT").assertLogMatch("secureConnectStart").assertLogMatch("secureConnectEnd: Handshake\\{" + "tlsVersion=TLS_1_[23] " + "cipherSuite=TLS_.* " + "peerCertificates=\\[CN=localhost\\] " + "localCertificates=\\[\\]}").assertLogMatch("connectEnd: h2").assertLogMatch("connectionAcquired: Connection\\{" + url.host() + ":\\d+, proxy=DIRECT hostAddress=" + url.host() + "/.+ cipherSuite=.+ protocol=h2\\}").assertLogMatch("requestHeadersStart").assertLogMatch("requestHeadersEnd").assertLogMatch("responseHeadersStart").assertLogMatch("responseHeadersEnd: Response\\{protocol=h2, code=200, message=, url=" + url + "\\}").assertLogMatch("responseBodyStart").assertLogMatch("responseBodyEnd: byteCount=0").assertLogMatch("connectionReleased").assertLogMatch("callEnd").assertNoMoreLogs();
}
Aggregations