Example usage for org.apache.http.impl.client HttpClients createMinimal

List of usage examples for org.apache.http.impl.client HttpClients createMinimal

Introduction

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

Prototype

public static CloseableHttpClient createMinimal() 

Source Link

Document

Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.

Usage

From source file:com.linecorp.armeria.server.http.auth.HttpAuthServiceTest.java

@Test
public void testCompositeAuth() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(getRequest("/composite", "unit test"))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }/*from   w ww  .j  av a  2 s .com*/
        try (CloseableHttpResponse res = hc
                .execute(basicGetRequest("/composite", BasicToken.of("brown", "cony")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }
        Map<String, String> passToken = ImmutableMap.<String, String>builder().put("realm", "dummy_realm")
                .put("oauth_consumer_key", "dummy_consumer_key").put("oauth_token", "dummy_oauth1a_token")
                .put("oauth_signature_method", "dummy").put("oauth_signature", "dummy_signature")
                .put("oauth_timestamp", "0").put("oauth_nonce", "dummy_nonce").put("version", "1.0").build();
        try (CloseableHttpResponse res = hc
                .execute(oauth1aGetRequest("/composite", OAuth1aToken.of(passToken)))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }
        try (CloseableHttpResponse res = hc
                .execute(oauth2GetRequest("/composite", OAuth2Token.of("dummy_oauth2_token")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(server.uri("/composite")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }
        try (CloseableHttpResponse res = hc
                .execute(basicGetRequest("/composite", BasicToken.of("choco", "pangyo")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }
    }
}

From source file:com.linecorp.armeria.testing.server.webapp.WebAppContainerTest.java

@Test
public void echoPost() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost post = new HttpPost(server().uri("/jsp/echo_post.jsp"));
        post.setEntity(new StringEntity("test"));

        try (CloseableHttpResponse res = hc.execute(post)) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");
            assertThat(actualContent)/*  w w w.  j a  va2s.co m*/
                    .isEqualTo("<html><body>" + "<p>Check request body</p>" + "<p>test</p>" + "</body></html>");
        }
    }
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

@Test
public void testFileSystemGet() throws Exception {
    final File barFile = new File(tmpDir, "bar.html");
    final String expectedContentA = "<html/>";
    final String expectedContentB = "<html><body/></html>";
    Files.write(barFile.toPath(), expectedContentA.getBytes(StandardCharsets.UTF_8));

    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final String lastModified;
        HttpUriRequest req = new HttpGet(newUri("/fs/bar.html"));
        try (CloseableHttpResponse res = hc.execute(req)) {
            lastModified = assert200Ok(res, "text/html", expectedContentA);
        }/* w  w w . ja va  2  s . co  m*/

        // Test if the 'If-Modified-Since' header works as expected.
        req = new HttpGet(newUri("/fs/bar.html"));
        req.setHeader(HttpHeaders.IF_MODIFIED_SINCE, currentHttpDate());

        try (CloseableHttpResponse res = hc.execute(req)) {
            assert304NotModified(res, lastModified);
        }

        // Test if the 'If-Modified-Since' header works as expected after the file is modified.
        req = new HttpGet(newUri("/fs/bar.html"));
        req.setHeader(HttpHeaders.IF_MODIFIED_SINCE, currentHttpDate());

        // HTTP-date has no sub-second precision; wait until the current second changes.
        Thread.sleep(1000);

        Files.write(barFile.toPath(), expectedContentB.getBytes(StandardCharsets.UTF_8));

        try (CloseableHttpResponse res = hc.execute(req)) {
            final String newLastModified = assert200Ok(res, "text/html", expectedContentB);

            // Ensure that the 'Last-Modified' header did not change.
            assertThat(newLastModified, is(not(lastModified)));
        }

        // Test if the cache detects the file removal correctly.
        final boolean deleted = barFile.delete();
        assertThat(deleted, is(true));

        req = new HttpGet(newUri("/fs/bar.html"));
        req.setHeader(HttpHeaders.IF_MODIFIED_SINCE, currentHttpDate());
        req.setHeader(HttpHeaders.CONNECTION, "close");

        try (CloseableHttpResponse res = hc.execute(req)) {
            assert404NotFound(res);
        }
    }
}

From source file:com.linecorp.armeria.server.http.HttpServiceTest.java

@Test
public void testHello() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/hello/foo")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Hello, foo!"));
        }//from   w  w  w .  j a v  a 2s.c  o  m

        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/hello/foo/bar")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }

        try (CloseableHttpResponse res = hc.execute(new HttpDelete(newUri("/hello/bar")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 405 Method Not Allowed"));
            assertThat(EntityUtils.toString(res.getEntity()), is("405 Method Not Allowed"));
        }
    }
}

From source file:com.linecorp.armeria.testing.server.webapp.WebAppContainerTest.java

@Test
public void echoPostWithEmptyBody() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost post = new HttpPost(server().uri("/jsp/echo_post.jsp"));

        try (CloseableHttpResponse res = hc.execute(post)) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");
            assertThat(actualContent)//from  w  ww . j  a v  a2 s .  c om
                    .isEqualTo("<html><body>" + "<p>Check request body</p>" + "<p></p>" + "</body></html>");
        }
    }
}

From source file:com.spotify.helios.system.SystemTestBase.java

@Before
public void baseSetup() throws Exception {
    System.setProperty("user.name", TEST_USER);
    masterPort = temporaryPorts.localPort("helios master");
    masterAdminPort = temporaryPorts.localPort("helios master admin");

    String className = getClass().getName();
    if (className.endsWith("ITCase")) {
        masterEndpoint = checkNotNull(System.getenv("HELIOS_ENDPOINT"),
                "For integration tests, HELIOS_ENDPOINT *must* be set");
        integrationMode = true;/* ww w.ja  v a2 s . co m*/
    } else if (className.endsWith("Test")) {
        integrationMode = false;
        masterEndpoint = "http://localhost:" + masterPort();
        masterAdminEndpoint = "http://localhost:" + masterAdminPort();
        // unit test
    } else {
        throw new RuntimeException("Test class' name must end in either 'Test' or 'ITCase'.");
    }

    zk = zooKeeperTestManager();
    listThreads();
    zk.ensure("/config");
    zk.ensure("/status");
    agentStateDirs = temporaryFolder.newFolder("helios-agents").toPath();
    masterStateDirs = temporaryFolder.newFolder("helios-masters").toPath();

    // TODO (mbrown): not 100% sure what a minimal client is but it sounds good
    httpClient = HttpClients.createMinimal();
}

From source file:org.apache.brooklyn.rest.CorsFilterLauncherTest.java

protected HttpClient client() {
    return HttpClients.createMinimal();
}

From source file:com.linecorp.armeria.server.http.HttpServiceTest.java

@Test
public void testDynamicHttpServices() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        // Run case 1.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic1/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 42"));
        }//from   ww w  .jav a  2  s  .  c o m
        // Run asynchronous case 1.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic1/int-async/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 43"));
        }
        // Run case 2.
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic1/long/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number: 42"));
        }
        // Run case 3.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic1/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[blah]"));
        }
        // Run case 1 but illegal parameter.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/int/fourty-two")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        // Run case 2 but without parameter (non-existing url).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic1/long/")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 3 but with not-mapped HTTP method (Post).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic1/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 4.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 42"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/int-async/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 43"));
        }
        // Run case 5.
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic2/long/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 6.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String: blah"));
        }
        // Run case 4 but illegal parameter.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/int/fourty-two")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        // Run case 5 but without parameter (non-existing url).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic2/long/")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 6 but with not-mapped HTTP method (Post).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic2/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Exceptions in business logic
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/exception/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic2/exception-async/1")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }

        // Run case 7.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic3/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 8.
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic3/long/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 9.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic3/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String: blah"));
        }
        // Run case 7 but illegal parameter.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/dynamic3/int/fourty-two")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        // Run case 8 but without parameter (non-existing url).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic3/long/")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 9 but with not-mapped HTTP method (Post).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(newUri("/dynamic3/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
    }
}

From source file:com.linecorp.armeria.server.http.auth.HttpAuthServiceTest.java

@Test
public void testAuthorizerException() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(server.uri("/authorizer_exception")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }/*  w  w w. j  ava  2s. c  o  m*/
    }
}

From source file:com.linecorp.armeria.testing.server.webapp.WebAppContainerTest.java

@Test
public void addressesAndPorts_127001() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(server().uri("/jsp/addrs_and_ports.jsp")))) {

            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");

            assertThat(actualContent).matches(
                    "<html><body>" + "<p>RemoteAddr: 127\\.0\\.0\\.1</p>" + "<p>RemoteHost: 127\\.0\\.0\\.1</p>"
                            + "<p>RemotePort: [1-9][0-9]+</p>" + "<p>LocalAddr: (?!null)[^<]+</p>"
                            + "<p>LocalName: " + server().server().defaultHostname() + "</p>" + "<p>LocalPort: "
                            + server().httpPort() + "</p>" + "<p>ServerName: 127\\.0\\.0\\.1</p>"
                            + "<p>ServerPort: " + server().httpPort() + "</p>" + "</body></html>");
        }/*from w  w w . j  av a 2s  .c o m*/
    }
}