Search in sources :

Example 86 with MockResponse

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

the class URLConnectionTest method testConnectViaHttpsWithSSLFallback.

public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
    server.enqueue(new MockResponse().setBody("this response comes via SSL"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via SSL", connection);
    // The first request will be an incomplete (bookkeeping) request
    // that the server disconnected from at start.
    server.takeRequest();
    // The request will be retried.
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 87 with MockResponse

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

the class URLConnectionTest method testSslFallback.

public void testSslFallback() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("This required a 2nd handshake"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertEquals("This required a 2nd handshake", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals(0, first.getSequenceNumber());
    RecordedRequest retry = server.takeRequest();
    assertEquals(0, retry.getSequenceNumber());
    assertEquals("SSLv3", retry.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 88 with MockResponse

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

the class URLConnectionTest method testGetContentOfType.

public void testGetContentOfType() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    try {
        connection.getContent(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        connection.getContent(new Class[] { null });
        fail();
    } catch (NullPointerException expected) {
    }
    assertNull(connection.getContent(new Class[] { getClass() }));
    connection.disconnect();
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

Example 89 with MockResponse

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

the class URLConnectionTest method testServerShutdownOutput.

public void testServerShutdownOutput() throws Exception {
    // This test causes MockWebServer to log a "connection failed" stack trace
    server.enqueue(new MockResponse().setBody("Output shutdown after this response").setSocketPolicy(SHUTDOWN_OUTPUT_AT_END));
    server.enqueue(new MockResponse().setBody("This response will fail to write"));
    server.enqueue(new MockResponse().setBody("This comes after a busted connection"));
    server.play();
    assertContent("Output shutdown after this response", server.getUrl("/a").openConnection());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertContent("This comes after a busted connection", server.getUrl("/b").openConnection());
    assertEquals(1, server.takeRequest().getSequenceNumber());
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse)

Example 90 with MockResponse

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

the class URLConnectionTest method testAuthenticateWithStreamingPost.

private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    server.play();
    Authenticator.setDefault(new SimpleAuthenticator());
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    if (streamingMode == StreamingMode.FIXED_LENGTH) {
        connection.setFixedLengthStreamingMode(requestBody.length);
    } else if (streamingMode == StreamingMode.CHUNKED) {
        connection.setChunkedStreamingMode(0);
    }
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    try {
        connection.getInputStream();
        fail();
    } catch (HttpRetryException expected) {
    }
    // no authorization header for the request...
    RecordedRequest request = server.takeRequest();
    assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
    assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
}
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) HttpRetryException(java.net.HttpRetryException)

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