Search in sources :

Example 16 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by ParanoidAndroid.

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 17 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by ParanoidAndroid.

the class AbstractProxyTest method testConnectViaProxy.

/**
     * http://code.google.com/p/android/issues/detail?id=2690
     */
private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
    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");
}
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 18 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by ParanoidAndroid.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 19 with HttpClient

use of org.apache.http.client.HttpClient in project neo4j by neo4j.

the class RetrieveNodeIT method shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue.

@Test
public void shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(nodeUri);
        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Host", "dummy.neo4j.org");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String entityBody = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
        assertThat(entityBody, containsString("http://dummy.neo4j.org/db/data/node/"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 20 with HttpClient

use of org.apache.http.client.HttpClient in project neo4j by neo4j.

the class RetrieveNodeIT method shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri.

@Test
public void shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(nodeUri);
        httpget.setHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String entityBody = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
        assertThat(entityBody, containsString(nodeUri.toString()));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.http.client.HttpClient)941 HttpResponse (org.apache.http.HttpResponse)584 HttpGet (org.apache.http.client.methods.HttpGet)429 IOException (java.io.IOException)308 Test (org.junit.Test)275 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)272 HttpPost (org.apache.http.client.methods.HttpPost)212 HttpEntity (org.apache.http.HttpEntity)128 URI (java.net.URI)92 InputStream (java.io.InputStream)81 StringEntity (org.apache.http.entity.StringEntity)74 ArrayList (java.util.ArrayList)69 ClientProtocolException (org.apache.http.client.ClientProtocolException)66 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)61 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)61 InputStreamReader (java.io.InputStreamReader)59 URL (java.net.URL)57 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)53 URISyntaxException (java.net.URISyntaxException)50 MockResponse (com.google.mockwebserver.MockResponse)48