use of okhttp3.Connection in project okhttp by square.
the class HttpOverHttp2Test method recoverFromOneInternalErrorRequiresNewConnection.
@Test
public void recoverFromOneInternalErrorRequiresNewConnection() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.INTERNAL_ERROR.httpCode));
server.enqueue(new MockResponse().setBody("abc"));
client = client.newBuilder().dns(new DoubleInetAddressDns()).build();
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());
// New connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class CertificatePinnerChainValidationTest method pinIntermediatePresentInChain.
/** The pinner should accept an intermediate from the server's chain. */
@Test
public void pinIntermediatePresentInChain() throws Exception {
HeldCertificate rootCa = new HeldCertificate.Builder().serialNumber("1").ca(3).commonName("root").build();
HeldCertificate intermediateCa = new HeldCertificate.Builder().issuedBy(rootCa).ca(2).serialNumber("2").commonName("intermediate_ca").build();
HeldCertificate certificate = new HeldCertificate.Builder().issuedBy(intermediateCa).serialNumber("3").commonName(server.getHostName()).build();
CertificatePinner certificatePinner = new CertificatePinner.Builder().add(server.getHostName(), CertificatePinner.pin(intermediateCa.certificate)).build();
SslClient contextBuilder = new SslClient.Builder().addTrustedCertificate(rootCa.certificate).build();
OkHttpClient client = defaultClient().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).certificatePinner(certificatePinner).build();
SslClient serverSslContext = new SslClient.Builder().certificateChain(certificate.keyPair, certificate.certificate, intermediateCa.certificate).build();
server.useHttps(serverSslContext.socketFactory, false);
// The request should complete successfully.
server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
assertEquals("abc", response1.body().string());
response1.close();
// Force a fresh connection for the next request.
client.connectionPool().evictAll();
// Confirm that a second request also succeeds. This should detect caching problems.
server.enqueue(new MockResponse().setBody("def").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
assertEquals("def", response2.body().string());
response2.close();
}
use of okhttp3.Connection in project okhttp by square.
the class Http2ConnectionTest method readSendsWindowUpdate.
@Test
public void readSendsWindowUpdate() throws Exception {
int windowSize = 100;
int windowUpdateThreshold = 50;
// Write the mocking script.
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
for (int i = 0; i < 3; i++) {
// Send frames of summing to size 50, which is windowUpdateThreshold.
peer.sendFrame().data(false, 3, data(24), 24);
peer.sendFrame().data(false, 3, data(25), 25);
peer.sendFrame().data(false, 3, data(1), 1);
// connection WINDOW UPDATE
peer.acceptFrame();
// stream WINDOW UPDATE
peer.acceptFrame();
}
peer.sendFrame().data(true, 3, data(0), 0);
peer.play();
// Play it back.
Http2Connection connection = connect(peer);
connection.okHttpSettings.set(INITIAL_WINDOW_SIZE, windowSize);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
assertEquals(0, stream.unacknowledgedBytesRead);
assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
Source in = stream.getSource();
Buffer buffer = new Buffer();
buffer.writeAll(in);
assertEquals(-1, in.read(buffer, 1));
assertEquals(150, buffer.size());
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
for (int i = 0; i < 3; i++) {
List<Integer> windowUpdateStreamIds = new ArrayList<>(2);
for (int j = 0; j < 2; j++) {
InFrame windowUpdate = peer.takeFrame();
assertEquals(Http2.TYPE_WINDOW_UPDATE, windowUpdate.type);
windowUpdateStreamIds.add(windowUpdate.streamId);
assertEquals(windowUpdateThreshold, windowUpdate.windowSizeIncrement);
}
// connection
assertTrue(windowUpdateStreamIds.contains(0));
// stream
assertTrue(windowUpdateStreamIds.contains(3));
}
}
use of okhttp3.Connection in project okhttp by square.
the class Http2ConnectionTest method receiveGoAwayHttp2.
@Test
public void receiveGoAwayHttp2() throws Exception {
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM 3
peer.acceptFrame();
// SYN_STREAM 5
peer.acceptFrame();
peer.sendFrame().goAway(3, ErrorCode.PROTOCOL_ERROR, EMPTY_BYTE_ARRAY);
// PING
peer.acceptFrame();
peer.sendFrame().ping(true, 1, 0);
// DATA STREAM 3
peer.acceptFrame();
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream1 = connection.newStream(headerEntries("a", "android"), true);
Http2Stream stream2 = connection.newStream(headerEntries("b", "banana"), true);
// Ensure the GO_AWAY that resets stream2 has been received.
connection.ping().roundTripTime();
BufferedSink sink1 = Okio.buffer(stream1.getSink());
BufferedSink sink2 = Okio.buffer(stream2.getSink());
sink1.writeUtf8("abc");
try {
sink2.writeUtf8("abc");
sink2.flush();
fail();
} catch (IOException expected) {
assertEquals("stream was reset: REFUSED_STREAM", expected.getMessage());
}
sink1.writeUtf8("def");
sink1.close();
try {
connection.newStream(headerEntries("c", "cola"), true);
fail();
} catch (ConnectionShutdownException expected) {
}
assertTrue(stream1.isOpen());
assertFalse(stream2.isOpen());
assertEquals(1, connection.openStreamCount());
// verify the peer received what was expected
InFrame synStream1 = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream1.type);
InFrame synStream2 = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream2.type);
InFrame ping = peer.takeFrame();
assertEquals(Http2.TYPE_PING, ping.type);
InFrame data1 = peer.takeFrame();
assertEquals(Http2.TYPE_DATA, data1.type);
assertEquals(3, data1.streamId);
assertTrue(Arrays.equals("abcdef".getBytes("UTF-8"), data1.data));
}
use of okhttp3.Connection in project okhttp by square.
the class Http2ConnectionTest method serverSendsEmptyDataClientDoesntSendWindowUpdateHttp2.
@Test
public void serverSendsEmptyDataClientDoesntSendWindowUpdateHttp2() throws Exception {
// Write the mocking script.
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
peer.sendFrame().data(true, 3, data(0), 0);
peer.play();
// Play it back.
Http2Connection connection = connect(peer);
Http2Stream client = connection.newStream(headerEntries("b", "banana"), false);
assertEquals(-1, client.getSource().read(new Buffer(), 1));
// Verify the peer received what was expected.
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
assertEquals(5, peer.frameCount());
}
Aggregations