Package com.jcabi.http.response
Class RestResponse
java.lang.Object
com.jcabi.http.response.RestResponse
- All Implemented Interfaces:
Response
REST response.
This response decorator is able to make basic assertions on HTTP response and manipulate with it afterwards, 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends Response>
TConvert it to another type, by encapsulation.assertBinary(org.hamcrest.Matcher<byte[]> matcher) Verifies HTTP response body content against provided matcher, and throwsAssertionErrorin case of mismatch.assertBody(org.hamcrest.Matcher<String> matcher) Verifies HTTP response body content against provided matcher, and throwsAssertionErrorin case of mismatch.assertHeader(String name, String value) Verifies HTTP header against provided matcher, and throwsAssertionErrorin case of mismatch.assertHeader(String name, org.hamcrest.Matcher<? super Iterable<String>> matcher) Verifies HTTP header against provided matcher, and throwsAssertionErrorin case of mismatch.assertStatus(int status) Verifies HTTP response status code against the provided absolute value, and throwsAssertionErrorin case of mismatch.assertStatus(org.hamcrest.Matcher<Integer> matcher) Verifies HTTP response status code against the provided matcher, and throwsAssertionErrorin case of mismatch.assertThat(org.hamcrest.Matcher<Response> matcher) Assert using custom matcher.final Requestback()Get back to the request it's related to.final byte[]binary()Raw body as an array of bytes.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.CookieGet one cookie by name.follow()Follow LOCATION header.headers()Get a collection of all headers.Jump to a new location.final Stringreason()Get status line reason phrase.final intstatus()Get status of the response as a positive integer number.final StringtoString()
-
Constructor Details
-
RestResponse
Public ctor.- Parameters:
resp- Response
-
-
Method Details
-
assertThat
Assert using custom matcher.- Parameters:
matcher- The matcher to use- Returns:
- The same object
-
assertStatus
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
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
Verifies HTTP response body content against provided matcher, and throwsAssertionErrorin case of mismatch.- Parameters:
matcher- The matcher to use- Returns:
- This object
-
assertBinary
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
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
Jump to a new location.- Parameters:
uri- Destination to jump to- Returns:
- New request
-
follow
Follow LOCATION header.- Returns:
- New request
-
cookie
Get one cookie by name.- Parameters:
name- Cookie name- Returns:
- Cookie found
-
toString
-
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
Description copied from interface:ResponseGet status line reason phrase. -
headers
Description copied from interface:ResponseGet a collection of all headers. -
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 an array of bytes. -
as
Description copied from interface:ResponseConvert it to another type, by encapsulation.
-