Search in sources :

Example 56 with RecordedRequest

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

the class URLConnectionTest method testRedirectedOnHttps.

public void testRedirectedOnHttps() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the new location!"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals("GET / HTTP/1.1", first.getRequestLine());
    RecordedRequest retry = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
    assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 57 with RecordedRequest

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

the class URLConnectionTest method testGzipEncodingEnabledByDefault.

/**
     * This test checks whether connections are gzipped by default. This
     * behavior in not required by the API, so a failure of this test does not
     * imply a bug in the implementation.
     */
public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException {
    server.enqueue(new MockResponse().setBody(gzip("ABCABCABC".getBytes("UTF-8"))).addHeader("Content-Encoding: gzip"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertNull(connection.getContentEncoding());
    assertEquals(-1, connection.getContentLength());
    RecordedRequest request = server.takeRequest();
    assertContains(request.getHeaders(), "Accept-Encoding: gzip");
}
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 58 with RecordedRequest

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

the class URLConnectionTest method testRedirected.

private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception {
    MockResponse response = new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo");
    transferKind.setBody(response, "This page has moved!", 10);
    server.enqueue(response);
    server.enqueue(new MockResponse().setBody("This is the new location!"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals("GET / HTTP/1.1", first.getRequestLine());
    RecordedRequest retry = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
    if (reuse) {
        assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
    }
}
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 59 with RecordedRequest

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

the class URLConnectionTest method testAuthenticateWithGet.

public void testAuthenticateWithGet() 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();
    SimpleAuthenticator authenticator = new SimpleAuthenticator();
    Authenticator.setDefault(authenticator);
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals(Authenticator.RequestorType.SERVER, authenticator.requestorType);
    assertEquals(server.getPort(), authenticator.requestingPort);
    assertEquals(InetAddress.getByName(server.getHostName()), authenticator.requestingSite);
    assertEquals("protected area", authenticator.requestingPrompt);
    assertEquals("http", authenticator.requestingProtocol);
    assertEquals("Basic", authenticator.requestingScheme);
    // no authorization header for the first request...
    RecordedRequest request = server.takeRequest();
    assertContainsNoneMatching(request.getHeaders(), "Authorization: .*");
    // ...but the three requests that follow requests include an authorization header
    for (int i = 0; i < 3; i++) {
        request = server.takeRequest();
        assertEquals("GET / HTTP/1.1", request.getRequestLine());
        assertContains(request.getHeaders(), "Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

Example 60 with RecordedRequest

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

the class URLConnectionTest method testClientSendsContentLength.

public void testClientSendsContentLength() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();
    out.write(new byte[] { 'A', 'B', 'C' });
    out.close();
    assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request = server.takeRequest();
    assertContains(request.getHeaders(), "Content-Length: 3");
}
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)

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