Search in sources :

Example 31 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

the class URLConnectionTest method testClientSendsContentLength.

public void testClientSendsContentLength() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();
    out.write(new byte[] { 'A', 'B', 'C' });
    out.close();
    assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request = server.takeRequest();
    assertContains(request.getHeaders(), "Content-Length: 3");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 32 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

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");
    // j2objc: URLConnection does not time out by default (i.e. getReadTimeout() returns 0).
    // When we create the native NSMutableURLRequest, we set its timeoutInterval to
    // JavaLangDouble_MAX_VALUE to make it never time out. This is problematic if the request
    // uses chunked transfer encoding, as it causes the NSURLSessionDataTask to send malformed
    // chunks and never terminate the request. A definite timeout is therefore needed here.
    conn.setReadTimeout(1000);
    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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 33 with RecordedRequest

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

the class URLConnectionTest method testRedirectToAnotherOriginServer.

public void testRedirectToAnotherOriginServer() throws Exception {
    MockWebServer server2 = new MockWebServer();
    try {
        // RoboVM note: Modified to call server2.shutdown() after test finishes regardless of outcome.
        server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
        server2.play();
        server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.getUrl("/").toString()).setBody("This page has moved!"));
        server.enqueue(new MockResponse().setBody("This is the first server again!"));
        server.play();
        URLConnection connection = server.getUrl("/").openConnection();
        assertEquals("This is the 2nd server!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
        assertEquals(server2.getUrl("/"), connection.getURL());
        // make sure the first server was careful to recycle the connection
        assertEquals("This is the first server again!", readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
        RecordedRequest first = server.takeRequest();
        assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort());
        RecordedRequest second = server2.takeRequest();
        assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort());
        RecordedRequest third = server.takeRequest();
        assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
    } finally {
        server2.shutdown();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 34 with RecordedRequest

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

the class URLConnectionTest method testProxyAuthenticateOnConnect.

public void testProxyAuthenticateOnConnect() throws Exception {
    Authenticator.setDefault(new SimpleAuthenticator());
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("A"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(server.toProxyAddress());
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(new RecordingHostnameVerifier());
    assertContent("A", connection);
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*");
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*");
}
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 35 with RecordedRequest

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

the class URLConnectionTest method testConnectViaHttps.

public void testConnectViaHttps() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("TLSv1", request.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)80 RecordedRequest (com.google.mockwebserver.RecordedRequest)80 HttpResponse (org.apache.http.HttpResponse)36 HttpClient (org.apache.http.client.HttpClient)36 HttpGet (org.apache.http.client.methods.HttpGet)36 HttpURLConnection (java.net.HttpURLConnection)26 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)22 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)15 MockWebServer (com.google.mockwebserver.MockWebServer)14 Scheme (org.apache.http.conn.scheme.Scheme)12 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 OutputStream (java.io.OutputStream)11 GZIPOutputStream (java.util.zip.GZIPOutputStream)11 URL (java.net.URL)9 URLConnection (java.net.URLConnection)8 CookieManager (java.net.CookieManager)6 HttpHost (org.apache.http.HttpHost)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6