com.rexsl.test
Interface TestResponse

All Superinterfaces:
JsonDocument, XmlDocument

public interface TestResponse
extends XmlDocument, JsonDocument

Response returned by TestClient.

It is used as an output of TestClient, which is an output of RestTester, for example:

 TestResponse resp = RestTester.start(new URI("http://www.google.com"))
   .get("load from page of Google");
 if (resp.getStatus() == 200) {
   // everything is fine
 } else if (resp.getStatus() == 404) {
   // google.com not found, hm...
 }
 

TestResponse extends XmlDocument, which is a abstract of an XML document, which can be retrieved from itself. For example:

 TestResponse resp = RestTester.start(new URI("http://example.com"))
   .get("load XML document");
 Collection<XmlDocument> employees = resp.nodes("/Staff/Employee");
 for (XmlDocument employee : employees) {
   String name = employee.xpath("name/text()").get(0);
   // ...
 }
 

TestResponse also extends JsonDocument, which is an abstract of a Json document, which can be retrieved from itself.

In case of any problems inside (connection, time-outs, assertions, etc.) implementation has to throw AssertionError.

Implementation of this interface shall be mutable and thread-safe.

Version:
$Id: TestResponse.java 1758 2012-05-28 14:51:16Z guard $
Author:
Yegor Bugayenko (yegor@rexsl.com)

Field Summary
static int MAX_ATTEMPTS
          How many attempts to make when assertThat(AssertionPolicy) reports a problem.
 
Method Summary
 TestResponse assertBody(org.hamcrest.Matcher<String> matcher)
          Verifies HTTP response body content against provided matcher, and throws AssertionError in case of mismatch.
 TestResponse assertHeader(String name, org.hamcrest.Matcher<Iterable<String>> matcher)
          Verifies HTTP header against provided matcher, and throws AssertionError in case of mismatch.
 TestResponse assertJson(String element)
          Verifies the Json data against the element identifier argument, and throws AssertionError in case of mismatch.
 TestResponse assertStatus(int status)
          Verifies HTTP response status code against the provided absolute value, and throws AssertionError in case of mismatch.
 TestResponse assertStatus(org.hamcrest.Matcher<Integer> matcher)
          Verifies HTTP response status code against the provided matcher, and throws AssertionError in case of mismatch.
 TestResponse assertThat(AssertionPolicy assertion)
          Assert something (AssertionError will be thrown if assertion fails).
 TestResponse assertXPath(String xpath)
          Verifies HTTP response body XHTML/XML content against XPath query, and throws AssertionError in case of mismatch.
 Cookie cookie(String name)
          Get cookie.
 void fail(String reason)
          Fail and report a problem (throws AssertionError with the message provided).
 TestClient follow()
          Follow the LOCATION header.
 String getBody()
          Get body as a string, assuming it's UTF-8.
 MultivaluedMap<String,String> getHeaders()
          Get a collection of all headers.
 int getStatus()
          Get status of the response as a positive integer number.
 String getStatusLine()
          Get status line of the response.
 TestResponse registerNs(String prefix, Object uri)
          Register additional namespace prefix for XPath.
 TestClient rel(String query)
          Find link in XML and return new client with this link as URI.
 
Methods inherited from interface com.rexsl.test.XmlDocument
merge, node, nodes, xpath
 
Methods inherited from interface com.rexsl.test.JsonDocument
json, nodesJson
 

Field Detail

MAX_ATTEMPTS

static final int MAX_ATTEMPTS
How many attempts to make when assertThat(AssertionPolicy) reports a problem.

See Also:
Constant Field Values
Method Detail

follow

TestClient follow()
Follow the LOCATION header.

Returns:
New client

rel

TestClient rel(@NotNull
               String query)
Find link in XML and return new client with this link as URI.

Parameters:
query - The path of the link, as XPath query
Returns:
New client ready to fetch content from this new page

getStatus

int getStatus()
Get status of the response as a positive integer number.

Returns:
The status code

getStatusLine

String getStatusLine()
Get status line of the response.

Returns:
The status line

getHeaders

MultivaluedMap<String,String> getHeaders()
Get a collection of all headers.

Returns:
The headers

cookie

Cookie cookie(@NotNull
              String name)
Get cookie.

Parameters:
name - Name of the cookie to get
Returns:
The cookie

getBody

String getBody()
Get body as a string, assuming it's UTF-8.

Returns:
The body

registerNs

TestResponse registerNs(@NotNull
                        String prefix,
                        @NotNull
                        Object uri)
Register additional namespace prefix for XPath.

For example:

 String name = RestTester.start(new URI("http://www.example.com"))
   .registerNs("ns1", "http://example.com")
   .registerNs("foo", "http://example.com/foo")
   .xpath("/ns1:root/foo:name/text()")
   .get(0);
 

A number of standard namespaces are registered by default in instances of XmlDocument returned by RestTester. Their full list is in SimpleXml.SimpleXml(String).

If a namespace prefix is already registered an IllegalArgumentException will be thrown.

Specified by:
registerNs in interface XmlDocument
Parameters:
prefix - The XPath prefix to register
uri - Namespace URI
Returns:
A new XML document, with this additional namespace registered

fail

void fail(@NotNull
          String reason)
Fail and report a problem (throws AssertionError with the message provided).

Parameters:
reason - Reason of failure

assertThat

TestResponse assertThat(@NotNull
                        AssertionPolicy assertion)
Assert something (AssertionError will be thrown if assertion fails).

Parameters:
assertion - The assertion to use
Returns:
This object
Since:
0.3.4

assertStatus

TestResponse 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:
This object

assertStatus

TestResponse assertStatus(@NotNull
                          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

TestResponse assertBody(@NotNull
                        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

assertHeader

TestResponse assertHeader(@NotNull
                          String name,
                          @NotNull
                          org.hamcrest.Matcher<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

assertXPath

TestResponse assertXPath(@NotNull
                         String xpath)
Verifies HTTP response body XHTML/XML content against XPath query, and throws AssertionError in case of mismatch.

Parameters:
xpath - Query to use
Returns:
This object

assertJson

TestResponse assertJson(@NotNull
                        String element)
Verifies the Json data against the element identifier argument, and throws AssertionError in case of mismatch.

Parameters:
element - Element in the Json data of this object
Returns:
This object


Copyright © 2011-2012 ReXSL.com. All Rights Reserved.