Class RestResponse

  • All Implemented Interfaces:
    Response

    @Immutable
    public final class RestResponse
    extends Object
    REST response.

    This response decorator is able to make basic assertions on HTTP response and manipulate with it afterwords, for example:

     String name = new JdkRequest("http://my.example.com")
       .fetch()
       .as(RestResponse.class)
       .assertStatus(200)
       .assertBody(Matchers.containsString("hello, world!"))
       .assertHeader("Content-Type", Matchers.hasItem("text/plain"))
       .jump(URI.create("/users"))
       .fetch();

    Method jump(URI) creates a new instance of class Request with all cookies transferred from the current one.

    The class is immutable and thread-safe.

    Since:
    0.8
    • Constructor Detail

      • RestResponse

        public RestResponse​(Response resp)
        Public ctor.
        Parameters:
        resp - Response
    • Method Detail

      • assertThat

        public RestResponse assertThat​(org.hamcrest.Matcher<Response> matcher)
        Assert using custom matcher.
        Parameters:
        matcher - The matcher to use
        Returns:
        The same object
      • assertStatus

        public RestResponse assertStatus​(int status)
        Verifies HTTP response status code against the provided absolute value, and throws AssertionError in case of mismatch.
        Parameters:
        status - Expected status code
        Returns:
        The same object
      • assertStatus

        public RestResponse assertStatus​(org.hamcrest.Matcher<Integer> matcher)
        Verifies HTTP response status code against the provided matcher, and throws AssertionError in case of mismatch.
        Parameters:
        matcher - Matcher to validate status code
        Returns:
        This object
      • assertBody

        public RestResponse assertBody​(org.hamcrest.Matcher<String> matcher)
        Verifies HTTP response body content against provided matcher, and throws AssertionError in case of mismatch.
        Parameters:
        matcher - The matcher to use
        Returns:
        This object
      • assertBinary

        public RestResponse assertBinary​(org.hamcrest.Matcher<byte[]> matcher)
        Verifies HTTP response body content against provided matcher, and throws AssertionError in case of mismatch.
        Parameters:
        matcher - The matcher to use
        Returns:
        This object
      • assertHeader

        public RestResponse assertHeader​(String name,
                                         org.hamcrest.Matcher<? super Iterable<String>> matcher)
        Verifies HTTP header against provided matcher, and throws AssertionError in case of mismatch.

        The iterator for the matcher will always be a real object an never NULL, even if such a header is absent in the response. If the header is absent the iterable will be empty.

        Parameters:
        name - Name of the header to match
        matcher - The matcher to use
        Returns:
        This object
      • assertHeader

        public RestResponse assertHeader​(String name,
                                         String value)
        Verifies HTTP header against provided matcher, and throws AssertionError in case of mismatch.
        Parameters:
        name - Name of the header to match
        value - The value to expect in one of the headers
        Returns:
        This object
        Since:
        0.9
      • jump

        public Request jump​(URI uri)
        Jump to a new location.
        Parameters:
        uri - Destination to jump to
        Returns:
        New request
      • follow

        public Request follow()
        Follow LOCATION header.
        Returns:
        New request
      • cookie

        public jakarta.ws.rs.core.Cookie cookie​(String name)
        Get one cookie by name.
        Parameters:
        name - Cookie name
        Returns:
        Cookie found
      • back

        public final Request back()
        Description copied from interface: Response
        Get back to the request it's related to.
        Specified by:
        back in interface Response
        Returns:
        The request we're in
      • status

        public final int status()
        Description copied from interface: Response
        Get status of the response as a positive integer number.
        Specified by:
        status in interface Response
        Returns:
        The status code
      • reason

        public final String reason()
        Description copied from interface: Response
        Get status line reason phrase.
        Specified by:
        reason in interface Response
        Returns:
        The status line reason phrase
      • body

        public String body()
        Description copied from interface: Response
        Get body as a string, assuming it's UTF-8 (if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).

        DISCLAIMER: The only encoding supported here is UTF-8. If the body of response contains any chars that can't be used and should be replaced with a "replacement character", a RuntimeException will be thrown. If you need to use some other encodings, use Response.binary() instead.

        Specified by:
        body in interface Response
        Returns:
        The body, as a UTF-8 string
      • binary

        public final byte[] binary()
        Description copied from interface: Response
        Raw body as a an array of bytes.
        Specified by:
        binary in interface Response
        Returns:
        The body, as a UTF-8 string
      • as

        public final <T extends Response> T as​(Class<T> type)
        Description copied from interface: Response
        Convert it to another type, by encapsulation.
        Specified by:
        as in interface Response
        Type Parameters:
        T - Type to use
        Parameters:
        type - Type to use
        Returns:
        New response