Class HttpClientMock

  • All Implemented Interfaces:
    Closeable, AutoCloseable, org.apache.hc.client5.http.classic.HttpClient, org.apache.hc.core5.io.ModalCloseable

    public class HttpClientMock
    extends org.apache.hc.client5.http.impl.classic.CloseableHttpClient
    • Constructor Detail

      • HttpClientMock

        public HttpClientMock()
        Creates mock of Apache HttpClient
      • HttpClientMock

        public HttpClientMock​(String defaultHost)
        Creates mock of Apache HttpClient with default host. All defined conditions without host will use default host
        Parameters:
        defaultHost - default host for later conditions
    • Method Detail

      • reset

        public void reset()
        Resets mock to initial state where there are no rules and no previous requests.
      • verify

        public HttpClientVerify verify()
        Creates verification builder.
        Returns:
        request number verification builder
      • onPost

        public HttpClientMockBuilder onPost()
        Starts defining new rule which requires HTTP POST method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onGet

        public HttpClientMockBuilder onGet()
        Starts defining new rule which requires HTTP GET method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onDelete

        public HttpClientMockBuilder onDelete()
        Starts defining new rule which requires HTTP DELETE method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onHead

        public HttpClientMockBuilder onHead()
        Starts defining new rule which requires HTTP HEAD method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onOption

        @Deprecated
        public HttpClientMockBuilder onOption()
        Deprecated.
        Method name contains misspelling, use onOptions()
        Starts defining new rule which requires HTTP OPTIONS method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onOptions

        public HttpClientMockBuilder onOptions()
        Starts defining new rule which requires HTTP OPTIONS method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onPut

        public HttpClientMockBuilder onPut()
        Starts defining new rule which requires HTTP PUT method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onPatch

        public HttpClientMockBuilder onPatch()
        Starts defining new rule which requires HTTP PATCH method.
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onGet

        public HttpClientMockBuilder onGet​(String url)
        Starts defining new rule which requires HTTP GET method and url. If provided url starts with "/" request url must be equal to concatenation of default host and url. Otherwise request url must equal to provided url. If provided url contains query parameters they are parsed and added as a separate conditions.

        For example:
        httpClientMock.onGet("http://localhost/login?user=Ben");
        is equal to
        httpClientMock.onGet("http://localhost/login").withParameter("user","Ben");

        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onPost

        public HttpClientMockBuilder onPost​(String url)
        Starts defining new rule which requires HTTP POST method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onPut

        public HttpClientMockBuilder onPut​(String url)
        Starts defining new rule which requires HTTP PUT method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onDelete

        public HttpClientMockBuilder onDelete​(String url)
        Starts defining new rule which requires HTTP DELETE method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onHead

        public HttpClientMockBuilder onHead​(String url)
        Starts defining new rule which requires HTTP HEAD method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onOptions

        public HttpClientMockBuilder onOptions​(String url)
        Starts defining new rule which requires HTTP OPTIONS method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • onPatch

        public HttpClientMockBuilder onPatch​(String url)
        Starts defining new rule which requires HTTP PATCH method and url. URL works the same way as in onGet
        Parameters:
        url - required url
        Returns:
        HttpClientMockBuilder which allows to define new rule
      • doExecute

        protected org.apache.hc.client5.http.impl.classic.CloseableHttpResponse doExecute​(org.apache.hc.core5.http.HttpHost httpHost,
                                                                                          org.apache.hc.core5.http.ClassicHttpRequest httpRequest,
                                                                                          org.apache.hc.core5.http.protocol.HttpContext httpContext)
                                                                                   throws IOException
        Specified by:
        doExecute in class org.apache.hc.client5.http.impl.classic.CloseableHttpClient
        Throws:
        IOException
      • debugOn

        public void debugOn()
      • debugOff

        public void debugOff()
      • addRequestInterceptor

        public void addRequestInterceptor​(org.apache.hc.core5.http.HttpRequestInterceptor requestInterceptor)
      • addResponseInterceptor

        public void addResponseInterceptor​(org.apache.hc.core5.http.HttpResponseInterceptor responseInterceptor)
      • close

        public void close​(org.apache.hc.core5.io.CloseMode closeMode)
      • close

        public void close()