@Immutable public final class RestResponse extends Object
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.
Constructor and Description |
---|
RestResponse(Response resp)
Public ctor.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
as(Class<T> type)
Convert it to another type, by encapsulation.
|
RestResponse |
assertBody(org.hamcrest.Matcher<String> matcher)
Verifies HTTP response body content against provided matcher,
and throws
AssertionError in case of mismatch. |
RestResponse |
assertHeader(String name,
org.hamcrest.Matcher<Iterable<String>> matcher)
Verifies HTTP header against provided matcher, and throws
AssertionError in case of mismatch. |
RestResponse |
assertHeader(String name,
String value)
Verifies HTTP header against provided matcher, and throws
AssertionError in case of mismatch. |
RestResponse |
assertStatus(int status)
Verifies HTTP response status code against the provided absolute value,
and throws
AssertionError in case of mismatch. |
RestResponse |
assertStatus(org.hamcrest.Matcher<Integer> matcher)
Verifies HTTP response status code against the provided matcher,
and throws
AssertionError 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's
UTF-8 (if there is something
else that can't be translated into a UTF-8 string a runtime exception
will be thrown). |
javax.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() |
public RestResponse(@NotNull(message="response can\'t be NULL") Response resp)
resp
- Response@NotNull(message="new response is never NULL") public RestResponse assertThat(@NotNull(message="matcher can\'t be NULL") org.hamcrest.Matcher<Response> matcher)
matcher
- The matcher to use@NotNull(message="response is never NULL") public RestResponse assertStatus(int status)
AssertionError
in case of mismatch.status
- Expected status code@NotNull(message="REST response is never NULL") public RestResponse assertStatus(@NotNull(message="status matcher can\'t be NULL") org.hamcrest.Matcher<Integer> matcher)
AssertionError
in case of mismatch.matcher
- Matcher to validate status code@NotNull(message="REST response is never NULL") public RestResponse assertBody(@NotNull(message="body matcher can\'t be NULL") org.hamcrest.Matcher<String> matcher)
AssertionError
in case of mismatch.matcher
- The matcher to use@NotNull(message="response is never NULL") public RestResponse assertHeader(@NotNull(message="header name can\'t be NULL") String name, @NotNull(message="header matcher can\'t be NULL") org.hamcrest.Matcher<Iterable<String>> matcher)
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.
name
- Name of the header to matchmatcher
- The matcher to use@NotNull(message="response is never NULL") public RestResponse assertHeader(@NotNull(message="header name can\'t be NULL") String name, @NotNull(message="header value can\'t be NULL") String value)
AssertionError
in case of mismatch.name
- Name of the header to matchvalue
- The value to expect in one of the headers@NotNull(message="request is never NULL") public Request jump(@NotNull(message="URI can\'t be NULL") URI uri)
uri
- Destination to jump to@NotNull(message="request is never NULL") public Request follow()
@NotNull(message="cookie is never NULL") public javax.ws.rs.core.Cookie cookie(@NotNull String name)
name
- Cookie namepublic final Request back()
Response
public final int status()
Response
public final String reason()
Response
public final Map<String,List<String>> headers()
Response
public String body()
Response
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.
public final byte[] binary()
Response
Copyright © 2012–2014 jcabi.com. All rights reserved.