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 classRequestwith 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.RestResponseassertBinary(org.hamcrest.Matcher<byte[]> matcher)Verifies HTTP response body content against provided matcher, and throwsAssertionErrorin case of mismatch.RestResponseassertBody(org.hamcrest.Matcher<String> matcher)Verifies HTTP response body content against provided matcher, and throwsAssertionErrorin case of mismatch.RestResponseassertHeader(String name, String value)Verifies HTTP header against provided matcher, and throwsAssertionErrorin case of mismatch.RestResponseassertHeader(String name, org.hamcrest.Matcher<? super Iterable<String>> matcher)Verifies HTTP header against provided matcher, and throwsAssertionErrorin case of mismatch.RestResponseassertStatus(int status)Verifies HTTP response status code against the provided absolute value, and throwsAssertionErrorin case of mismatch.RestResponseassertStatus(org.hamcrest.Matcher<Integer> matcher)Verifies HTTP response status code against the provided matcher, and throwsAssertionErrorin case of mismatch.RestResponseassertThat(org.hamcrest.Matcher<Response> matcher)Assert using custom matcher.Requestback()Get back to the request it's related to.byte[]binary()Raw body as a an array of bytes.Stringbody()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.Cookiecookie(String name)Get one cookie by name.Requestfollow()Follow LOCATION header.Map<String,List<String>>headers()Get a collection of all headers.Requestjump(URI uri)Jump to a new location.Stringreason()Get status line reason phrase.intstatus()Get status of the response as a positive integer number.StringtoString()
-
-
-
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 throwsAssertionErrorin 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 throwsAssertionErrorin 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 throwsAssertionErrorin 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 throwsAssertionErrorin 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 throwsAssertionErrorin 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 throwsAssertionErrorin 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:ResponseGet back to the request it's related to.
-
status
public final int status()
Description copied from interface:ResponseGet status of the response as a positive integer number.
-
reason
public final String reason()
Description copied from interface:ResponseGet status line reason phrase.
-
headers
public final Map<String,List<String>> headers()
Description copied from interface:ResponseGet a collection of all headers.
-
body
public String body()
Description copied from interface:ResponseGet 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
RuntimeExceptionwill be thrown. If you need to use some other encodings, useResponse.binary()instead.
-
binary
public final byte[] binary()
Description copied from interface:ResponseRaw body as a an array of bytes.
-
-