Class HttpClientTestUtil


  • public class HttpClientTestUtil
    extends java.lang.Object
    Utility class aiding in testing HTTP functionality.
    Author:
    Daniel Sagenschneider
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String PROPERTY_NO_TIMEOUT
      System property configured to true to not time out HttpClient instances.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.http.client.CredentialsProvider configureCredentials​(org.apache.http.impl.client.HttpClientBuilder builder, java.lang.String realm, java.lang.String scheme, java.lang.String username, java.lang.String password)
      Configures CredentialsProvider for the HttpClientBuilder.
      static void configureHttps​(org.apache.http.impl.client.HttpClientBuilder builder)
      Configures the HttpClientBuilder for HTTPS.
      static void configureNoRedirects​(org.apache.http.impl.client.HttpClientBuilder builder)
      Configures no redirects for the HttpClientBuilder.
      static org.apache.http.impl.client.CloseableHttpClient createHttpClient()
      Creates a CloseableHttpClient ready for use with default values.
      static org.apache.http.impl.client.CloseableHttpClient createHttpClient​(boolean isSecure)
      Creates a CloseableHttpClient ready for use.
      static org.apache.http.impl.client.HttpClientBuilder createHttpClientBuilder()
      Create the HttpClientBuilder.
      static org.apache.http.impl.client.HttpClientBuilder createHttpClientBuilder​(int timeout)
      Create the HttpClientBuilder.
      static java.lang.String entityToString​(org.apache.http.HttpResponse response)
      Obtains the HttpEntity content.
      static java.lang.String entityToString​(org.apache.http.HttpResponse response, java.nio.charset.Charset charset)
      Obtains the HttpEntity content.
      static int getClientTimeout()
      Obtains the client time out.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PROPERTY_NO_TIMEOUT

        public static final java.lang.String PROPERTY_NO_TIMEOUT
        System property configured to true to not time out HttpClient instances. This is useful for debugging server handling of requests.
        See Also:
        Constant Field Values
    • Method Detail

      • getClientTimeout

        public static int getClientTimeout()
        Obtains the client time out.
        Returns:
        Client time out.
      • entityToString

        public static java.lang.String entityToString​(org.apache.http.HttpResponse response)
                                               throws java.io.IOException
        Obtains the HttpEntity content.
        Parameters:
        response - HttpResponse.
        Returns:
        Content of HttpEntity.
        Throws:
        java.io.IOException - If fails to obtain content.
      • entityToString

        public static java.lang.String entityToString​(org.apache.http.HttpResponse response,
                                                      java.nio.charset.Charset charset)
                                               throws java.io.IOException
        Obtains the HttpEntity content.
        Parameters:
        response - HttpResponse.
        charset - Charset.
        Returns:
        Content of HttpEntity.
        Throws:
        java.io.IOException - If fails to obtain content.
      • createHttpClientBuilder

        public static org.apache.http.impl.client.HttpClientBuilder createHttpClientBuilder()
        Create the HttpClientBuilder.
        Returns:
        HttpClientBuilder.
      • createHttpClientBuilder

        public static org.apache.http.impl.client.HttpClientBuilder createHttpClientBuilder​(int timeout)
        Create the HttpClientBuilder.
        Returns:
        HttpClientBuilder.
      • createHttpClient

        public static org.apache.http.impl.client.CloseableHttpClient createHttpClient()
        Creates a CloseableHttpClient ready for use with default values.
        Returns:
        CloseableHttpClient.
      • createHttpClient

        public static org.apache.http.impl.client.CloseableHttpClient createHttpClient​(boolean isSecure)
        Creates a CloseableHttpClient ready for use.
        Parameters:
        isSecure - Indicate if require secure connection.
        Returns:
        CloseableHttpClient.
      • configureHttps

        public static void configureHttps​(org.apache.http.impl.client.HttpClientBuilder builder)
        Configures the HttpClientBuilder for HTTPS.
        Parameters:
        builder - HttpClientBuilder.
      • configureNoRedirects

        public static void configureNoRedirects​(org.apache.http.impl.client.HttpClientBuilder builder)
        Configures no redirects for the HttpClientBuilder.
        Parameters:
        builder - HttpClientBuilder.
      • configureCredentials

        public static org.apache.http.client.CredentialsProvider configureCredentials​(org.apache.http.impl.client.HttpClientBuilder builder,
                                                                                      java.lang.String realm,
                                                                                      java.lang.String scheme,
                                                                                      java.lang.String username,
                                                                                      java.lang.String password)
        Configures CredentialsProvider for the HttpClientBuilder.
        Parameters:
        builder - HttpClientBuilder.
        realm - Security realm.
        scheme - Security scheme.
        username - User name.
        password - Password.
        Returns:
        CredentialsProvider.