use of okhttp3.RecordingEventListener 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.RecordingEventListener in project okhttp by square.
the class WebSocketHttpTest method webSocketsDontTriggerEventListener.
@Test
public void webSocketsDontTriggerEventListener() {
RecordingEventListener listener = new RecordingEventListener();
client = client.newBuilder().eventListenerFactory(clientTestRule.wrap(listener)).build();
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
webSocket.send("Web Sockets and Events?!");
serverListener.assertTextMessage("Web Sockets and Events?!");
webSocket.close(1000, "");
serverListener.assertClosing(1000, "");
server.close(1000, "");
clientListener.assertClosing(1000, "");
clientListener.assertClosed(1000, "");
serverListener.assertClosed(1000, "");
assertThat(listener.recordedEventTypes()).isEmpty();
}
use of okhttp3.RecordingEventListener in project okhttp by square.
the class DuplexTest method duplexWithRedirect.
/**
* Duplex calls that have follow-ups are weird. By the time we know there's a follow-up we've
* already split off another thread to stream the request body. Because we permit at most one
* exchange at a time we break the request stream out from under that writer.
*/
@Test
public void duplexWithRedirect() throws Exception {
enableProtocol(Protocol.HTTP_2);
CountDownLatch duplexResponseSent = new CountDownLatch(1);
listener = new RecordingEventListener() {
@Override
public void responseHeadersEnd(Call call, Response response) {
try {
// Wait for the server to send the duplex response before acting on the 301 response
// and resetting the stream.
duplexResponseSent.await();
} catch (InterruptedException e) {
throw new AssertionError();
}
super.responseHeadersEnd(call, response);
}
};
client = client.newBuilder().eventListener(listener).build();
MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /b"), new MockDuplexResponseBody().sendResponse("/a has moved!\n", duplexResponseSent).requestIOException().exhaustResponse());
server.enqueue(new MockResponse().setBody("this is /b"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
try (Response response = call.execute()) {
BufferedSource responseBody = response.body().source();
assertThat(responseBody.readUtf8Line()).isEqualTo("this is /b");
}
BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
try {
requestBody.writeUtf8("request body\n");
requestBody.flush();
fail();
} catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo("stream was reset: CANCEL");
}
mockDuplexResponseBody.awaitSuccess();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart", "SecureConnectEnd", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "RequestBodyStart", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd", "RequestFailed");
}
use of okhttp3.RecordingEventListener in project okhttp by square.
the class ClientAuthTest method invalidClientAuthEvents.
@Test
public void invalidClientAuthEvents() throws Throwable {
server.enqueue(new MockResponse().setBody("abc"));
clientCert = new HeldCertificate.Builder().signedBy(clientIntermediateCa).serialNumber(4L).commonName("Jethro Willis").addSubjectAlternativeName("jethrowillis.com").validityInterval(1, 2).build();
OkHttpClient client = buildClient(clientCert, clientIntermediateCa.certificate());
RecordingEventListener listener = new RecordingEventListener();
client = client.newBuilder().eventListener(listener).build();
SSLSocketFactory socketFactory = buildServerSslSocketFactory();
server.useHttps(socketFactory, false);
server.requireClientAuth();
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
try {
call.execute();
fail();
} catch (IOException expected) {
}
// Observed Events are variable
// JDK 14
// CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
// SecureConnectEnd, ConnectEnd, ConnectionAcquired, RequestHeadersStart, RequestHeadersEnd,
// ResponseFailed, ConnectionReleased, CallFailed
// JDK 8
// CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
// ConnectFailed, CallFailed
// Gradle - JDK 11
// CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
// SecureConnectEnd, ConnectFailed, CallFailed
List<String> recordedEventTypes = listener.recordedEventTypes();
assertThat(recordedEventTypes).startsWith("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart");
assertThat(recordedEventTypes).endsWith("CallFailed");
}
Aggregations