use of okhttp3.CallEvent.RequestHeadersEnd 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.CallEvent.RequestHeadersEnd 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");
}
use of okhttp3.CallEvent.RequestHeadersEnd 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