Search in sources :

Example 26 with Connection

use of okhttp3.Connection 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) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Example 27 with Connection

use of okhttp3.Connection 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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 28 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ConnectionReuseTest method connectionsAreNotReusedWithResponseConnectionClose.

@Test
public void connectionsAreNotReusedWithResponseConnectionClose() throws Exception {
    server.enqueue(new MockResponse().addHeader("Connection", "close").setBody("a"));
    server.enqueue(new MockResponse().setBody("b"));
    Request requestA = new Request.Builder().url(server.url("/")).build();
    Request requestB = new Request.Builder().url(server.url("/")).build();
    assertConnectionNotReused(requestA, requestB);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 29 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HeadersTest method readNameValueBlockDropsForbiddenHeadersHttp2.

@Test
public void readNameValueBlockDropsForbiddenHeadersHttp2() throws IOException {
    List<Header> headerBlock = headerEntries(":status", "200 OK", ":version", "HTTP/1.1", "connection", "close");
    Request request = new Request.Builder().url("http://square.com/").build();
    Response response = Http2Codec.readHttp2HeadersList(headerBlock).request(request).build();
    Headers headers = response.headers();
    assertEquals(1, headers.size());
    assertEquals(":version", headers.name(0));
    assertEquals("HTTP/1.1", headers.value(0));
}
Also used : Header(okhttp3.internal.http2.Header) HttpHeaders(okhttp3.internal.http.HttpHeaders) Test(org.junit.Test)

Example 30 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HeadersTest method headersNotEquals.

@Test
public void headersNotEquals() {
    Headers headers1 = new Headers.Builder().add("Connection", "close").add("Transfer-Encoding", "chunked").build();
    Headers headers2 = new Headers.Builder().add("Connection", "keep-alive").add("Transfer-Encoding", "chunked").build();
    assertFalse(headers1.equals(headers2));
    assertFalse(headers1.hashCode() == headers2.hashCode());
}
Also used : HttpHeaders(okhttp3.internal.http.HttpHeaders) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)73 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)41 InputStream (java.io.InputStream)39 Connection (com.trilead.ssh2.Connection)36 Request (okhttp3.Request)36 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)25 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 BufferedSink (okio.BufferedSink)14