Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with MockResponse

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

the class URLConnectionTest method testConnectionCloseInResponse.

public void testConnectionCloseInResponse() throws IOException, InterruptedException {
    server.enqueue(new MockResponse().addHeader("Connection: close"));
    server.enqueue(new MockResponse());
    server.play();
    HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
    assertEquals(200, a.getResponseCode());
    HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
    assertEquals(200, b.getResponseCode());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals("When connection: close is used, each request should get its own connection", 0, server.takeRequest().getSequenceNumber());
}
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