Search in sources :

Example 31 with MockResponse

use of com.google.mockwebserver.MockResponse 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 32 with MockResponse

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

the class URLConnectionTest method testNonRetryableRequestBodyAfterBrokenConnection.

public void testNonRetryableRequestBodyAfterBrokenConnection() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AFTER_READING_REQUEST));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/a").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setDoOutput(true);
    connection.setFixedLengthStreamingMode(3);
    OutputStream out = connection.getOutputStream();
    out.write(new byte[] { 1, 2, 3 });
    out.close();
    try {
        connection.getInputStream();
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) IOException(java.io.IOException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 33 with MockResponse

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

the class URLConnectionTest method testConnectViaHttpsReusingConnectionsDifferentFactories.

public void testConnectViaHttpsReusingConnectionsDifferentFactories() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    server.play();
    // install a custom SSL socket factory so the server can be authorized
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    try {
        readAscii(connection.getInputStream(), Integer.MAX_VALUE);
        fail("without an SSL socket factory, the connection should fail");
    } catch (SSLException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLException(javax.net.ssl.SSLException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 34 with MockResponse

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

the class URLConnectionTest method testReadTimeoutsOnRecycledConnections.

public void testReadTimeoutsOnRecycledConnections() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC"));
    server.play();
    // The request should work once and then fail
    URLConnection connection = server.getUrl("").openConnection();
    // Read timeout of a day, sure to cause the test to timeout and fail.
    connection.setReadTimeout(24 * 3600 * 1000);
    InputStream input = connection.getInputStream();
    assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
    input.close();
    try {
        connection = server.getUrl("").openConnection();
        // Set the read timeout back to 100ms, this request will time out
        // because we've only enqueued one response.
        connection.setReadTimeout(100);
        connection.getInputStream();
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 35 with MockResponse

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

the class URLConnectionTest method testSetDoOutputOrDoInputAfterConnectFails.

public void testSetDoOutputOrDoInputAfterConnectFails() throws Exception {
    server.enqueue(new MockResponse());
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    connection.connect();
    try {
        connection.setDoOutput(true);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.setDoInput(true);
        fail();
    } catch (IllegalStateException expected) {
    }
    connection.disconnect();
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

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