use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method concurrentHttp2ConnectionsDeduplicated.
/**
* We don't know if the connection will support HTTP/2 until after we've connected. When multiple
* connections are requested concurrently OkHttp will pessimistically connect multiple times, then
* close any unnecessary connections. This test confirms that behavior works as intended.
*
* <p>This test uses proxy tunnels to get a hook while a connection is being established.
*/
@Test
public void concurrentHttp2ConnectionsDeduplicated() throws Exception {
server.useHttps(sslClient.socketFactory, true);
// Force a fresh connection pool for the test.
client.connectionPool().evictAll();
final QueueDispatcher queueDispatcher = new QueueDispatcher();
queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
queueDispatcher.enqueueResponse(new MockResponse().setBody("call2 response"));
queueDispatcher.enqueueResponse(new MockResponse().setBody("call1 response"));
// We use a re-entrant dispatcher to initiate one HTTPS connection while the other is in flight.
server.setDispatcher(new Dispatcher() {
int requestCount;
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse result = queueDispatcher.dispatch(request);
requestCount++;
if (requestCount == 1) {
// Before handling call1's CONNECT we do all of call2. This part re-entrant!
try {
Call call2 = client.newCall(new Request.Builder().url("https://android.com/call2").build());
Response response2 = call2.execute();
assertEquals("call2 response", response2.body().string());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return result;
}
@Override
public MockResponse peek() {
return queueDispatcher.peek();
}
@Override
public void shutdown() {
queueDispatcher.shutdown();
}
});
client = client.newBuilder().proxy(server.toProxyAddress()).build();
Call call1 = client.newCall(new Request.Builder().url("https://android.com/call1").build());
Response response2 = call1.execute();
assertEquals("call1 response", response2.body().string());
RecordedRequest call1Connect = server.takeRequest();
assertEquals("CONNECT", call1Connect.getMethod());
assertEquals(0, call1Connect.getSequenceNumber());
RecordedRequest call2Connect = server.takeRequest();
assertEquals("CONNECT", call2Connect.getMethod());
assertEquals(0, call2Connect.getSequenceNumber());
RecordedRequest call2Get = server.takeRequest();
assertEquals("GET", call2Get.getMethod());
assertEquals("/call2", call2Get.getPath());
assertEquals(0, call2Get.getSequenceNumber());
RecordedRequest call1Get = server.takeRequest();
assertEquals("GET", call1Get.getMethod());
assertEquals("/call1", call1Get.getPath());
assertEquals(1, call1Get.getSequenceNumber());
assertEquals(1, client.connectionPool().connectionCount());
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method get.
@Test
public void get() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method serverSendsPushPromise_GET.
@Test
public void serverSendsPushPromise_GET() throws Exception {
PushPromise pushPromise = new PushPromise("GET", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setBody("bar").setStatus("HTTP/1.1 200 Sweet"));
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
RecordedRequest pushedRequest = server.takeRequest();
assertEquals("GET /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
assertEquals("bar", pushedRequest.getHeader("foo"));
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method readResponseHeaderTimeout.
@Test
public void readResponseHeaderTimeout() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
server.enqueue(new MockResponse().setBody("A"));
client = client.newBuilder().readTimeout(1000, MILLISECONDS).build();
// Make a call expecting a timeout reading the response headers.
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
try {
call1.execute();
fail("Should have timed out!");
} catch (SocketTimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
// Confirm that a subsequent request on the same connection is not impacted.
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
assertEquals("A", response2.body().string());
// Confirm that the connection was reused.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method serverSendsPushPromise_HEAD.
@Test
public void serverSendsPushPromise_HEAD() throws Exception {
PushPromise pushPromise = new PushPromise("HEAD", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setStatus("HTTP/1.1 204 Sweet"));
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
RecordedRequest pushedRequest = server.takeRequest();
assertEquals("HEAD /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
assertEquals("bar", pushedRequest.getHeader("foo"));
}
Aggregations