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.ServerTest.java

@Test
public void testDynamicRequestTimeoutInvocation() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost req = new HttpPost(uri("/timeout-not"));
        req.setEntity(new StringEntity("Hello, world!", StandardCharsets.UTF_8));

        try (CloseableHttpResponse res = hc.execute(req)) {
            assertThat(HttpStatusClass.valueOf(res.getStatusLine().getStatusCode()),
                    is(HttpStatusClass.SUCCESS));
        }/*from   ww w  .j a  va 2s . c  o m*/
    }
}

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

@Test
public void getWithQueryString() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc
                .execute(new HttpGet(server().uri("/jsp/query_string.jsp?foo=%31&bar=%32")))) {

            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  .  ja  v a  2  s.  co  m
                    .isEqualTo("<html><body>" + "<p>foo is 1</p>" + "<p>bar is 2</p>" + "</body></html>");
        }

        // Send a query again with different values to make sure the query strings are not cached.
        try (CloseableHttpResponse res = hc
                .execute(new HttpGet(server().uri("/jsp/query_string.jsp?foo=%33&bar=%34")))) {

            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");
            assertThat(actualContent)
                    .isEqualTo("<html><body>" + "<p>foo is 3</p>" + "<p>bar is 4</p>" + "</body></html>");
        }
    }
}

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

@Test
public void testOAuth1a() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        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("/oauth1a", OAuth1aToken.of(passToken)))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }//from w w w.  ja va  2s.c o  m
        Map<String, String> failToken = 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("/oauth1a", OAuth1aToken.of(failToken)))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }
    }
}

From source file:com.linecorp.armeria.test.webapp.WebAppContainerTest.java

@Test
public void testAddressesAndPorts_127001() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(uri("/jsp/addrs_and_ports.jsp")))) {
            assertThat(res.getStatusLine().toString(), is("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("");

            assertTrue(actualContent,//from   ww w. j a v  a2s  .  c  om
                    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().defaultHostname()
                            + "</p>" + "<p>LocalPort: " + server().activePort().get().localAddress().getPort()
                            + "</p>" + "<p>ServerName: 127\\.0\\.0\\.1</p>" + "<p>ServerPort: "
                            + server().activePort().get().localAddress().getPort() + "</p>"
                            + "</body></html>"));
        }
    }
}

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

@Test
public void testGetPreCompressedSupportsBrotli() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpGet request = new HttpGet(newUri("/compressed/foo.txt"));
        request.setHeader("Accept-Encoding", "br");
        try (CloseableHttpResponse res = hc.execute(request)) {
            assertThat(res.getFirstHeader("Content-Encoding").getValue(), is("br"));
            assertThat(res.getFirstHeader("Content-Type").getValue(), is("text/plain; charset=utf-8"));
            // Test would be more readable and fun by decompressing like the gzip one, but since JDK doesn't
            // support brotli yet, just compare the compressed content to avoid adding a complex dependency.
            final byte[] content = ByteStreams.toByteArray(res.getEntity().getContent());
            assertThat(content,//from  w  w w.j  a  v  a2 s  .com
                    is(Resources.toByteArray(Resources.getResource(baseResourceDir + "foo/foo.txt.br"))));
        }
    }
}

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

@Test
public void postWithQueryString() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost post = new HttpPost(server().uri("/jsp/query_string.jsp?foo=3"));
        post.setEntity(new UrlEncodedFormEntity(Collections.singletonList(new BasicNameValuePair("bar", "4")),
                StandardCharsets.UTF_8));

        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   www  . j ava2  s.c  o m
                    .isEqualTo("<html><body>" + "<p>foo is 3</p>" + "<p>bar is 4</p>" + "</body></html>");
        }

        // Send a query again with different values to make sure the query strings are not cached.
        final HttpPost post2 = new HttpPost(server().uri("/jsp/query_string.jsp?foo=5"));
        post2.setEntity(new UrlEncodedFormEntity(Collections.singletonList(new BasicNameValuePair("bar", "6")),
                StandardCharsets.UTF_8));

        try (CloseableHttpResponse res = hc.execute(post2)) {
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");
            assertThat(actualContent)
                    .isEqualTo("<html><body>" + "<p>foo is 5</p>" + "<p>bar is 6</p>" + "</body></html>");
        }
    }
}

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

@Test
public void testOAuth2() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc
                .execute(oauth2GetRequest("/oauth2", OAuth2Token.of("dummy_oauth2_token")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }/*from   w  w  w .j a v  a  2 s .com*/
        try (CloseableHttpResponse res = hc
                .execute(oauth2GetRequest("/oauth2", OAuth2Token.of("DUMMY_oauth2_token")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }
    }
}

From source file:com.linecorp.armeria.test.webapp.WebAppContainerTest.java

@Test
public void testAddressesAndPorts_localhost() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpGet request = new HttpGet(uri("/jsp/addrs_and_ports.jsp"));
        request.setHeader("Host", "localhost:1111");
        try (CloseableHttpResponse res = hc.execute(request)) {
            assertThat(res.getStatusLine().toString(), is("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("");

            assertTrue(actualContent,// w w  w .j a  v a  2 s  .c  o  m
                    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().defaultHostname()
                            + "</p>" + "<p>LocalPort: " + server().activePort().get().localAddress().getPort()
                            + "</p>" + "<p>ServerName: localhost</p>" + "<p>ServerPort: 1111</p>"
                            + "</body></html>"));
        }
    }
}

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

@Test
public void testGetPreCompressedSupportsBothPrefersBrotli() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpGet request = new HttpGet(newUri("/compressed/foo.txt"));
        request.setHeader("Accept-Encoding", "gzip, br");
        try (CloseableHttpResponse res = hc.execute(request)) {
            assertThat(res.getFirstHeader("Content-Encoding").getValue(), is("br"));
            assertThat(res.getFirstHeader("Content-Type").getValue(), is("text/plain; charset=utf-8"));
            // Test would be more readable and fun by decompressing like the gzip one, but since JDK doesn't
            // support brotli yet, just compare the compressed content to avoid adding a complex dependency.
            final byte[] content = ByteStreams.toByteArray(res.getEntity().getContent());
            assertThat(content,/*w  w  w.  j ava  2 s .  com*/
                    is(Resources.toByteArray(Resources.getResource(baseResourceDir + "foo/foo.txt.br"))));
        }
    }
}

From source file:com.linecorp.armeria.server.http.auth.AuthServiceTest.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"));
        }/*  w  w  w . j a v  a  2s . c o  m*/
        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(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"));
        }
    }
}