Search in sources :

Example 26 with MockResponse

use of com.google.mockwebserver.MockResponse in project robovm by robovm.

the class URLConnectionTest method testInspectSslAfterConnect.

/**
     * Test that we can inspect the SSL session after connect().
     * http://code.google.com/p/android/issues/detail?id=24431
     */
public void testInspectSslAfterConnect() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse());
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.connect();
    assertNotNull(connection.getHostnameVerifier());
    assertNull(connection.getLocalCertificates());
    assertNotNull(connection.getServerCertificates());
    assertNotNull(connection.getCipherSuite());
    assertNotNull(connection.getPeerPrincipal());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 27 with MockResponse

use of com.google.mockwebserver.MockResponse in project robovm by robovm.

the class URLConnectionTest method doUpload.

private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception {
    int n = 512 * 1024;
    server.setBodyLimit(0);
    server.enqueue(new MockResponse());
    server.play();
    HttpURLConnection conn = (HttpURLConnection) server.getUrl("/").openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    if (uploadKind == TransferKind.CHUNKED) {
        conn.setChunkedStreamingMode(-1);
    } else {
        conn.setFixedLengthStreamingMode(n);
    }
    OutputStream out = conn.getOutputStream();
    if (writeKind == WriteKind.BYTE_BY_BYTE) {
        for (int i = 0; i < n; ++i) {
            out.write('x');
        }
    } else {
        byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64 * 1024];
        Arrays.fill(buf, (byte) 'x');
        for (int i = 0; i < n; i += buf.length) {
            out.write(buf, 0, Math.min(buf.length, n - i));
        }
    }
    out.close();
    assertEquals(200, conn.getResponseCode());
    RecordedRequest request = server.takeRequest();
    assertEquals(n, request.getBodySize());
    if (uploadKind == TransferKind.CHUNKED) {
        assertTrue(request.getChunkSizes().size() > 0);
    } else {
        assertTrue(request.getChunkSizes().isEmpty());
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream)

Example 28 with MockResponse

use of com.google.mockwebserver.MockResponse in project robovm by robovm.

the class URLConnectionTest method testSetChunkedEncodingAsRequestProperty.

public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    server.play();
    HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
    urlConnection.setRequestProperty("Transfer-encoding", "chunked");
    urlConnection.setDoOutput(true);
    urlConnection.getOutputStream().write("ABC".getBytes("UTF-8"));
    assertEquals(200, urlConnection.getResponseCode());
    RecordedRequest request = server.takeRequest();
    assertEquals("ABC", new String(request.getBody(), "UTF-8"));
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

Example 29 with MockResponse

use of com.google.mockwebserver.MockResponse in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpProxyToHttps.

/**
     * We were verifying the wrong hostname when connecting to an HTTPS site
     * through a proxy. http://b/3097277
     */
private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(hostnameVerifier);
    assertContent("this response comes via a secure proxy", connection);
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 30 with MockResponse

use of com.google.mockwebserver.MockResponse in project robovm by robovm.

the class URLConnectionTest method testReadTimeouts.

public void testReadTimeouts() throws IOException {
    /*
         * This relies on the fact that MockWebServer doesn't close the
         * connection after a response has been sent. This causes the client to
         * try to read more bytes than are sent, which results in a timeout.
         */
    MockResponse timeout = new MockResponse().setBody("ABC").clearHeaders().addHeader("Content-Length: 4");
    server.enqueue(timeout);
    // to keep the server alive
    server.enqueue(new MockResponse().setBody("unused"));
    server.play();
    URLConnection urlConnection = server.getUrl("/").openConnection();
    urlConnection.setReadTimeout(1000);
    InputStream in = urlConnection.getInputStream();
    assertEquals('A', in.read());
    assertEquals('B', in.read());
    assertEquals('C', in.read());
    try {
        // if Content-Length was accurate, this would return -1 immediately
        in.read();
        fail();
    } catch (SocketTimeoutException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)240 HttpURLConnection (java.net.HttpURLConnection)89 RecordedRequest (com.google.mockwebserver.RecordedRequest)80 HttpGet (org.apache.http.client.methods.HttpGet)54 HttpClient (org.apache.http.client.HttpClient)48 HttpResponse (org.apache.http.HttpResponse)42 MockWebServer (com.google.mockwebserver.MockWebServer)39 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)36 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)32 URLConnection (java.net.URLConnection)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)23 LargeTest (android.test.suitebuilder.annotation.LargeTest)18 GZIPInputStream (java.util.zip.GZIPInputStream)18 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)18 OutputStream (java.io.OutputStream)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 CookieManager (java.net.CookieManager)14 URL (java.net.URL)14