Search in sources :

Example 71 with MockResponse

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

the class AbstractProxyTest method testParamPreferredOverSystemProperty.

private void testParamPreferredOverSystemProperty(ProxyConfig proxyConfig) throws Exception {
    server.enqueue(new MockResponse().setBody("Via request parameter proxy!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet("http://origin.foo/bar");
    proxyConfig.configure(server, client, request);
    HttpResponse response = client.execute(request);
    assertEquals("Via request parameter proxy!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET http://origin.foo/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 72 with MockResponse

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

the class AbstractProxyTest method testConnectToHttps.

public void testConnectToHttps() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
    server.play();
    HttpClient httpClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, server.getPort()));
    HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
    assertEquals("this response comes via HTTPS", contentToString(response));
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 73 with MockResponse

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

the class AbstractProxyTest method testRetryWithProxy.

// http://b/5372438
public void testRetryWithProxy() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    HttpGet request = new HttpGet("http://android.com/foo");
    ProxyConfig.REQUEST_PARAMETER.configure(server, httpProxyClient, request);
    try {
        httpProxyClient.execute(request);
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException)

Example 74 with MockResponse

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

the class DownloadManagerFunctionalTest method testRelativeRedirect.

/**
     * Tests the download failure error from an unhandled HTTP status code
     */
@LargeTest
public void testRelativeRedirect() throws Exception {
    Uri uri = getServerUri(DEFAULT_FILENAME);
    final MockResponse resp = buildResponse(HTTP_REDIRECT);
    resp.setHeader("Location", ":" + uri.getSchemeSpecificPart());
    enqueueResponse(resp);
    byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
    enqueueResponse(buildResponse(HTTP_OK, blobData));
    Request request = new Request(uri);
    request.setTitle(DEFAULT_FILENAME);
    long dlRequest = mDownloadManager.enqueue(request);
    waitForDownloadOrTimeout(dlRequest);
    verifyAndCleanupSingleFileDownload(dlRequest, blobData);
    assertEquals(1, mReceiver.numDownloadsCompleted());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Request(android.app.DownloadManager.Request) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 75 with MockResponse

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

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