Package com.jcabi.http.response
Class RestResponse
- java.lang.Object
-
- com.jcabi.http.response.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 classRequest
with all cookies transferred from the current one.The class is immutable and thread-safe.
- Since:
- 0.8
-
-
Constructor Summary
Constructors Constructor Description RestResponse(Response resp)
Public ctor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Response>
Tas(Class<T> type)
Convert it to another type, by encapsulation.RestResponse
assertBinary(org.hamcrest.Matcher<byte[]> matcher)
Verifies HTTP response body content against provided matcher, and throwsAssertionError
in case of mismatch.RestResponse
assertBody(org.hamcrest.Matcher<String> matcher)
Verifies HTTP response body content against provided matcher, and throwsAssertionError
in case of mismatch.RestResponse
assertHeader(String name, String value)
Verifies HTTP header against provided matcher, and throwsAssertionError
in case of mismatch.RestResponse
assertHeader(String name, org.hamcrest.Matcher<? super Iterable<String>> matcher)
Verifies HTTP header against provided matcher, and throwsAssertionError
in case of mismatch.RestResponse
assertStatus(int status)
Verifies HTTP response status code against the provided absolute value, and throwsAssertionError
in case of mismatch.RestResponse
assertStatus(org.hamcrest.Matcher<Integer> matcher)
Verifies HTTP response status code against the provided matcher, and throwsAssertionError
in case of mismatch.RestResponse
assertThat(org.hamcrest.Matcher<Response> matcher)
Assert using custom matcher.Request
back()
Get back to the request it's related to.byte[]
binary()
Raw body as a an array of bytes.String
body()
Get body as a string, assuming it'sUTF-8
(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).jakarta.ws.rs.core.Cookie
cookie(String name)
Get one cookie by name.Request
follow()
Follow LOCATION header.Map<String,List<String>>
headers()
Get a collection of all headers.Request
jump(URI uri)
Jump to a new location.String
reason()
Get status line reason phrase.int
status()
Get status of the response as a positive integer number.String
toString()
-
-
-
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 throwsAssertionError
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 throwsAssertionError
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 throwsAssertionError
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 throwsAssertionError
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 throwsAssertionError
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 matchmatcher
- The matcher to use- Returns:
- This object
-
assertHeader
public RestResponse assertHeader(String name, String value)
Verifies HTTP header against provided matcher, and throwsAssertionError
in case of mismatch.- Parameters:
name
- Name of the header to matchvalue
- 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.
-
status
public final int status()
Description copied from interface:Response
Get status of the response as a positive integer number.
-
reason
public final String reason()
Description copied from interface:Response
Get status line reason phrase.
-
headers
public final Map<String,List<String>> headers()
Description copied from interface:Response
Get a collection of all headers.
-
body
public String body()
Description copied from interface:Response
Get body as a string, assuming it'sUTF-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, useResponse.binary()
instead.
-
binary
public final byte[] binary()
Description copied from interface:Response
Raw body as a an array of bytes.
-
-