Search in sources :

Example 81 with MockResponse

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

the class URLConnectionTest method testClientConfiguredCustomContentEncoding.

// JVM failure.
//    /**
//     * 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");
//    }
// TODO(tball): b/28067294
//    public void testClientConfiguredGzipContentEncoding() throws Exception {
//        byte[] bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"));
//        server.enqueue(new MockResponse()
//                .setBody(bodyBytes)
//                .addHeader("Content-Encoding: gzip")
//                .addHeader("Content-Length: " + bodyBytes.length));
//        server.play();
//
//        URLConnection connection = server.getUrl("/").openConnection();
//        connection.addRequestProperty("Accept-Encoding", "gzip");
//        InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
//        assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
//        assertEquals(bodyBytes.length, connection.getContentLength());
//
//        RecordedRequest request = server.takeRequest();
//        assertContains(request.getHeaders(), "Accept-Encoding: gzip");
//        assertEquals("gzip", connection.getContentEncoding());
//    }
// TODO(tball): b/28067294
//    public void testGzipAndConnectionReuseWithFixedLength() throws Exception {
//        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH);
//    }
// TODO(tball): b/28067294
//    public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception {
//        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED);
//    }
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)

Example 82 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 83 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)

Example 84 with MockResponse

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

the class DownloadManagerFunctionalTest method testErrorTooManyRedirects.

/**
     * Tests the download failure error after too many redirects (>5).
     */
@LargeTest
public void testErrorTooManyRedirects() throws Exception {
    Uri uri = getServerUri(DEFAULT_FILENAME);
    // force 6 redirects
    for (int i = 0; i < 6; ++i) {
        final MockResponse resp = buildResponse(HTTP_REDIRECT);
        resp.setHeader("Location", uri.toString());
        enqueueResponse(resp);
    }
    doErrorTest(uri, DownloadManager.ERROR_TOO_MANY_REDIRECTS);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 85 with MockResponse

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

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)

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