Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf1df887 authored by Jesse Wilson's avatar Jesse Wilson
Browse files

New tests for AndroidHttpClient with a proxy server.

Change-Id: I03ec2f1257824c8675156dd80f6045512d1a36fe
http://b/3254717
parent 80bc16f7
Loading
Loading
Loading
Loading
+18 −12
Original line number Diff line number Diff line
@@ -33,15 +33,16 @@ import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import tests.http.MockResponse;
import tests.http.MockWebServer;
import tests.http.RecordedRequest;

public class ProxyTest extends TestCase {
public abstract class AbstractProxyTest extends TestCase {

    private MockWebServer server = new MockWebServer();

    protected abstract HttpClient newHttpClient();

    @Override protected void tearDown() throws Exception {
        System.clearProperty("proxyHost");
        System.clearProperty("proxyPort");
@@ -54,7 +55,7 @@ public class ProxyTest extends TestCase {
        super.tearDown();
    }

    public void testConnectToHttps() throws IOException, InterruptedException {
    public void testConnectToHttps() throws Exception {
        TestSSLContext testSSLContext = TestSSLContext.create();

        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
@@ -63,9 +64,9 @@ public class ProxyTest extends TestCase {
                .setBody("this response comes via HTTPS"));
        server.play();

        HttpClient httpClient = new DefaultHttpClient();
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(
                testSSLContext.clientContext.getSocketFactory());
        HttpClient httpClient = newHttpClient();

        SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
        sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
        httpClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", sslSocketFactory, server.getPort()));
@@ -78,6 +79,12 @@ public class ProxyTest extends TestCase {
        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    }

    private SSLSocketFactory newSslSocketFactory(TestSSLContext testSSLContext) throws Exception {
        // call through to Apache HTTP's non-public SSLSocketFactory constructor
        return SSLSocketFactory.class.getConstructor(javax.net.ssl.SSLSocketFactory.class)
                .newInstance(testSSLContext.clientContext.getSocketFactory());
    }

    /**
     * We had bugs where proxy system properties weren't being honored.
     * http://b/3254717
@@ -108,7 +115,7 @@ public class ProxyTest extends TestCase {
        server.enqueue(mockResponse);
        server.play();

        HttpClient httpProxyClient = new DefaultHttpClient();
        HttpClient httpProxyClient = newHttpClient();

        HttpGet request = new HttpGet("http://android.com/foo");
        proxyConfig.configure(server, httpProxyClient, request);
@@ -150,9 +157,8 @@ public class ProxyTest extends TestCase {
                .setBody("this response comes via a secure proxy"));
        server.play();

        HttpClient httpProxyClient = new DefaultHttpClient();
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(
                testSSLContext.clientContext.getSocketFactory());
        HttpClient httpProxyClient = newHttpClient();
        SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
        sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
        httpProxyClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", sslSocketFactory, 443));
@@ -187,7 +193,7 @@ public class ProxyTest extends TestCase {
        System.setProperty("http.proxyHost", "proxy.foo");
        System.setProperty("http.proxyPort", "8080");

        HttpClient client = new DefaultHttpClient();
        HttpClient client = newHttpClient();
        HttpGet request = new HttpGet("http://origin.foo/bar");
        proxyConfig.configure(server, client, request);
        HttpResponse response = client.execute(request);
@@ -203,7 +209,7 @@ public class ProxyTest extends TestCase {
        System.setProperty("http.proxyHost", "proxy.foo");
        System.setProperty("http.proxyPort", "8080");

        HttpClient client = new DefaultHttpClient();
        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);
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.http;

import org.apache.http.client.HttpClient;

public final class AndroidHttpClientProxyTest extends AbstractProxyTest {

    @Override protected HttpClient newHttpClient() {
        return AndroidHttpClient.newInstance(AbstractProxyTest.class.getName());
    }
}
+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.http;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

public final class DefaultHttpClientProxyTest extends AbstractProxyTest {

    @Override protected HttpClient newHttpClient() {
        return new DefaultHttpClient();
    }
}