Search in sources :

Example 16 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.

the class DropwizardApacheConnectorTest method multiple_headers_with_the_same_name_are_processed_successfully.

@Test
public void multiple_headers_with_the_same_name_are_processed_successfully() throws Exception {
    final CloseableHttpClient client = mock(CloseableHttpClient.class);
    final DropwizardApacheConnector dropwizardApacheConnector = new DropwizardApacheConnector(client, null, false);
    final Header[] apacheHeaders = { new BasicHeader("Set-Cookie", "test1"), new BasicHeader("Set-Cookie", "test2") };
    final CloseableHttpResponse apacheResponse = mock(CloseableHttpResponse.class);
    when(apacheResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
    when(apacheResponse.getAllHeaders()).thenReturn(apacheHeaders);
    when(client.execute(Mockito.any())).thenReturn(apacheResponse);
    final ClientRequest jerseyRequest = mock(ClientRequest.class);
    when(jerseyRequest.getUri()).thenReturn(URI.create("http://localhost"));
    when(jerseyRequest.getMethod()).thenReturn("GET");
    when(jerseyRequest.getHeaders()).thenReturn(new MultivaluedHashMap<>());
    final ClientResponse jerseyResponse = dropwizardApacheConnector.apply(jerseyRequest);
    assertThat(jerseyResponse.getStatus()).isEqualTo(apacheResponse.getStatusLine().getStatusCode());
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicHeader(org.apache.http.message.BasicHeader) ClientRequest(org.glassfish.jersey.client.ClientRequest) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 17 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesTheDefaultRoutePlanner.

@Test
public void usesTheDefaultRoutePlanner() throws Exception {
    final CloseableHttpClient httpClient = builder.using(configuration).createClient(apacheBuilder, connectionManager, "test").getClient();
    assertThat(httpClient).isNotNull();
    assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isNull();
    assertThat(spyHttpClientField("routePlanner", httpClient)).isInstanceOf(DefaultRoutePlanner.class);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Test(org.junit.Test)

Example 18 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesProxyWithAuth.

@Test
public void usesProxyWithAuth() throws Exception {
    HttpClientConfiguration config = new HttpClientConfiguration();
    AuthConfiguration auth = new AuthConfiguration("secret", "stuff");
    ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080, "http", auth);
    config.setProxyConfiguration(proxy);
    CloseableHttpClient httpClient = checkProxy(config, new HttpHost("dropwizard.io", 80), new HttpHost("192.168.52.11", 8080, "http"));
    CredentialsProvider credentialsProvider = (CredentialsProvider) FieldUtils.getField(httpClient.getClass(), "credentialsProvider", true).get(httpClient);
    assertThat(credentialsProvider.getCredentials(new AuthScope("192.168.52.11", 8080))).isEqualTo(new UsernamePasswordCredentials("secret", "stuff"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 19 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.

the class HttpClientBuilderTest method checkProxy.

private CloseableHttpClient checkProxy(HttpClientConfiguration config, HttpHost target, HttpHost expectedProxy) throws Exception {
    CloseableHttpClient httpClient = builder.using(config).build("test");
    HttpRoutePlanner routePlanner = (HttpRoutePlanner) FieldUtils.getField(httpClient.getClass(), "routePlanner", true).get(httpClient);
    HttpRoute route = routePlanner.determineRoute(target, new HttpGet(target.toURI()), new BasicHttpContext());
    assertThat(route.getProxyHost()).isEqualTo(expectedProxy);
    assertThat(route.getTargetHost()).isEqualTo(target);
    assertThat(route.getHopCount()).isEqualTo(expectedProxy != null ? 2 : 1);
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet)

Example 20 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestNanoFileUpLoad method testPostWithMultipartFormUpload3.

@Test
public void testPostWithMultipartFormUpload3() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String textFileName = UPLOAD_JAVA_FILE;
    HttpPost post = new HttpPost("http://localhost:8192/uploadFile3");
    executeUpload(httpclient, textFileName, post);
    FileItem file = this.testServer.files.get("upfile").get(0);
    Assert.assertEquals(file.getSize(), new File(textFileName).length());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) Test(org.junit.Test)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)353 HttpGet (org.apache.http.client.methods.HttpGet)181 Test (org.junit.Test)178 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)172 HttpResponse (org.apache.http.HttpResponse)105 HttpEntity (org.apache.http.HttpEntity)82 IOException (java.io.IOException)75 HttpPost (org.apache.http.client.methods.HttpPost)62 StringEntity (org.apache.http.entity.StringEntity)59 InputStream (java.io.InputStream)40 StatusLine (org.apache.http.StatusLine)40 URI (java.net.URI)34 HttpHost (org.apache.http.HttpHost)29 RequestConfig (org.apache.http.client.config.RequestConfig)26 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 Header (org.apache.http.Header)20 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)19 AuthScope (org.apache.http.auth.AuthScope)18 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)18 CredentialsProvider (org.apache.http.client.CredentialsProvider)16