Example usage for org.apache.http.impl.client CloseableHttpClient CloseableHttpClient

List of usage examples for org.apache.http.impl.client CloseableHttpClient CloseableHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient CloseableHttpClient.

Prototype

CloseableHttpClient

Source Link

Usage

From source file:org.apache.metamodel.neo4j.Neo4jRequestWrapperTest.java

@Test
public void testCreateCypherQueryWithAuthentication() {
    if (!isConfigured()) {
        System.err.println(getInvalidConfigurationMessage());
        return;/*from w  w w. j  a va2 s .c o  m*/
    }

    CloseableHttpClient mockHttpClient = new CloseableHttpClient() {

        @Override
        public void close() throws IOException {
            // Do nothing
        }

        @Override
        public HttpParams getParams() {
            // Do nothing
            return null;
        }

        @Override
        public ClientConnectionManager getConnectionManager() {
            // Do nothing
            return null;
        }

        @Override
        protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context)
                throws IOException, ClientProtocolException {
            assertTrue(request instanceof HttpPost);
            HttpPost httpPost = (HttpPost) request;

            Header[] headers = httpPost.getHeaders("Authorization");
            assertNotNull(headers);
            assertEquals(1, headers.length);
            String base64Encoded = headers[0].getValue();
            base64Encoded = base64Encoded.replace("Basic ", "");
            String decoded = new String(BaseEncoding.base64().decode(base64Encoded), StandardCharsets.UTF_8);
            assertEquals("testUsername:testPassword", decoded);

            assertEquals("{\"statements\":[{\"statement\":\"MATCH (n) RETURN n;\"}]}",
                    EntityUtils.toString(httpPost.getEntity()));

            CloseableHttpResponse mockResponse = new MockClosableHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
            return mockResponse;
        }
    };

    Neo4jRequestWrapper wrapper = new Neo4jRequestWrapper(mockHttpClient,
            new HttpHost(getHostname(), getPort()), "testUsername", "testPassword", getServiceRoot());
    wrapper.executeCypherQuery("MATCH (n) RETURN n;");
    // Assertions are in the HttpClient
}

From source file:org.apache.metamodel.neo4j.Neo4jRequestWrapperTest.java

@Test
public void testCreateCypherQueryWithoutAuthentication() {
    if (!isConfigured()) {
        System.err.println(getInvalidConfigurationMessage());
        return;//  w w w.  j  a  v a  2s.c om
    }

    CloseableHttpClient mockHttpClient = new CloseableHttpClient() {

        @Override
        public void close() throws IOException {
            // Do nothing
        }

        @Override
        public HttpParams getParams() {
            // Do nothing
            return null;
        }

        @Override
        public ClientConnectionManager getConnectionManager() {
            // Do nothing
            return null;
        }

        @Override
        protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context)
                throws IOException, ClientProtocolException {
            assertTrue(request instanceof HttpPost);
            HttpPost httpPost = (HttpPost) request;

            Header[] headers = httpPost.getHeaders("Authorization");
            assertNotNull(headers);
            assertEquals(0, headers.length);

            assertEquals("{\"statements\":[{\"statement\":\"MATCH (n) RETURN n;\"}]}",
                    EntityUtils.toString(httpPost.getEntity()));

            CloseableHttpResponse mockResponse = new MockClosableHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
            return mockResponse;
        }
    };

    Neo4jRequestWrapper wrapper = new Neo4jRequestWrapper(mockHttpClient,
            new HttpHost(getHostname(), getPort()), getServiceRoot());
    wrapper.executeCypherQuery("MATCH (n) RETURN n;");
    // Assertions are in the HttpClient
}