Package org.apache.http.impl.client

Examples of org.apache.http.impl.client.CloseableHttpClient


   
    @Test
    public void testGetSubResource() throws Exception {
        HttpGet get = new HttpGet("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/orders/223/products/323");
        get.addHeader("Accept" , "application/json");
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();

        try {
            HttpResponse response = httpclient.execute(get);
            assertEquals(200, response.getStatusLine().getStatusCode());
            assertEquals("{\"Product\":{\"description\":\"product 323\",\"id\":323}}",
                         EntityUtils.toString(response.getEntity()));
        } finally {
            httpclient.close();
        }
    }
View Full Code Here


    public void testPutConsumer() throws Exception {
        HttpPut put = new HttpPut("http://localhost:" + getPort() + "/CxfRsRouterTest/route/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();
        }
    }
View Full Code Here

        HttpPost post = new HttpPost("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers");
        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(200, 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/");
            httpclient.execute(del);
        } finally {
            httpclient.close();
        }

    }
View Full Code Here

        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/");
            httpclient.execute(del);
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

        Map<String, Object> endpointProps = testEndpoint.getProperties();
        assertEquals("Single endpoint property is expected", 1, endpointProps.size());
        assertEquals("Wrong property value", "aValue", endpointProps.get("aKey"));
       
        HttpGet get = new HttpGet(testEndpoint.getAddress());
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        try {
            HttpResponse response = httpclient.execute(get);
            assertEquals(404, response.getStatusLine().getStatusCode());
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

        HttpGet get = new HttpGet("http://localhost:" + portNum + "/users/homer");

        // do not follow redirects
        RequestConfig requestconfig = RequestConfig.custom().setRedirectsEnabled(false).build();
       
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestconfig).build();
       
        try {
           
            HttpResponse response = client.execute(get);
   
            for (Header header : response.getAllHeaders()) {
                log.info("Header {}", header);
            }
   
            assertEquals(302, response.getStatusLine().getStatusCode());
            assertTrue("Should have location header", response.containsHeader("Location"));
            assertEquals("http://somewhere.com", response.getFirstHeader("Location").getValue());
            assertEquals("bar", response.getFirstHeader("Foo").getValue());
        } finally {
            client.close();
        }
    }
View Full Code Here

    public static void initializePortNum() {
        portNum = AvailablePortFinder.getNextAvailable();
    }
   
    public HttpResponse doExecute(HttpUriRequest method) throws Exception {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        try {
            HttpResponse response = client.execute(method);
            response.setEntity(new BufferedHttpEntity(response.getEntity()));
            return response;
        } finally {
            client.close();
        }
    }
View Full Code Here

    @Test
    public void testGetCustomer() throws Exception {
        HttpGet get = new HttpGet("http://localhost:9000/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();
        }
    }
View Full Code Here

    @Test
    public void testGetCustomerWithQuery() throws Exception {
        HttpGet get = new HttpGet("http://localhost:9000/route/customerservice/customers?id=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();
        }
    }
View Full Code Here

    @Test
    public void testGetCustomer() throws Exception {
        HttpGet get = new HttpGet("http://localhost:9000/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());

            // should either by John or Mary depending on PUT test executed first
            String s = EntityUtils.toString(response.getEntity());
            boolean isJohn = "{\"Customer\":{\"id\":123,\"name\":\"John\"}}".equals(s);
            boolean isMary = "{\"Customer\":{\"id\":123,\"name\":\"Mary\"}}".equals(s);
            assertTrue("Should be John or Mary", isJohn || isMary);
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.http.impl.client.CloseableHttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.