FileDocCategorySizeDatePackage
AbstractProxyTest.javaAPI DocAndroid 5.1 API12215Thu Mar 12 22:22:12 GMT 2015android.net.http

AbstractProxyTest

public abstract class AbstractProxyTest extends TestCase

Fields Summary
private com.google.mockwebserver.MockWebServer
server
Constructors Summary
Methods Summary
private voidassertContains(java.util.List headers, java.lang.String header)

            @Override void configure(MockWebServer server, HttpClient client, HttpRequest request) {
                System.setProperty("proxyHost", "localhost");
                System.setProperty("proxyPort", Integer.toString(server.getPort()));
            }
        
            @Override void configure(MockWebServer server, HttpClient client, HttpRequest request) {
                System.setProperty("http.proxyHost", "localhost");
                System.setProperty("http.proxyPort", Integer.toString(server.getPort()));
            }
        
            @Override void configure(MockWebServer server, HttpClient client, HttpRequest request) {
                System.setProperty("https.proxyHost", "localhost");
                System.setProperty("https.proxyPort", Integer.toString(server.getPort()));
            }
        
            @Override void configure(MockWebServer server, HttpClient client, HttpRequest request) {
                client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                        new HttpHost("localhost", server.getPort()));
            }
        
            @Override void configure(MockWebServer server, HttpClient client, HttpRequest request) {
                request.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                        new HttpHost("localhost", server.getPort()));
            }
        
        assertTrue(headers.toString(), headers.contains(header));
    
private java.lang.StringcontentToString(org.apache.http.HttpResponse response)

        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        Reader reader = new InputStreamReader(response.getEntity().getContent());
        int length;
        while ((length = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, length);
        }
        reader.close();
        return writer.toString();
    
protected abstract org.apache.http.client.HttpClientnewHttpClient()

private org.apache.http.conn.ssl.SSLSocketFactorynewSslSocketFactory(libcore.javax.net.ssl.TestSSLContext testSSLContext)

        // call through to Apache HTTP's non-public SSLSocketFactory constructor
        return SSLSocketFactory.class.getConstructor(javax.net.ssl.SSLSocketFactory.class)
                .newInstance(testSSLContext.clientContext.getSocketFactory());
    
protected voidtearDown()


       

          
        System.clearProperty("proxyHost");
        System.clearProperty("proxyPort");
        System.clearProperty("http.proxyHost");
        System.clearProperty("http.proxyPort");
        System.clearProperty("https.proxyHost");
        System.clearProperty("https.proxyPort");

        server.shutdown();
        super.tearDown();
    
public voidtestClientParamPreferredOverSystemProperty()

        testParamPreferredOverSystemProperty(ProxyConfig.CLIENT_PARAMETER);
    
public voidtestConnectToHttps()

        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());
    
private voidtestConnectViaHttpProxyToHttps(android.net.http.AbstractProxyTest$ProxyConfig proxyConfig)

        TestSSLContext testSSLContext = TestSSLContext.create();

        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
        server.enqueue(new MockResponse()
                .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
                .clearHeaders());
        server.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody("this response comes via a secure proxy"));
        server.play();

        HttpClient httpProxyClient = newHttpClient();
        SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
        sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
        httpProxyClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", sslSocketFactory, 443));

        HttpGet request = new HttpGet("https://android.com/foo");
        proxyConfig.configure(server, httpProxyClient, request);

        HttpResponse response = httpProxyClient.execute(request);
        assertEquals("this response comes via a secure proxy", contentToString(response));

        RecordedRequest connect = server.takeRequest();
        assertEquals("Connect line failure on proxy " + proxyConfig,
                "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
        assertContains(connect.getHeaders(), "Host: android.com");

        RecordedRequest get = server.takeRequest();
        assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
        assertContains(get.getHeaders(), "Host: android.com");
    
public voidtestConnectViaHttpProxyToHttpsUsingClientParameter()

        testConnectViaHttpProxyToHttps(ProxyConfig.CLIENT_PARAMETER);
    
public voidtestConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty()

        testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY);
    
public voidtestConnectViaHttpProxyToHttpsUsingProxySystemProperty()

        testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY);
    
public voidtestConnectViaHttpProxyToHttpsUsingRequestParameter()

        testConnectViaHttpProxyToHttps(ProxyConfig.REQUEST_PARAMETER);
    
private voidtestConnectViaProxy(android.net.http.AbstractProxyTest$ProxyConfig proxyConfig)
http://code.google.com/p/android/issues/detail?id=2690

        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");
    
public voidtestConnectViaProxyUsingClientParameter()

        testConnectViaProxy(ProxyConfig.CLIENT_PARAMETER);
    
public voidtestConnectViaProxyUsingHttpProxySystemProperty()

        testConnectViaProxy(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY);
    
public voidtestConnectViaProxyUsingProxySystemProperty()
We had bugs where proxy system properties weren't being honored. http://b/3254717

        testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY);
    
public voidtestConnectViaProxyUsingRequestParameter()

        testConnectViaProxy(ProxyConfig.REQUEST_PARAMETER);
    
public voidtestExplicitNoProxyCancelsSystemProperty()

        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());
    
private voidtestParamPreferredOverSystemProperty(android.net.http.AbstractProxyTest$ProxyConfig proxyConfig)

        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());
    
public voidtestRequestParamPreferredOverSystemProperty()

        testParamPreferredOverSystemProperty(ProxyConfig.REQUEST_PARAMETER);
    
public voidtestRetryWithProxy()

        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) {
        }