Search in sources :

Example 61 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ResurrectionRemix.

the class CookiesTest method testCookiesWithNonMatchingCase.

/**
     * Test that cookies aren't case-sensitive with respect to hostname.
     * http://b/3167208
     */
public void testCookiesWithNonMatchingCase() throws Exception {
    // use a proxy so we can manipulate the origin server's host name
    server = new MockWebServer();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=first; Domain=my.t-mobile.com").addHeader("Set-Cookie: b=second; Domain=.T-mobile.com").addHeader("Set-Cookie: c=third; Domain=.t-mobile.com").setBody("This response sets some cookies."));
    server.enqueue(new MockResponse().setBody("This response gets those cookies back."));
    server.play();
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", server.getPort()));
    HttpResponse getCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    getCookies.getEntity().consumeContent();
    server.takeRequest();
    HttpResponse sendCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    sendCookies.getEntity().consumeContent();
    RecordedRequest sendCookiesRequest = server.takeRequest();
    assertContains(sendCookiesRequest.getHeaders(), "Cookie: a=first; b=second; c=third");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 62 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ResurrectionRemix.

the class AbstractProxyTest method testConnectViaProxy.

/**
     * http://code.google.com/p/android/issues/detail?id=2690
     */
private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setBody("this response comes via a proxy");
    server.enqueue(mockResponse);
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    HttpGet request = new HttpGet("http://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a proxy", contentToString(response));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET http://android.com/foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 63 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ResurrectionRemix.

the class AbstractProxyTest method testExplicitNoProxyCancelsSystemProperty.

public void testExplicitNoProxyCancelsSystemProperty() throws Exception {
    server.enqueue(new MockResponse().setBody("Via the origin server!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet(server.getUrl("/bar").toURI());
    request.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, ConnRouteParams.NO_HOST);
    HttpResponse response = client.execute(request);
    assertEquals("Via the origin server!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET /bar HTTP/1.1", recordedRequest.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 64 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerBaseTest method buildResponse.

/**
     * Helper to build a response from the MockWebServer with no body.
     *
     * @param status The HTTP status code to return for this response
     * @return Returns the mock web server response that was queued (which can be modified)
     */
protected MockResponse buildResponse(int status) {
    MockResponse response = new MockResponse().setResponseCode(status);
    response.setHeader("Content-type", mFileType);
    return response;
}
Also used : MockResponse(com.google.mockwebserver.MockResponse)

Example 65 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerFunctionalTest method testServerDropConnection_body.

/**
     * Tests that we get an error code when the server drops the connection during a download.
     */
@LargeTest
public void testServerDropConnection_body() throws Exception {
    // file size = 25000 bytes
    byte[] blobData = generateData(25000, DataType.TEXT);
    final MockResponse resp = buildResponse(HTTP_OK, blobData);
    resp.setHeader("Content-Length", "50000");
    enqueueResponse(resp);
    long dlRequest = doCommonStandardEnqueue();
    waitForDownloadOrTimeout(dlRequest);
    Cursor cursor = getCursor(dlRequest);
    try {
        verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_FAILED);
        verifyInt(cursor, DownloadManager.COLUMN_REASON, DownloadManager.ERROR_CANNOT_RESUME);
    } finally {
        cursor.close();
    }
    // Even tho the server drops the connection, we should still get a completed notification
    assertEquals(1, mReceiver.numDownloadsCompleted());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Cursor(android.database.Cursor) LargeTest(android.test.suitebuilder.annotation.LargeTest)

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