Search in sources :

Example 76 with MockResponse

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

the class URLConnectionTest method testConnectViaProxy.

//  JVM failure.
//    public void testGetResponseCodeNoResponseBody() throws Exception {
//        server.enqueue(new MockResponse()
//                .addHeader("abc: def"));
//        server.play();
//
//        URL url = server.getUrl("/");
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setDoInput(false);
//        assertEquals("def", conn.getHeaderField("abc"));
//        assertEquals(200, conn.getResponseCode());
//        try {
//            conn.getInputStream();
//            fail();
//        } catch (ProtocolException expected) {
//        }
//    }
// TODO(tball): b/28067294
//    public void testConnectViaProxyUsingProxyArg() throws Exception {
//        testConnectViaProxy(ProxyConfig.CREATE_ARG);
//    }
// TODO(tball): b/28067294
//    public void testConnectViaProxyUsingProxySystemProperty() throws Exception {
//        testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY);
//    }
private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
    MockResponse mockResponse = new MockResponse().setBody("this response comes via a proxy");
    server.enqueue(mockResponse);
    server.play();
    URL url = new URL("http://android.com/foo");
    HttpURLConnection connection = proxyConfig.connect(server, url);
    assertContent("this response comes via a proxy", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET http://android.com/foo HTTP/1.1", request.getRequestLine());
    assertContains(request.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL)

Example 77 with MockResponse

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

the class URLConnectionTest method testMarkAndReset.

//  JVM failure.
//    public void testMarkAndResetWithNoLengthHeaders() throws IOException {
//        testMarkAndReset(TransferKind.END_OF_STREAM);
//    }
private void testMarkAndReset(TransferKind transferKind) throws IOException {
    MockResponse response = new MockResponse();
    transferKind.setBody(response, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1024);
    server.enqueue(response);
    server.enqueue(response);
    server.play();
    InputStream in = server.getUrl("/").openConnection().getInputStream();
    assertFalse("This implementation claims to support mark().", in.markSupported());
    in.mark(5);
    assertEquals("ABCDE", readAscii(in, 5));
    try {
        in.reset();
        fail();
    } catch (IOException expected) {
    }
    assertEquals("FGHIJKLMNOPQRSTUVWXYZ", readAscii(in, Integer.MAX_VALUE));
    assertContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ", server.getUrl("/").openConnection());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 78 with MockResponse

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

the class URLConnectionTest method testInputStreamAvailable.

private void testInputStreamAvailable(TransferKind transferKind) throws IOException {
    String body = "ABCDEFGH";
    MockResponse response = new MockResponse();
    transferKind.setBody(response, body, 4);
    server.enqueue(response);
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    InputStream in = connection.getInputStream();
    for (int i = 0; i < body.length(); i++) {
        assertTrue(in.available() >= 0);
        assertEquals(body.charAt(i), in.read());
    }
    assertEquals(0, in.available());
    assertEquals(-1, in.read());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 79 with MockResponse

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

the class URLConnectionTest method testGetContentLengthConnects.

public void testGetContentLengthConnects() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    assertEquals(3, connection.getContentLength());
    connection.disconnect();
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

Example 80 with MockResponse

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

the class URLConnectionTest method testNotRedirectedFromHttpToHttps.

public void testNotRedirectedFromHttpToHttps() throws IOException, InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: https://anyhost/foo").setBody("This page has moved!"));
    server.play();
    HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
    assertEquals("This page has moved!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection)

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