use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method duplexWithRewriteInterceptors.
@Test
public void duplexWithRewriteInterceptors() throws Exception {
enableProtocol(Protocol.HTTP_2);
MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().receiveRequest("REQUEST A\n").sendResponse("response B\n").exhaustRequest().exhaustResponse());
client = client.newBuilder().addInterceptor(new UppercaseRequestInterceptor()).addInterceptor(new UppercaseResponseInterceptor()).build();
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();
requestBody.writeUtf8("request A\n");
requestBody.flush();
BufferedSource responseBody = response.body().source();
assertThat(responseBody.readUtf8Line()).isEqualTo("RESPONSE B");
requestBody.close();
assertThat(responseBody.readUtf8Line()).isNull();
}
mockDuplexResponseBody.awaitSuccess();
}
use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method http1DoesntSupportDuplex.
@Test
public void http1DoesntSupportDuplex() throws IOException {
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
try {
call.execute();
fail();
} catch (ProtocolException expected) {
}
}
use of okhttp3.internal.duplex.AsyncRequestBody 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.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method trueDuplexClientWritesFirst.
@Test
public void trueDuplexClientWritesFirst() throws Exception {
enableProtocol(Protocol.HTTP_2);
MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().receiveRequest("request A\n").sendResponse("response B\n").receiveRequest("request C\n").sendResponse("response D\n").receiveRequest("request E\n").sendResponse("response F\n").exhaustRequest().exhaustResponse());
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();
requestBody.writeUtf8("request A\n");
requestBody.flush();
BufferedSource responseBody = response.body().source();
assertThat(responseBody.readUtf8Line()).isEqualTo("response B");
requestBody.writeUtf8("request C\n");
requestBody.flush();
assertThat(responseBody.readUtf8Line()).isEqualTo("response D");
requestBody.writeUtf8("request E\n");
requestBody.flush();
assertThat(responseBody.readUtf8Line()).isEqualTo("response F");
requestBody.close();
assertThat(responseBody.readUtf8Line()).isNull();
}
mockDuplexResponseBody.awaitSuccess();
}
use of okhttp3.internal.duplex.AsyncRequestBody in project okhttp by square.
the class DuplexTest method duplexWithAuthChallenge.
/**
* Auth requires follow-ups. Unlike redirects, the auth follow-up also has a request body. This
* test makes a single call with two duplex requests!
*/
@Test
public void duplexWithAuthChallenge() throws Exception {
enableProtocol(Protocol.HTTP_2);
String credential = Credentials.basic("jesse", "secret");
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential, null)).build();
MockDuplexResponseBody mockResponseBody1 = enqueueResponseWithBody(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED), new MockDuplexResponseBody().sendResponse("please authenticate!\n").requestIOException().exhaustResponse());
MockDuplexResponseBody mockResponseBody2 = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("response body\n").exhaustResponse().receiveRequest("request body\n").exhaustRequest());
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
Response response2 = call.execute();
// First duplex request is detached with violence.
BufferedSink requestBody1 = ((AsyncRequestBody) call.request().body()).takeSink();
try {
requestBody1.writeUtf8("not authenticated\n");
requestBody1.flush();
fail();
} catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo("stream was reset: CANCEL");
}
mockResponseBody1.awaitSuccess();
// Second duplex request proceeds normally.
BufferedSink requestBody2 = ((AsyncRequestBody) call.request().body()).takeSink();
requestBody2.writeUtf8("request body\n");
requestBody2.close();
BufferedSource responseBody2 = response2.body().source();
assertThat(responseBody2.readUtf8Line()).isEqualTo("response body");
assertTrue(responseBody2.exhausted());
mockResponseBody2.awaitSuccess();
// No more requests attempted!
((AsyncRequestBody) call.request().body()).assertNoMoreSinks();
}
Aggregations