Search in sources :

Example 36 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)

Example 37 with MockResponse

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

the class URLConnectionTest method testInspectSslBeforeConnect.

public void testInspectSslBeforeConnect() 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());
    assertNotNull(connection.getHostnameVerifier());
    try {
        connection.getLocalCertificates();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getServerCertificates();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getCipherSuite();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getPeerPrincipal();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 38 with MockResponse

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

the class URLConnectionTest method testNonHexChunkSize.

public void testNonHexChunkSize() throws IOException {
    server.enqueue(new MockResponse().setBody("5\r\nABCDE\r\nG\r\nFGHIJKLMNOPQRSTU\r\n0\r\n\r\n").clearHeaders().addHeader("Transfer-encoding: chunked"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    try {
        readAscii(connection.getInputStream(), Integer.MAX_VALUE);
        fail();
    } catch (IOException e) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 39 with MockResponse

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

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