use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class HttpLoggingInterceptorTest method duplexRequestsAreNotLogged.
@Test
public void duplexRequestsAreNotLogged() throws Exception {
platform.assumeHttp2Support();
platform.assumeNotBouncyCastle();
// HTTP/2
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
url = server.url("/");
setLevel(Level.BODY);
server.enqueue(new MockResponse().setBody("Hello response!"));
RequestBody asyncRequestBody = new RequestBody() {
@Override
@Nullable
public MediaType contentType() {
return null;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("Hello request!");
sink.close();
}
@Override
public boolean isDuplex() {
return true;
}
};
Request request = request().post(asyncRequestBody).build();
Response response = client.newCall(request).execute();
assumeTrue(response.protocol().equals(Protocol.HTTP_2));
assertThat(response.body().string()).isEqualTo("Hello response!");
applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("--> END POST (duplex request body omitted)").assertLogMatch("<-- 200 " + url + " \\(\\d+ms\\)").assertLogEqual("content-length: 15").assertLogEqual("").assertLogEqual("Hello response!").assertLogEqual("<-- END HTTP (15-byte body)").assertNoMoreLogs();
}
use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class HttpLoggingInterceptorTest method oneShotRequestsAreNotLogged.
@Test
public void oneShotRequestsAreNotLogged() throws Exception {
url = server.url("/");
setLevel(Level.BODY);
server.enqueue(new MockResponse().setBody("Hello response!"));
RequestBody asyncRequestBody = new RequestBody() {
@Override
@Nullable
public MediaType contentType() {
return null;
}
int counter = 0;
@Override
public void writeTo(BufferedSink sink) throws IOException {
counter++;
assertThat(counter).isLessThanOrEqualTo(1);
sink.writeUtf8("Hello request!");
sink.close();
}
@Override
public boolean isOneShot() {
return true;
}
};
Request request = request().post(asyncRequestBody).build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("Hello response!");
applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("--> END POST (one-shot body omitted)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 15").assertLogEqual("").assertLogEqual("Hello response!").assertLogEqual("<-- END HTTP (15-byte body)").assertNoMoreLogs();
}
use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method trueDuplexServerWritesFirst.
@Test
public void trueDuplexServerWritesFirst() throws Exception {
enableProtocol(Protocol.HTTP_2);
MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("response A\n").receiveRequest("request B\n").sendResponse("response C\n").receiveRequest("request D\n").sendResponse("response E\n").receiveRequest("request F\n").exhaustResponse().exhaustRequest());
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
try (Response response = call.execute()) {
BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
BufferedSource responseBody = response.body().source();
assertThat(responseBody.readUtf8Line()).isEqualTo("response A");
requestBody.writeUtf8("request B\n");
requestBody.flush();
assertThat(responseBody.readUtf8Line()).isEqualTo("response C");
requestBody.writeUtf8("request D\n");
requestBody.flush();
assertThat(responseBody.readUtf8Line()).isEqualTo("response E");
requestBody.writeUtf8("request F\n");
requestBody.flush();
assertThat(responseBody.readUtf8Line()).isNull();
requestBody.close();
}
mockDuplexResponseBody.awaitSuccess();
}
use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method duplexWith100Continue.
@Test
public void duplexWith100Continue() throws Exception {
enableProtocol(Protocol.HTTP_2);
MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().setSocketPolicy(SocketPolicy.EXPECT_CONTINUE), new MockDuplexResponseBody().receiveRequest("request body\n").sendResponse("response body\n").exhaustRequest());
Call call = client.newCall(new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(new AsyncRequestBody()).build());
try (Response response = call.execute()) {
BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
requestBody.writeUtf8("request body\n");
requestBody.flush();
BufferedSource responseBody = response.body().source();
assertThat(responseBody.readUtf8Line()).isEqualTo("response body");
requestBody.close();
assertThat(responseBody.readUtf8Line()).isNull();
}
mockDuplexResponseBody.awaitSuccess();
}
use of okhttp3.internal.duplex.AsyncRequestBody 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");
}
Aggregations