com.jayway.restassured.internal
Class ResponseSpecificationImpl

java.lang.Object
  extended by com.jayway.restassured.internal.ResponseSpecificationImpl
All Implemented Interfaces:
FilterableResponseSpecification, RequestSender, ResponseSpecification, groovy.lang.GroovyObject

public class ResponseSpecificationImpl
extends Object
implements FilterableResponseSpecification, groovy.lang.GroovyObject


Nested Class Summary
 class ResponseSpecificationImpl.HamcrestAssertionClosure
           
 
Constructor Summary
ResponseSpecificationImpl(String bodyRootPath, Object responseContentType, ResponseSpecification defaultSpec, ResponseParserRegistrar rpr)
           
 
Method Summary
 ResponseSpecification and()
          Syntactic sugar, e.g.
 ResponseSpecification body(org.hamcrest.Matcher matcher, org.hamcrest.Matcher[] additionalMatchers)
          Expect that the response body conforms to one or more Hamcrest matchers.
 ResponseSpecification body(String key, List<Argument> arguments, org.hamcrest.Matcher matcher, Object[] additionalKeyMatcherPairs)
          Same as ResponseSpecification.body(String, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key.
 ResponseSpecification body(String key, org.hamcrest.Matcher matcher, Object[] additionalKeyMatcherPairs)
          Expect that the JSON or XML response body conforms to one or more Hamcrest matchers.
 ResponseSpecification content(org.hamcrest.Matcher matcher, org.hamcrest.Matcher[] additionalMatchers)
          Expect that the response content conforms to one or more Hamcrest matchers.
 ResponseSpecification content(String key, List<Argument> arguments, org.hamcrest.Matcher matcher, Object[] additionalKeyMatcherPairs)
          Same as ResponseSpecification.content(String, java.util.List, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key.
 ResponseSpecification content(String key, org.hamcrest.Matcher matcher, Object[] additionalKeyMatcherPairs)
          Expect that the JSON or XML response content conforms to one or more Hamcrest matchers.
 ResponseSpecification contentType(groovyx.net.http.ContentType contentType)
          Set the response content type to be contentType.
 ResponseSpecification contentType(String contentType)
          Set the response content type to be contentType.
 ResponseSpecification cookie(String cookieName)
          Expect that a cookie exist in the response, regardless of value (it may have no value at all).
 ResponseSpecification cookie(String cookieName, org.hamcrest.Matcher expectedValueMatcher)
          Expect that a response cookie matches the supplied cookie name and hamcrest matcher.
 ResponseSpecification cookie(String cookieName, Object expectedValue)
          Expect that a response cookie matches the supplied name and value.
 ResponseSpecification cookies(Map expectedCookies)
          Expect that response cookies matches those specified in a Map.
 ResponseSpecification cookies(String firstExpectedCookieName, Object firstExpectedCookieValue, Object[] expectedCookieNameValuePairs)
          Expect that response cookies matches the supplied cookie names and values.
 ResponseSpecification defaultParser(Parser parser)
          Register a default predefined parser that will be used if no other parser (registered or pre-defined) matches the response content-type.
 Response delete(String path, Map pathParams)
          Perform a DELETE request to a path.
 Response delete(String path, Object[] pathParams)
          Perform a DELETE request to a path.
 ResponseSpecification expect()
          Syntactic sugar, e.g.
 Response get(String path, Map pathParams)
          Perform a GET request to a path.
 Response get(String path, Object[] pathParams)
          Perform a GET request to a path.
 groovy.lang.MetaClass getMetaClass()
           
 Object getProperty(String property)
           
 String getResponseContentType()
           
 String getRootPath()
           
 org.hamcrest.Matcher<Integer> getStatusCode()
           
 org.hamcrest.Matcher<String> getStatusLine()
           
 RequestSpecification given()
          Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 boolean hasAssertionsDefined()
           
 boolean hasBodyAssertionsDefined()
           
 boolean hasCookieAssertions()
           
 boolean hasHeaderAssertions()
           
 Response head(String path, Map pathParams)
          Perform a HEAD request to a path.
 Response head(String path, Object[] pathParams)
          Perform a HEAD request to a path.
 ResponseSpecification header(String headerName, org.hamcrest.Matcher expectedValueMatcher)
          Expect that a response header matches the supplied header name and hamcrest matcher.
 ResponseSpecification header(String headerName, String expectedValue)
          Expect that a response header matches the supplied name and value.
 ResponseSpecification headers(Map expectedHeaders)
          Expect that response headers matches those specified in a Map.
 ResponseSpecification headers(String firstExpectedHeaderName, Object firstExpectedHeaderValue, Object[] expectedHeaders)
          Expect that response headers matches the supplied headers and values.
 Object invokeMethod(String method, Object arguments)
           
 ResponseLogSpecification log()
          Returns the ResponseLogSpecification that allows you to log different parts of the ResponseSpecification.
 ResponseSpecification parser(String contentType, Parser parser)
          Register a content-type to be parsed using a predefined parser.
 Response post(String path, Map pathParams)
          Perform a POST request to a path.
 Response post(String path, Object[] pathParams)
          Perform a POST request to a path.
 Response put(String path, Map pathParams)
          Perform a PUT request to a path.
 Response put(String path, Object[] pathParams)
          Perform a PUT request to a path.
 RequestSpecification request()
          Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 ResponseSpecification response()
          Syntactic sugar, e.g.
 ResponseSpecification root(String rootPath)
          Set the root path of the response body so that you don't need to write the entire path for each expectation.
 ResponseSpecification rootPath(String rootPath)
          Set the root path of the response body so that you don't need to write the entire path for each expectation.
 void setMetaClass(groovy.lang.MetaClass mc)
           
 void setProperty(String property, Object value)
           
 void setRequestSpec(RequestSpecification requestSpecification)
           
 ResponseSpecification spec(ResponseSpecification responseSpecificationToMerge)
          Expect that the response matches an entire specification.
 ResponseSpecification specification(ResponseSpecification responseSpecificationToMerge)
          Expect that the response matches an entire specification.
 ResponseSpecification statusCode(int expectedStatusCode)
          Expect that the response status code matches an integer.
 ResponseSpecification statusCode(org.hamcrest.Matcher<Integer> expectedStatusCode)
          Expect that the response status code matches the given Hamcrest matcher.
 ResponseSpecification statusLine(org.hamcrest.Matcher<String> expectedStatusLine)
          Expect that the response status line matches the given Hamcrest matcher.
 ResponseSpecification statusLine(String expectedStatusLine)
          Expect that the response status line matches the given String.
 ResponseSpecification that()
          Syntactic sugar, e.g.
 ResponseSpecification then()
          Syntactic sugar, e.g.
 ResponseSpecification when()
          Syntactic sugar, e.g.
 RequestSpecification with()
          Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResponseSpecificationImpl

public ResponseSpecificationImpl(String bodyRootPath,
                                 Object responseContentType,
                                 ResponseSpecification defaultSpec,
                                 ResponseParserRegistrar rpr)
Method Detail

getMetaClass

public groovy.lang.MetaClass getMetaClass()
Specified by:
getMetaClass in interface groovy.lang.GroovyObject

setMetaClass

public void setMetaClass(groovy.lang.MetaClass mc)
Specified by:
setMetaClass in interface groovy.lang.GroovyObject

invokeMethod

public Object invokeMethod(String method,
                           Object arguments)
Specified by:
invokeMethod in interface groovy.lang.GroovyObject

getProperty

public Object getProperty(String property)
Specified by:
getProperty in interface groovy.lang.GroovyObject

setProperty

public void setProperty(String property,
                        Object value)
Specified by:
setProperty in interface groovy.lang.GroovyObject

content

public ResponseSpecification content(org.hamcrest.Matcher matcher,
                                     org.hamcrest.Matcher[] additionalMatchers)
Description copied from interface: ResponseSpecification
Expect that the response content conforms to one or more Hamcrest matchers. E.g.
 // Expect that the response content (body) contains the string "winning-numbers"
 expect().content(containsString("winning-numbers")).when().get("/lotto");

 // Expect that the response content (body) contains the string "winning-numbers" and "winners"
 expect().content(containsString("winning-numbers"), containsString("winners")).when().get("/lotto");
 

Specified by:
content in interface ResponseSpecification
Parameters:
matcher - The hamcrest matcher that must response content must match.
additionalMatchers - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification

content

public ResponseSpecification content(String key,
                                     org.hamcrest.Matcher matcher,
                                     Object[] additionalKeyMatcherPairs)
Description copied from interface: ResponseSpecification
Expect that the JSON or XML response content conforms to one or more Hamcrest matchers.

JSON example

Assume that a GET request to "/lotto" returns a JSON response containing:

 { "lotto":{
   "lottoId":5,
   "winning-numbers":[2,45,34,23,7,5,3],
   "winners":[{
     "winnerId":23,
     "numbers":[2,45,34,23,3,5]
   },{
     "winnerId":54,
     "numbers":[52,3,12,11,18,22]
   }]
  }}
 
You can verify that the lottoId is equal to 5 like this:
 expect().content("lotto.lottoId", equalTo(5)).when().get("/lotto");
 
You can also verify that e.g. one of the the winning numbers is 45.
 expect().content("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 
Or both at the same time:
 expect().content("lotto.lottoId", equalTo(5)).and().content("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 
or a slightly short version:
 expect().content("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 

XML example

Assume that a GET request to "/xml" returns a XML response containing:

 <greeting>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
 </greeting>
 

You can now verify that the firstName is equal to "John" like this:
 expect().content("greeting.firstName", equalTo("John")).when().get("/xml");
 
To verify both the first name and last name you can do like this:
 expect().content("greeting.firstName", equalTo("John")).and().content("greeting.lastName", equalTo("Doe")).when().get("/xml");
 
Or the slightly shorter version of:
 expect().content("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe")).when().get("/xml");
 

Notes

Note that if the response content type is not of type application/xml or application/json you cannot use this verification.

The only difference between the content and body methods are of syntactic nature.

Specified by:
content in interface ResponseSpecification
matcher - The hamcrest matcher that must response content must match.
additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification

statusCode

public ResponseSpecification statusCode(org.hamcrest.Matcher<Integer> expectedStatusCode)
Description copied from interface: ResponseSpecification
Expect that the response status code matches the given Hamcrest matcher. E.g.
 expect().statusCode(equalTo(200)).when().get("/something");
 

Specified by:
statusCode in interface ResponseSpecification
Parameters:
expectedStatusCode - The expected status code matcher.
Returns:
the response specification

statusCode

public ResponseSpecification statusCode(int expectedStatusCode)
Description copied from interface: ResponseSpecification
Expect that the response status code matches an integer. E.g.
 expect().statusCode(200).when().get("/something");
 
This is the same as:
 expect().statusCode(equalTo(200)).when().get("/something");
 

Specified by:
statusCode in interface ResponseSpecification
Parameters:
expectedStatusCode - The expected status code.
Returns:
the response specification

statusLine

public ResponseSpecification statusLine(org.hamcrest.Matcher<String> expectedStatusLine)
Description copied from interface: ResponseSpecification
Expect that the response status line matches the given Hamcrest matcher. E.g.
 expect().statusLine(equalTo("No Content")).when().get("/something");
 

Specified by:
statusLine in interface ResponseSpecification
Parameters:
expectedStatusLine - The expected status line matcher.
Returns:
the response specification

headers

public ResponseSpecification headers(Map expectedHeaders)
Description copied from interface: ResponseSpecification
Expect that response headers matches those specified in a Map.

E.g. expect that the response of the GET request to "/something" contains header headerName1=headerValue1 and headerName2=headerValue2:

 Map expectedHeaders = new HashMap();
 expectedHeaders.put("headerName1", "headerValue1"));
 expectedHeaders.put("headerName2", "headerValue2");

 expect().response().headers(expectedHeaders).when().get("/something");
 

You can also use Hamcrest matchers:

 Map expectedHeaders = new HashMap();
 expectedHeaders.put("Content-Type", containsString("charset=UTF-8"));
 expectedHeaders.put("Content-Length", "160");

 expect().headers(expectedHeaders).when().get("/something");
 

Specified by:
headers in interface ResponseSpecification
Parameters:
expectedHeaders - The Map of expected response headers
Returns:
the response specification

headers

public ResponseSpecification headers(String firstExpectedHeaderName,
                                     Object firstExpectedHeaderValue,
                                     Object[] expectedHeaders)
Description copied from interface: ResponseSpecification
Expect that response headers matches the supplied headers and values.

E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache and Content-Encoding=gzip:

 expect().headers("Pragma", "no-cache", "Content-Encoding", "gzip").when().get("/something");
 

You can also use Hamcrest matchers:

 expect().response().headers("Content-Type", containsString("application/json"), "Pragma", equalsTo("no-cache")).when().get("/something");
 
and you can even mix string matching and hamcrest matching:
 expect().headers("Content-Type", containsString("application/json"), "Pragma", "no-cache").when().get("/something");
 

Specified by:
headers in interface ResponseSpecification
Parameters:
firstExpectedHeaderName - The name of the first header
firstExpectedHeaderValue - The value of the first header
expectedHeaders - A list of expected "header name" - "header value" pairs.
Returns:
the response specification

header

public ResponseSpecification header(String headerName,
                                    org.hamcrest.Matcher expectedValueMatcher)
Description copied from interface: ResponseSpecification
Expect that a response header matches the supplied header name and hamcrest matcher.

E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:

 expect().header("Pragma", containsString("no")).when().get("/something");
 

You can also expect several headers:

 expect().header("Pragma", equalsTo("no-cache")),and().header("Content-Encoding", containsString("zip")).when().get("/something");
 
Also take a look at ResponseSpecification.headers(String, Object, Object...) )} for a short version of passing multiple headers.

Specified by:
header in interface ResponseSpecification
Parameters:
headerName - The name of the expected header
expectedValueMatcher - The Hamcrest matcher that must conform to the value
Returns:
the response specification

header

public ResponseSpecification header(String headerName,
                                    String expectedValue)
Description copied from interface: ResponseSpecification
Expect that a response header matches the supplied name and value.

E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:

 expect().header("Pragma", "no-cache").when().get("/something");
 

You can also expect several headers:

 expect().header("Pragma", "no-cache"),and().header("Content-Encoding", "gzip").when().get("/something");
 
Also take a look at ResponseSpecification.headers(String, Object, Object...) for a short version of passing multiple headers.

Specified by:
header in interface ResponseSpecification
Parameters:
headerName - The name of the expected header
expectedValue - The value of the expected header
Returns:
the response specification

cookies

public ResponseSpecification cookies(Map expectedCookies)
Description copied from interface: ResponseSpecification
Expect that response cookies matches those specified in a Map.

E.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:

 Map expectedCookies = new HashMap();
 expectedCookies.put("cookieName1", "cookieValue1"));
 expectedCookies.put("cookieName2", "cookieValue2");

 expect().response().cookies(expectedCookies).when().get("/something");
 

You can also use Hamcrest matchers:

 Map expectedCookies = new HashMap();
 expectedCookies.put("cookieName1", containsString("Value1"));
 expectedCookies.put("cookieName2", "cookieValue2");

 expect().cookies(expectedCookies).when().get("/something");
 

Specified by:
cookies in interface ResponseSpecification
Parameters:
expectedCookies - A Map of expected response cookies
Returns:
the response specification

cookies

public ResponseSpecification cookies(String firstExpectedCookieName,
                                     Object firstExpectedCookieValue,
                                     Object[] expectedCookieNameValuePairs)
Description copied from interface: ResponseSpecification
Expect that response cookies matches the supplied cookie names and values.

E.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:

 expect().cookies("cookieName1", "cookieValue1", "cookieName2", "cookieValue2").when().get("/something");
 

You can also use Hamcrest matchers:

 expect().response().cookies("cookieName1", containsString("Value1"), "cookieName2", equalsTo("cookieValue2")).when().get("/something");
 
and you can even mix string matching and hamcrest matching:
 expect().cookies("cookieName1", containsString("Value1"), "cookieName2", "cookieValue2").when().get("/something");
 

Specified by:
cookies in interface ResponseSpecification
Parameters:
firstExpectedCookieName - The name of the first cookie
firstExpectedCookieValue - The value of the first cookie
expectedCookieNameValuePairs - A list of expected "cookie name" - "cookie value" pairs.
Returns:
the response specification

cookie

public ResponseSpecification cookie(String cookieName,
                                    org.hamcrest.Matcher expectedValueMatcher)
Description copied from interface: ResponseSpecification
Expect that a response cookie matches the supplied cookie name and hamcrest matcher.

E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1

 expect().cookie("cookieName1", containsString("Value1")).when().get("/something");
 

You can also expect several cookies:

 expect().cookie("cookieName1", equalsTo("cookieValue1")),and().cookie("cookieName2", containsString("Value2")).when().get("/something");
 
Also take a look at ResponseSpecification.cookies(String, Object, Object...) for a short version of passing multiple cookies.

Specified by:
cookie in interface ResponseSpecification
Parameters:
cookieName - The name of the expected cookie
expectedValueMatcher - The Hamcrest matcher that must conform to the value
Returns:
the response specification

cookie

public ResponseSpecification cookie(String cookieName)
Description copied from interface: ResponseSpecification
Expect that a cookie exist in the response, regardless of value (it may have no value at all).

Specified by:
cookie in interface ResponseSpecification
Parameters:
cookieName - the cookie to validate that it exists
Returns:
the response specification

cookie

public ResponseSpecification cookie(String cookieName,
                                    Object expectedValue)
Description copied from interface: ResponseSpecification
Expect that a response cookie matches the supplied name and value.

E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1:

 expect().cookie("cookieName1", "cookieValue1").when().get("/something");
 

You can also expect several cookies:

 expect().cookie("cookieName1", "cookieValue1"),and().cookie("cookieName2", "cookieValue2").when().get("/something");
 
Also take a look at ResponseSpecification.cookies(String, Object, Object...) for a short version of passing multiple cookies.

Specified by:
cookie in interface ResponseSpecification
Parameters:
cookieName - The name of the expected cookie
expectedValue - The value of the expected cookie
Returns:
the response specification

spec

public ResponseSpecification spec(ResponseSpecification responseSpecificationToMerge)
Description copied from interface: ResponseSpecification
Expect that the response matches an entire specification.
 ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();

 expect().
         spec(responseSpec).
         body("x.y.z", equalTo("something")).
 when().
        get("/something");
 
This is useful when you want to reuse multiple different expectations in several tests.

The specification passed to this method is merged with the current specification. Note that the supplied specification can overwrite data in the current specification. The following settings are overwritten:

The following settings are merged: This method is the same as ResponseSpecification.specification(ResponseSpecification) but the name is a bit shorter.

Specified by:
spec in interface ResponseSpecification
Parameters:
responseSpecificationToMerge - The specification to merge with.
Returns:
the response specification

specification

public ResponseSpecification specification(ResponseSpecification responseSpecificationToMerge)
Description copied from interface: ResponseSpecification
Expect that the response matches an entire specification.
 ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();

 expect().
         specification(responseSpec).
         body("x.y.z", equalTo("something")).
 when().
        get("/something");
 
This is useful when you want to reuse multiple different expectations in several tests.

The specification passed to this method is merged with the current specification. Note that the supplied specification can overwrite data in the current specification. The following settings are overwritten:

The following settings are merged: This method is the same as ResponseSpecification.spec(ResponseSpecification) but the name is a bit longer.

Specified by:
specification in interface ResponseSpecification
Parameters:
responseSpecificationToMerge - The specification to merge with.
Returns:
the response specification

statusLine

public ResponseSpecification statusLine(String expectedStatusLine)
Description copied from interface: ResponseSpecification
Expect that the response status line matches the given String. E.g.
 expect().statusLine("No Content").when().get("/something");
 
This is the same as:
 expect().statusLine(equalTo("No Content")).when().get("/something");
 

Specified by:
statusLine in interface ResponseSpecification
Parameters:
expectedStatusLine - The expected status line.
Returns:
the response specification

body

public ResponseSpecification body(org.hamcrest.Matcher matcher,
                                  org.hamcrest.Matcher[] additionalMatchers)
Description copied from interface: ResponseSpecification
Expect that the response body conforms to one or more Hamcrest matchers. E.g.
 // Expect that the response body (content) contains the string "winning-numbers"
 expect().body(containsString("winning-numbers")).when().get("/lotto");

 // Expect that the response body (content) contains the string "winning-numbers" and "winners"
 expect().body(containsString("winning-numbers"), containsString("winners")).when().get("/lotto");
 

Specified by:
body in interface ResponseSpecification
Parameters:
matcher - The hamcrest matcher that must response body must match.
additionalMatchers - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification

body

public ResponseSpecification body(String key,
                                  org.hamcrest.Matcher matcher,
                                  Object[] additionalKeyMatcherPairs)
Description copied from interface: ResponseSpecification
Expect that the JSON or XML response body conforms to one or more Hamcrest matchers.

JSON example

Assume that a GET request to "/lotto" returns a JSON response containing:

 { "lotto":{
   "lottoId":5,
   "winning-numbers":[2,45,34,23,7,5,3],
   "winners":[{
     "winnerId":23,
     "numbers":[2,45,34,23,3,5]
   },{
     "winnerId":54,
     "numbers":[52,3,12,11,18,22]
   }]
  }}
 
You can verify that the lottoId is equal to 5 like this:
 expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
 
You can also verify that e.g. one of the the winning numbers is 45.
 expect().body("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 
Or both at the same time:
 expect().body("lotto.lottoId", equalTo(5)).and().body("lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 
or a slightly short version:
 expect().body("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45)).when().get("/lotto");
 

XML example

Assume that a GET request to "/xml" returns a XML response containing:

 <greeting>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
 </greeting>
 

You can now verify that the firstName is equal to "John" like this:
 expect().body("greeting.firstName", equalTo("John")).when().get("/xml");
 
To verify both the first name and last name you can do like this:
 expect().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe")).when().get("/xml");
 
Or the slightly shorter version of:
 expect().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe")).when().get("/xml");
 

Notes

Note that if the response body type is not of type application/xml or application/json you cannot use this verification.

The only difference between the content and body methods are of syntactic nature.

Specified by:
body in interface ResponseSpecification
Parameters:
key - The body key
matcher - The hamcrest matcher that must response body must match.
additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification

body

public ResponseSpecification body(String key,
                                  List<Argument> arguments,
                                  org.hamcrest.Matcher matcher,
                                  Object[] additionalKeyMatcherPairs)
Description copied from interface: ResponseSpecification
Same as ResponseSpecification.body(String, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key. This is useful in situations where you have e.g. pre-defined variables that constitutes the key:
 String someSubPath = "else";
 int index = 1;
 expect().body("something.%s[%d]", withArgs(someSubPath, index), equalTo("some value")). ..
 
or if you have complex root paths and don't wish to duplicate the path for small variations:
 expect().
          root("filters.filterConfig[%d].filterConfigGroups.find { it.name == 'Gold' }.includes").
          body("", withArgs(0), hasItem("first")).
          body("", withArgs(1), hasItem("second")).
          ..
 
The key and arguments follows the standard formatting syntax of Java.

Note that withArgs can be statically imported from the com.jayway.restassured.RestAssured class.

Specified by:
body in interface ResponseSpecification
Parameters:
key - The body key
matcher - The hamcrest matcher that must response body must match.
additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification
See Also:
ResponseSpecification.body(String, org.hamcrest.Matcher, Object...)

content

public ResponseSpecification content(String key,
                                     List<Argument> arguments,
                                     org.hamcrest.Matcher matcher,
                                     Object[] additionalKeyMatcherPairs)
Description copied from interface: ResponseSpecification
Same as ResponseSpecification.content(String, java.util.List, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key. This is useful in situations where you have e.g. pre-defined variables that constitutes the key:
 String someSubPath = "else";
 int index = 1;
 expect().body("something.%s[%d]", withArgs(someSubPath, index), equalTo("some value")). ..
 
or if you have complex root paths and don't wish to duplicate the path for small variations:
 expect().
          root("filters.filterConfig[%d].filterConfigGroups.find { it.name == 'Gold' }.includes").
          body("", withArgs(0), hasItem("first")).
          body("", withArgs(1), hasItem("second")).
          ..
 
The key and arguments follows the standard formatting syntax of Java.

Note that withArgs can be statically imported from the com.jayway.restassured.RestAssured class.

Specified by:
content in interface ResponseSpecification
Parameters:
key - The body key
matcher - The hamcrest matcher that must response body must match.
additionalKeyMatcherPairs - Optionally additional hamcrest matchers that must return true.
Returns:
the response specification
See Also:
ResponseSpecification.content(String, org.hamcrest.Matcher, Object...)

log

public ResponseLogSpecification log()
Description copied from interface: ResponseSpecification
Returns the ResponseLogSpecification that allows you to log different parts of the ResponseSpecification. This is mainly useful for debug purposes when writing your tests. It's a shortcut for:
 given().filter(ResponseLoggingFilter.responseLogger()). ..
 

Specified by:
log in interface ResponseSpecification
Returns:
the response log specification

when

public ResponseSpecification when()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().body(containsString("OK")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Specified by:
when in interface ResponseSpecification
Returns:
the response specification

response

public ResponseSpecification response()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().response().body(containsString("OK")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Specified by:
response in interface ResponseSpecification
Returns:
the response specification

given

public RequestSpecification given()
Description copied from interface: ResponseSpecification
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 expect().body(containsString("OK")).given().parameters("param1", "value1").when().get("/something");
 

Specified by:
given in interface ResponseSpecification
Returns:
the request com.jayway.restassured.specification

that

public ResponseSpecification that()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().that().body(containsString("OK")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Specified by:
that in interface ResponseSpecification
Returns:
the response specification

request

public RequestSpecification request()
Description copied from interface: ResponseSpecification
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 expect().body(containsString("OK")).and().request().parameters("param1", "value1").when().get("/something");
 

Specified by:
request in interface ResponseSpecification
Returns:
the request com.jayway.restassured.specification

get

public Response get(String path,
                    Object[] pathParams)
Description copied from interface: RequestSender
Perform a GET request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
get in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do get("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

post

public Response post(String path,
                     Object[] pathParams)
Description copied from interface: RequestSender
Perform a POST request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
post in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do post("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

put

public Response put(String path,
                    Object[] pathParams)
Description copied from interface: RequestSender
Perform a PUT request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
put in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do put("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

delete

public Response delete(String path,
                       Object[] pathParams)
Description copied from interface: RequestSender
Perform a DELETE request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
delete in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do delete("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

head

public Response head(String path,
                     Object[] pathParams)
Description copied from interface: RequestSender
Perform a HEAD request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
head in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

get

public Response get(String path,
                    Map pathParams)
Description copied from interface: RequestSender
Perform a GET request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
get in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

post

public Response post(String path,
                     Map pathParams)
Description copied from interface: RequestSender
Perform a POST request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
post in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

put

public Response put(String path,
                    Map pathParams)
Description copied from interface: RequestSender
Perform a PUT request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
put in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

delete

public Response delete(String path,
                       Map pathParams)
Description copied from interface: RequestSender
Perform a DELETE request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
delete in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

head

public Response head(String path,
                     Map pathParams)
Description copied from interface: RequestSender
Perform a HEAD request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.

Specified by:
head in interface RequestSender
Parameters:
path - The path to send the request to.
pathParams - The path parameters.
Returns:
The response of the request. The response can only be returned if you don't use any REST Assured response expectations.

parser

public ResponseSpecification parser(String contentType,
                                    Parser parser)
Description copied from interface: ResponseSpecification
Register a content-type to be parsed using a predefined parser. E.g. let's say you want parse content-type application/vnd.uoml+xml with the XML parser to be able to verify the response using the XML dot notations:
 expect().body("document.child", equalsTo("something"))..
 
Since application/vnd.uoml+xml is not registered to be processed by the XML parser by default you need to explicitly tell REST Assured to use this parser before making the request:
 expect().parser("application/vnd.uoml+xml", Parser.XML).when(). ..;
 
You can also specify by it for every response by using:
 RestAssured.registerParser("application/vnd.uoml+xml", Parser.XML);
 

Specified by:
parser in interface ResponseSpecification
Parameters:
contentType - The content-type to register
parser - The parser to use when verifying the response.

and

public ResponseSpecification and()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().body(containsString("OK")).and().body(containsString("something else")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).body(containsString("something else")).when().get("/something");
 

Specified by:
and in interface ResponseSpecification
Returns:
the response specification

with

public RequestSpecification with()
Description copied from interface: ResponseSpecification
Returns the request com.jayway.restassured.specification so that you can define the properties of the request.
 expect().body(containsString("OK")).and().with().request().parameters("param1", "value1").get("/something");
 

Specified by:
with in interface ResponseSpecification
Returns:
the request com.jayway.restassured.specification

then

public ResponseSpecification then()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().body(containsString("OK")).then().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Specified by:
then in interface ResponseSpecification
Returns:
the response specification

expect

public ResponseSpecification expect()
Description copied from interface: ResponseSpecification
Syntactic sugar, e.g.
 expect().body(containsString("OK")).and().expect().body(containsString("something else")).when().get("/something");
 
is that same as:
 * expect().body(containsString("OK")).and().body(containsString("something else")).when().get("/something");
 

Specified by:
expect in interface ResponseSpecification
Returns:
the response specification

rootPath

public ResponseSpecification rootPath(String rootPath)
Description copied from interface: ResponseSpecification
Set the root path of the response body so that you don't need to write the entire path for each expectation. E.g. instead of writing:
 expect().
          body("x.y.firstName", is(..)).
          body("x.y.lastName", is(..)).
          body("x.y.age", is(..)).
          body("x.y.gender", is(..)).
 when().
          get(..);
you can use a root path and do:
 expect().
          rootPath("x.y").
          body("firstName", is(..)).
          body("lastName", is(..)).
          body("age", is(..)).
          body("gender", is(..)).
 when().
          get(..);
 
Note that this method is exactly the same as ResponseSpecification.root(String).

Specified by:
rootPath in interface ResponseSpecification
Parameters:
rootPath - The root path to use.

root

public ResponseSpecification root(String rootPath)
Description copied from interface: ResponseSpecification
Set the root path of the response body so that you don't need to write the entire path for each expectation. E.g. instead of writing:
 expect().
          body("x.y.firstName", is(..)).
          body("x.y.lastName", is(..)).
          body("x.y.age", is(..)).
          body("x.y.gender", is(..)).
 when().
          get(..);
you can use a root and do:
 expect().root("x.y").
          body("firstName", is(..)).
          body("lastName", is(..)).
          body("age", is(..)).
          body("gender", is(..)).
 when().
          get(..);
 
Note that this method is exactly the same as ResponseSpecification.rootPath(String) but slightly shorter.

Specified by:
root in interface ResponseSpecification
Parameters:
rootPath - The root path to use.

hasBodyAssertionsDefined

public boolean hasBodyAssertionsDefined()

hasAssertionsDefined

public boolean hasAssertionsDefined()

defaultParser

public ResponseSpecification defaultParser(Parser parser)
Description copied from interface: ResponseSpecification
Register a default predefined parser that will be used if no other parser (registered or pre-defined) matches the response content-type. E.g. let's say that for some reason no content-type is defined in the response but the content is nevertheless JSON encoded. To be able to expect the content in REST Assured you need to set the default parser:
 expect().defaultParser(Parser.JSON).when(). ..;
 
You can also specify it for every response by using:
 RestAssured.defaultParser(Parser.JSON);
 

Specified by:
defaultParser in interface ResponseSpecification
Parameters:
parser - The parser to use when verifying the response if no other parser is found for the response content-type.

contentType

public ResponseSpecification contentType(groovyx.net.http.ContentType contentType)
Description copied from interface: ResponseSpecification
Set the response content type to be contentType.

Note that this will affect the way the response is decoded. E,g. if you can't use JSON/XML matching (see e.g. ResponseSpecification.body(String, Matcher, Object...)) if you specify a content-type of "text/plain". If you don't specify the response content type REST Assured will automatically try to figure out which content type to use.

Specified by:
contentType in interface ResponseSpecification
Parameters:
contentType - The content type of the response.
Returns:
the response specification

contentType

public ResponseSpecification contentType(String contentType)
Description copied from interface: ResponseSpecification
Set the response content type to be contentType.

Note that this will affect the way the response is decoded. E,g. if you can't use JSON/XML matching (see e.g. ResponseSpecification.body(String, Matcher, Object...)) if you specify a content-type of "text/plain". If you don't specify the response content type REST Assured will automatically try to figure out which content type to use.

Specified by:
contentType in interface ResponseSpecification
Parameters:
contentType - The content type of the response.
Returns:
the response specification

setRequestSpec

public void setRequestSpec(RequestSpecification requestSpecification)

getStatusCode

public org.hamcrest.Matcher<Integer> getStatusCode()
Specified by:
getStatusCode in interface FilterableResponseSpecification
Returns:
The Hamcrest matcher that needs to be match the status code (may be null).

getStatusLine

public org.hamcrest.Matcher<String> getStatusLine()
Specified by:
getStatusLine in interface FilterableResponseSpecification
Returns:
The Hamcrest matcher that needs to be match the status line (may be null).

hasHeaderAssertions

public boolean hasHeaderAssertions()
Specified by:
hasHeaderAssertions in interface FilterableResponseSpecification
Returns:
true if any header assertions are defined

hasCookieAssertions

public boolean hasCookieAssertions()
Specified by:
hasCookieAssertions in interface FilterableResponseSpecification
Returns:
true if any cookie assertions are defined

getResponseContentType

public String getResponseContentType()
Specified by:
getResponseContentType in interface FilterableResponseSpecification
Returns:
The response content type

getRootPath

public String getRootPath()
Specified by:
getRootPath in interface FilterableResponseSpecification
Returns:
The body root path when expecting XML or JSON


Copyright © 2010-2011. All Rights Reserved.