Search in sources :

Example 51 with RecordedRequest

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

the class URLConnectionTest method testAuthenticateWithPost.

public void testAuthenticateWithPost() throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
    // fail auth three times...
    server.enqueue(pleaseAuthenticate);
    server.enqueue(pleaseAuthenticate);
    server.enqueue(pleaseAuthenticate);
    // ...then succeed the fourth time
    server.enqueue(new MockResponse().setBody("Successful auth!"));
    server.play();
    Authenticator.setDefault(new SimpleAuthenticator());
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    // no authorization header for the first request...
    RecordedRequest request = server.takeRequest();
    assertContainsNoneMatching(request.getHeaders(), "Authorization: .*");
    // ...but the three requests that follow include an authorization header
    for (int i = 0; i < 3; i++) {
        request = server.takeRequest();
        assertEquals("POST / HTTP/1.1", request.getRequestLine());
        assertContains(request.getHeaders(), "Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
        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)

Example 52 with RecordedRequest

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

the class URLConnectionTest method testClientConfiguredCustomContentEncoding.

public void testClientConfiguredCustomContentEncoding() throws Exception {
    server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    connection.addRequestProperty("Accept-Encoding", "custom");
    assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request = server.takeRequest();
    assertContains(request.getHeaders(), "Accept-Encoding: custom");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 53 with RecordedRequest

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

the class URLConnectionTest method testResponseRedirectedWithPost.

private void testResponseRedirectedWithPost(int redirectCode) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(redirectCode).addHeader("Location: /page2").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("Page 2"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/page1").openConnection();
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertTrue(connection.getDoOutput());
    RecordedRequest page1 = server.takeRequest();
    assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
    assertEquals(Arrays.toString(requestBody), Arrays.toString(page1.getBody()));
    RecordedRequest page2 = server.takeRequest();
    assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
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)

Example 54 with RecordedRequest

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

the class URLConnectionTest method testRequestHeaders.

public void testRequestHeaders() throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    server.play();
    HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
    urlConnection.addRequestProperty("D", "e");
    urlConnection.addRequestProperty("D", "f");
    assertEquals("f", urlConnection.getRequestProperty("D"));
    assertEquals("f", urlConnection.getRequestProperty("d"));
    Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties();
    assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D")));
    assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("d")));
    try {
        requestHeaders.put("G", Arrays.asList("h"));
        fail("Modified an unmodifiable view.");
    } catch (UnsupportedOperationException expected) {
    }
    try {
        requestHeaders.get("D").add("i");
        fail("Modified an unmodifiable view.");
    } catch (UnsupportedOperationException expected) {
    }
    try {
        urlConnection.setRequestProperty(null, "j");
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        urlConnection.addRequestProperty(null, "k");
        fail();
    } catch (NullPointerException expected) {
    }
    // should fail silently!
    urlConnection.setRequestProperty("NullValue", null);
    assertNull(urlConnection.getRequestProperty("NullValue"));
    // should fail silently!
    urlConnection.addRequestProperty("AnotherNullValue", null);
    assertNull(urlConnection.getRequestProperty("AnotherNullValue"));
    urlConnection.getResponseCode();
    RecordedRequest request = server.takeRequest();
    assertContains(request.getHeaders(), "D: e");
    assertContains(request.getHeaders(), "D: f");
    assertContainsNoneMatching(request.getHeaders(), "NullValue.*");
    assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*");
    assertContainsNoneMatching(request.getHeaders(), "G:.*");
    assertContainsNoneMatching(request.getHeaders(), "null:.*");
    try {
        urlConnection.addRequestProperty("N", "o");
        fail("Set header after connect");
    } catch (IllegalStateException expected) {
    }
    try {
        urlConnection.setRequestProperty("P", "q");
        fail("Set header after connect");
    } catch (IllegalStateException expected) {
    }
    try {
        urlConnection.getRequestProperties();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 55 with RecordedRequest

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

the class URLConnectionTest method testProxyConnectIncludesProxyHeadersOnly.

/**
     * Test which headers are sent unencrypted to the HTTP proxy.
     */
public void testProxyConnectIncludesProxyHeadersOnly() throws IOException, InterruptedException {
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(server.toProxyAddress());
    connection.addRequestProperty("Private", "Secret");
    connection.addRequestProperty("Proxy-Authorization", "bar");
    connection.addRequestProperty("User-Agent", "baz");
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(hostnameVerifier);
    assertContent("encrypted response from the origin server", connection);
    RecordedRequest connect = server.takeRequest();
    assertContainsNoneMatching(connect.getHeaders(), "Private.*");
    assertContains(connect.getHeaders(), "Proxy-Authorization: bar");
    assertContains(connect.getHeaders(), "User-Agent: baz");
    assertContains(connect.getHeaders(), "Host: android.com");
    assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive");
    RecordedRequest get = server.takeRequest();
    assertContains(get.getHeaders(), "Private: Secret");
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
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)

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