Search in sources :

Example 6 with CloseableHttpClient

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

the class CxfBeanTest method testPutConsumer.

@Test
public void testPutConsumer() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + PORT1 + "/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 7 with CloseableHttpClient

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

the class CxfBeanTest method testPostConsumerUniqueResponseCode.

@Test
public void testPostConsumerUniqueResponseCode() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + PORT1 + "/customerservice/customersUniqueResponseCode");
    post.addHeader("Accept", "text/xml");
    StringEntity entity = new StringEntity(POST2_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    post.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(post);
        assertEquals(201, response.getStatusLine().getStatusCode());
        String id = getCustomerId("James");
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>" + id + "</id><name>James</name></Customer>", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 8 with CloseableHttpClient

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

the class CxfRsRouterTest method testGetCustomer.

@Test
public void testGetCustomer() throws Exception {
    HttpGet get = new HttpGet("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers/123");
    get.addHeader("Accept", "application/json");
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(get);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("{\"Customer\":{\"id\":123,\"name\":\"John\"}}", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 9 with CloseableHttpClient

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

the class CxfRsRouterTest method testPostConsumerUniqueResponseCode.

@Test
public void testPostConsumerUniqueResponseCode() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customersUniqueResponseCode");
    post.addHeader("Accept", "text/xml");
    StringEntity entity = new StringEntity(POST_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    post.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(post);
        assertEquals(201, response.getStatusLine().getStatusCode());
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>124</id><name>Jack</name></Customer>", EntityUtils.toString(response.getEntity()));
        HttpDelete del = new HttpDelete("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers/124/");
        response = httpclient.execute(del);
        // need to check the response of delete method
        assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        httpclient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 10 with CloseableHttpClient

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

the class CxfRsConsumerTest method testPutConsumer.

@Test
public void testPutConsumer() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + CXT + "/rest/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.addHeader("test", "header1;header2");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) 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