use of okhttp3.Connection in project okhttp by square.
the class Http2ConnectionTest method maxFrameSizeHonored.
@Test
public void maxFrameSizeHonored() throws Exception {
byte[] buff = new byte[peer.maxOutboundDataLength() + 1];
Arrays.fill(buff, (byte) '*');
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
// DATA
peer.acceptFrame();
// DATA
peer.acceptFrame();
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
BufferedSink out = Okio.buffer(stream.getSink());
out.write(buff);
out.flush();
out.close();
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
InFrame data = peer.takeFrame();
assertEquals(peer.maxOutboundDataLength(), data.data.length);
data = peer.takeFrame();
assertEquals(1, data.data.length);
}
use of okhttp3.Connection in project okhttp by square.
the class HttpOverHttp2Test method cancelWithStreamNotCompleted.
/** https://github.com/square/okhttp/issues/1191 */
// TODO: recover gracefully when a connection is shutdown.
@Ignore
@Test
public void cancelWithStreamNotCompleted() throws Exception {
// Ensure that the (shared) connection pool is in a consistent state.
client.connectionPool().evictAll();
assertEquals(0, client.connectionPool().connectionCount());
server.enqueue(new MockResponse().setBody("abc"));
server.enqueue(new MockResponse().setBody("def"));
// Disconnect before the stream is created. A connection is still established!
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call1.execute();
call1.cancel();
// That connection is pooled, and it works.
assertEquals(1, client.connectionPool().connectionCount());
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
assertEquals("def", response2.body().string());
assertEquals(0, server.takeRequest().getSequenceNumber());
// Clean up the connection.
response.close();
}
use of okhttp3.Connection 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 okhttp3.Connection in project okhttp by square.
the class HttpOverHttp2Test method recoverFromOneRefusedStreamReusesConnection.
@Test
public void recoverFromOneRefusedStreamReusesConnection() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.REFUSED_STREAM.httpCode));
server.enqueue(new MockResponse().setBody("abc"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("abc", response.body().string());
// New connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Reused connection.
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class HttpOverHttp2Test method settingsLimitsMaxConcurrentStreams.
/**
* Push a setting that permits up to 2 concurrent streams, then make 3 concurrent requests and
* confirm that the third concurrent request prepared a new connection.
*/
@Test
public void settingsLimitsMaxConcurrentStreams() throws Exception {
Settings settings = new Settings();
settings.set(Settings.MAX_CONCURRENT_STREAMS, 2);
// Read & write a full request to confirm settings are accepted.
server.enqueue(new MockResponse().withSettings(settings));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().setBody("GHI"));
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
Call call3 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response3 = call3.execute();
assertEquals("ABC", response1.body().string());
assertEquals("DEF", response2.body().string());
assertEquals("GHI", response3.body().string());
// Settings connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Reuse settings connection.
assertEquals(1, server.takeRequest().getSequenceNumber());
// Reuse settings connection.
assertEquals(2, server.takeRequest().getSequenceNumber());
// New connection!
assertEquals(0, server.takeRequest().getSequenceNumber());
}
Aggregations