Search in sources :

Example 81 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method connectViaHttpsWithSSLFallback.

// TODO(jwilson): tests below this marker need to be migrated to OkHttp's request/response API.
@Test
public void connectViaHttpsWithSSLFallback() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("this response comes via SSL"));
    urlFactory.setClient(urlFactory.client().newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build());
    connection = urlFactory.open(server.url("/foo").url());
    assertContent("this response comes via SSL", connection);
    RecordedRequest failHandshakeRequest = server.takeRequest();
    assertNull(failHandshakeRequest.getRequestLine());
    RecordedRequest fallbackRequest = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", fallbackRequest.getRequestLine());
    assertEquals(TlsVersion.TLS_1_0, fallbackRequest.getTlsVersion());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 82 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method singleByteReadIsSigned.

@Test
public void singleByteReadIsSigned() throws IOException {
    server.enqueue(new MockResponse().setBody(new Buffer().writeByte(-2).writeByte(-1)));
    connection = urlFactory.open(server.url("/").url());
    InputStream in = connection.getInputStream();
    assertEquals(254, in.read());
    assertEquals(255, in.read());
    assertEquals(-1, in.read());
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 83 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method bodyPermittedOnDelete.

/**
   * The RFC is unclear in this regard as it only specifies that this should invalidate the cache
   * entry (if any).
   */
@Test
public void bodyPermittedOnDelete() throws Exception {
    server.enqueue(new MockResponse());
    HttpURLConnection connection = urlFactory.open(server.url("/").url());
    connection.setRequestMethod("DELETE");
    connection.setDoOutput(true);
    connection.getOutputStream().write("BODY".getBytes(UTF_8));
    assertEquals(200, connection.getResponseCode());
    RecordedRequest request = server.takeRequest();
    assertEquals("DELETE", request.getMethod());
    assertEquals("BODY", request.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) Test(org.junit.Test)

Example 84 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method getContentEncodingConnects.

@Test
public void getContentEncodingConnects() throws Exception {
    server.enqueue(new MockResponse().addHeader("Content-Encoding: identity").setBody("ABC"));
    connection = urlFactory.open(server.url("/").url());
    assertEquals("identity", connection.getContentEncoding());
    connection.getInputStream().close();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 85 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method writeTimeouts.

/** Confirm that an unacknowledged write times out. */
@Test
public void writeTimeouts() throws IOException {
    MockWebServer server = new MockWebServer();
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    final int SOCKET_BUFFER_SIZE = 4 * 1024;
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).writeTimeout(500, TimeUnit.MILLISECONDS).build());
    server.start();
    server.enqueue(new MockResponse().throttleBody(1, 1, // Prevent the server from reading!
    TimeUnit.SECONDS));
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    try {
        // 2 MiB.
        byte[] data = new byte[2 * 1024 * 1024];
        out.write(data);
        fail();
    } catch (SocketTimeoutException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) OutputStream(java.io.OutputStream) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)79 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)43 Connection (com.trilead.ssh2.Connection)40 InputStream (java.io.InputStream)40 Request (okhttp3.Request)38 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)26 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 ResponseBody (okhttp3.ResponseBody)14