use of okhttp3.OkHttpClient in project okhttp by square.
the class ConnectionReuseTest method connectionsAreNotReusedIfSslSocketFactoryChanges.
@Test
public void connectionsAreNotReusedIfSslSocketFactoryChanges() throws Exception {
enableHttps();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
response.body().close();
// This client shares a connection pool but has a different SSL socket factory.
SslClient sslClient2 = new SslClient.Builder().build();
OkHttpClient anotherClient = client.newBuilder().sslSocketFactory(sslClient2.socketFactory, sslClient2.trustManager).build();
// This client fails to connect because the new SSL socket factory refuses.
try {
anotherClient.newCall(request).execute();
fail();
} catch (SSLException expected) {
}
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class ConnectionReuseTest method connectionsAreNotReusedIfHostnameVerifierChanges.
@Test
public void connectionsAreNotReusedIfHostnameVerifierChanges() throws Exception {
enableHttps();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
Request request = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request).execute();
response1.body().close();
// This client shares a connection pool but has a different SSL socket factory.
OkHttpClient anotherClient = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).build();
Response response2 = anotherClient.newCall(request).execute();
response2.body().close();
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class RealWebSocket method connect.
public void connect(OkHttpClient client) {
client = client.newBuilder().protocols(ONLY_HTTP1).build();
final int pingIntervalMillis = client.pingIntervalMillis();
final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
call = Internal.instance.newWebSocketCall(client, request);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
try {
checkResponse(response);
} catch (ProtocolException e) {
failWebSocket(e, response);
closeQuietly(response);
return;
}
// Promote the HTTP streams into web socket streams.
StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
// Prevent connection pooling!
streamAllocation.noNewStreams();
Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
// Process all web socket messages.
try {
listener.onOpen(RealWebSocket.this, response);
String name = "OkHttp WebSocket " + request.url().redact();
initReaderAndWriter(name, pingIntervalMillis, streams);
streamAllocation.connection().socket().setSoTimeout(0);
loopReader();
} catch (Exception e) {
failWebSocket(e, null);
}
}
@Override
public void onFailure(Call call, IOException e) {
failWebSocket(e, null);
}
});
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class CacheAdapterTest method setUp.
@Before
public void setUp() throws Exception {
server = new MockWebServer();
client = new OkHttpClient();
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class HttpOverHttp2Test method connectionTimeout.
@Test
public void connectionTimeout() throws Exception {
server.enqueue(new MockResponse().setBody("A").setBodyDelay(1, SECONDS));
OkHttpClient client1 = client.newBuilder().readTimeout(2000, MILLISECONDS).build();
Call call1 = client1.newCall(new Request.Builder().url(server.url("/")).build());
OkHttpClient client2 = client.newBuilder().readTimeout(200, MILLISECONDS).build();
Call call2 = client2.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
assertEquals("A", response1.body().string());
try {
call2.execute();
fail();
} catch (IOException expected) {
}
// Confirm that the connection was reused.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
Aggregations