public class ValidatableResponseImpl extends ValidatableResponseOptionsImpl implements ValidatableResponse, groovy.lang.GroovyObject
response, responseSpec
Constructor and Description |
---|
ValidatableResponseImpl(String contentType,
ResponseParserRegistrar rpr,
RestAssuredConfig config,
Response response,
ExtractableResponse extractableResponse,
LogRepository logRepository) |
Modifier and Type | Method and Description |
---|---|
ValidatableResponse |
all()
Logs everything in the response, including e.g.
|
ValidatableResponse |
all(boolean shouldPrettyPrint)
Logs everything in the response, including e.g.
|
ValidatableResponse |
and()
Syntactic sugar, e.g.
|
ValidatableResponse |
appendRootPath(String pathToAppend)
Append the given path to the root path of the response body so that you don't need to write the entire path for each expectation.
|
ValidatableResponse |
appendRootPath(String pathToAppend,
List arguments)
Append the given path to the root path with arguments supplied of the response body so that you don't need to write the entire path for each expectation.
|
ValidatableResponse |
assertThat()
Syntactic sugar, e.g.
|
ValidatableResponse |
body()
Logs only the content of the body.
|
ValidatableResponse |
body(boolean shouldPrettyPrint)
Logs only the content of the body and pretty-print the body if specified.
|
ValidatableResponse |
body(List arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
This as special kind of expectation that is mainly useful when you've specified a root path with an argument placeholder.
|
ValidatableResponse |
body(List arguments,
ResponseAwareMatcher responseAwareMatcher)
Compare a path in the response body to something available in the response using arguments when root path is used.
|
ValidatableResponse |
body(org.hamcrest.Matcher matcher,
org.hamcrest.Matcher... additionalMatchers)
Validate that the response body conforms to one or more Hamcrest matchers.
|
ValidatableResponse |
body(String path,
List arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Same as
ValidatableResponseOptions.body(String, org.hamcrest.Matcher, Object...) expect that you can pass arguments to the key. |
ValidatableResponse |
body(String key,
List arguments,
ResponseAwareMatcher responseAwareMatcher)
Compare a path in the response body to something available in the response using arguments.
|
ValidatableResponse |
body(String path,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs)
Validate that the JSON or XML response body conforms to one or more Hamcrest matchers.
|
ValidatableResponse |
body(String key,
ResponseAwareMatcher responseAwareMatcher)
Compare a path in the response body to something available in the response, for example another path.
|
ValidatableResponse |
content(String path,
List arguments,
org.hamcrest.Matcher matcher,
Object... additionalKeyMatcherPairs) |
ValidatableResponse |
contentType(ContentType contentType)
Set the response content type to be
contentType . |
ValidatableResponse |
contentType(org.hamcrest.Matcher contentType)
Validate the response content type to be
contentType . |
ValidatableResponse |
contentType(String contentType)
Set the response content type to be
contentType . |
ValidatableResponse |
cookie(String cookieName)
Validate that a cookie exist in the response, regardless of value (it may have no value at all).
|
ValidatableResponse |
cookie(String cookieName,
DetailedCookieMatcher detailedCookieMatcher)
Validate that a detailed response cookie matches the supplied cookie name and hamcrest matcher (see
DetailedCookieMatcher ). |
ValidatableResponse |
cookie(String cookieName,
org.hamcrest.Matcher expectedValueMatcher)
Validate that a response cookie matches the supplied cookie name and hamcrest matcher.
|
ValidatableResponse |
cookie(String cookieName,
Object expectedValue)
Validate that a response cookie matches the supplied name and value.
|
ValidatableResponse |
cookies()
Logs only the cookies.
|
ValidatableResponse |
cookies(Map expectedCookies)
Validate that response cookies matches those specified in a Map.
|
ValidatableResponse |
cookies(String firstExpectedCookieName,
Object firstExpectedCookieValue,
Object... expectedCookieNameValuePairs)
Validate that response cookies matches the supplied cookie names and values.
|
ValidatableResponse |
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.
|
ValidatableResponse |
detachRootPath(String pathToDetach)
Detach the given path from the root path.
|
ValidatableResponse |
everything()
Logs everything in the response, including e.g.
|
ValidatableResponse |
everything(boolean shouldPrettyPrint)
* Logs everything in the response, including e.g.
|
ExtractableResponse |
extract()
Extract values from the response or return the response instance itself.
|
groovy.lang.MetaClass |
getMetaClass() |
ValidatableResponse |
header(String headerName,
java.util.function.Function f,
org.hamcrest.Matcher matcher)
Expect that a response header matches the supplied header name and hamcrest matcher using a mapping function.
|
ValidatableResponse |
header(String headerName,
org.hamcrest.Matcher expectedValueMatcher)
Validate that a response header matches the supplied header name and hamcrest matcher.
|
ValidatableResponse |
header(String headerName,
ResponseAwareMatcher r)
Compare a header in the response to something else available in the response.
|
ValidatableResponse |
header(String headerName,
String expectedValue)
Validate that a response header matches the supplied name and value.
|
ValidatableResponse |
headers()
Logs only the headers.
|
ValidatableResponse |
headers(Map expectedHeaders)
Validate that response headers matches those specified in a Map.
|
ValidatableResponse |
headers(String firstExpectedHeaderName,
Object firstExpectedHeaderValue,
Object... expectedHeaders)
Validate that response headers matches the supplied headers and values.
|
ValidatableResponse |
ifError()
Logs everything only if an error occurs (status code >= 400).
|
ValidatableResponse |
ifStatusCodeIsEqualTo(int statusCode)
Logs everything only if if the status code is equal to
statusCode . |
ValidatableResponse |
ifStatusCodeMatches(org.hamcrest.Matcher matcher)
Logs everything only if if the status code matches the supplied
matcher |
ValidatableResponse |
ifValidationFails()
Logs everything if a test validation fails.
|
ValidatableResponse |
ifValidationFails(LogDetail logDetail)
Logs with the supplied log detail only if the validation fails.
|
ValidatableResponse |
ifValidationFails(LogDetail logDetail,
boolean shouldPrettyPrint)
Logs all parameters only if the validations fail.
|
ValidatableResponseLogSpec |
log()
Returns the
ValidatableResponseLogSpec that allows you to log different parts of the Response . |
ValidatableResponse |
noRoot()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
ValidatableResponse |
noRootPath()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
Response |
originalResponse() |
ValidatableResponse |
parser(String contentType,
Parser parser)
Register a content-type to be parsed using a predefined parser.
|
ValidatableResponse |
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.
|
ValidatableResponse |
root(String rootPath,
List arguments)
Set the root path with arguments of the response body so that you don't need to write the entire path for each expectation.
|
ValidatableResponse |
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.
|
ValidatableResponse |
rootPath(String rootPath,
List arguments)
Set the root path with arguments of the response body so that you don't need to write the entire path for each expectation.
|
void |
setMetaClass(groovy.lang.MetaClass mc) |
ValidatableResponse |
spec(ResponseSpecification responseSpecification)
Validate that the response matches an entire specification.
|
ValidatableResponse |
status()
Logs only the status line (includes the status code)
|
ValidatableResponse |
statusCode(int expectedStatusCode)
Validate that the response status code matches an integer.
|
ValidatableResponse |
statusCode(org.hamcrest.Matcher expectedStatusCode)
Validate that the response status code matches the given Hamcrest matcher.
|
ValidatableResponse |
statusLine(org.hamcrest.Matcher expectedStatusLine)
Validate that the response status line matches the given Hamcrest matcher.
|
ValidatableResponse |
statusLine(String expectedStatusLine)
Validate that the response status line matches the given String.
|
ValidatableResponse |
time(org.hamcrest.Matcher matcher)
Validate that the response time (in milliseconds) matches the supplied
matcher . |
ValidatableResponse |
time(org.hamcrest.Matcher matcher,
TimeUnit timeUnit)
Validate that the response time matches the supplied
matcher and time unit. |
ValidatableResponse |
using()
Syntactic sugar, e.g.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
appendRoot, appendRoot, detachRoot
public ValidatableResponseImpl(String contentType, ResponseParserRegistrar rpr, RestAssuredConfig config, Response response, ExtractableResponse extractableResponse, LogRepository logRepository)
public Response originalResponse()
originalResponse
in class ValidatableResponseOptionsImpl
public ValidatableResponse time(org.hamcrest.Matcher matcher)
ValidatableResponseOptions
matcher
. For example:
when(). get("/something"). then(). time(lessThan(2000));where
lessThan
is a Hamcrest matchertime
in interface ValidatableResponseOptions<ValidatableResponse,Response>
time
in class ValidatableResponseOptionsImpl
ValidatableResponse
instance.public ValidatableResponse time(org.hamcrest.Matcher matcher, TimeUnit timeUnit)
ValidatableResponseOptions
matcher
and time unit. For example:
when(). get("/something"). then(). time(lessThan(2), TimeUnit.SECONDS);where
lessThan
is a Hamcrest matchertime
in interface ValidatableResponseOptions<ValidatableResponse,Response>
time
in class ValidatableResponseOptionsImpl
ValidatableResponse
instance.public ValidatableResponse defaultParser(Parser parser)
ValidatableResponseOptions
get("/x").then().using().defaultParser(Parser.JSON).assertThat(). ..;You can also specify it for every response by using:
RestAssured.defaultParser(Parser.JSON);
defaultParser
in interface ValidatableResponseOptions<ValidatableResponse,Response>
defaultParser
in class ValidatableResponseOptionsImpl
parser
- The parser to use when verifying the response if no other parser is found for the response content-type.public ValidatableResponse parser(String contentType, Parser parser)
ValidatableResponseOptions
get("/x").then().assertThat().body("document.child", equalsTo("something")Since application/custom 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:
get("/x").then().parser("application/custom", Parser.XML).assertThat(). ..;You can also specify by it for every response by using:
RestAssured.registerParser("application/custom", Parser.XML);
parser
in interface ValidatableResponseOptions<ValidatableResponse,Response>
parser
in class ValidatableResponseOptionsImpl
contentType
- The content-type to registerparser
- The parser to use when verifying the response.public ValidatableResponse spec(ResponseSpecification responseSpecification)
ValidatableResponseOptions
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); get("/something").then() spec(responseSpec). body("x.y.z", equalTo("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:
spec
in interface ValidatableResponseOptions<ValidatableResponse,Response>
spec
in class ValidatableResponseOptionsImpl
responseSpecification
- The specification to merge with.public ValidatableResponse assertThat()
ValidatableResponseOptions
get("/something").then().assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().body(containsString("OK")).body(containsString("something else"));
assertThat
in interface ValidatableResponseOptions<ValidatableResponse,Response>
assertThat
in class ValidatableResponseOptionsImpl
public ValidatableResponse using()
ValidatableResponseOptions
get("/something").then().using().defaultParser(JSON).assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().defaultParser(JSON).body(containsString("OK")).body(containsString("something else"));
using
in interface ValidatableResponseOptions<ValidatableResponse,Response>
using
in class ValidatableResponseOptionsImpl
public ValidatableResponse and()
ValidatableResponseOptions
get("/something").then().assertThat().body(containsString("OK")).and().body(containsString("something else"));is that same as:
get("/something").then().assertThat().body(containsString("OK")).body(containsString("something else"));
and
in interface ValidatableResponseOptions<ValidatableResponse,Response>
and
in class ValidatableResponseOptionsImpl
public ValidatableResponse all()
ValidatableResponseLogSpec
all
in interface ValidatableResponseLogSpec
all
in class ValidatableResponseOptionsImpl
public ValidatableResponse body(List arguments, ResponseAwareMatcher responseAwareMatcher)
ValidatableResponseOptions
{ "data" : { "user1" : { "userId" : "my-id1", "href" : "http://localhost:8080/my-id1" }, "user2" : { "userId" : "my-id2", "href" : "http://localhost:8080/my-id2" }, } }you can then verify the href using:
when(). get("/x"). then(). root("data.%s"). body(withArgs("user1"), new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
responseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.ValidatableResponseOptions.body(String, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
public ValidatableResponse body(String key, List arguments, ResponseAwareMatcher responseAwareMatcher)
ValidatableResponseOptions
{ "userId" : "my-id", "my.href" : "http://localhost:8080/my-id" }you can then verify the href using:
get("/x").then().body("%s.href", withArgs("my"), new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
key
- The body pathresponseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.ValidatableResponseOptions.body(String, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
public ValidatableResponse body(String key, ResponseAwareMatcher responseAwareMatcher)
ValidatableResponseOptions
{ "userId" : "my-id", "href" : "http://localhost:8080/my-id" }you can then verify the href using:
get("/x").then().body("href", new ResponseAwareMatcherNote that you can also use some of the predefined methods in() { public Matcher> matcher(Response response) { return equalTo("http://localhost:8080/" + response.path("userId")); } });
RestAssuredMatchers
.body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
key
- The body pathresponseAwareMatcher
- The ResponseAwareMatcher
that creates the Hamcrest matcher.ValidatableResponseOptions.body(String, java.util.List, ResponseAwareMatcher)
,
RestAssuredMatchers.endsWithPath(String)
,
RestAssuredMatchers.startsWithPath(String)
,
RestAssuredMatchers.containsPath(String)
,
RestAssuredMatchers.equalToPath(String)
public ValidatableResponse body(String path, List arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ValidatableResponseOptions
ValidatableResponseOptions.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; get("/x").then().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:
get("/x").then(). 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 io.restassured.RestAssured
class.
body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
path
- The body patharguments
- The arguments to apply to the keymatcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.ValidatableResponseOptions.body(String, org.hamcrest.Matcher, Object...)
public ValidatableResponse body(List arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ValidatableResponseOptions
get(..).then(). root("x.%s"). // Root path with a placeholder body(withArgs("firstName"), equalTo(..)). body(withArgs("lastName"), equalTo(..)).Note that this is the same as doing:
get(..).then(). root("x.%s"). // Root path with a placeholder content(withArgs("firstName"), equalTo(..)). content(withArgs("lastName"), equalTo(..)).
body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
arguments
- The arguments to apply to the root path.matcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.ValidatableResponseOptions.body(String, org.hamcrest.Matcher, Object...)
public ValidatableResponse statusCode(org.hamcrest.Matcher expectedStatusCode)
ValidatableResponseOptions
get("/something").then().assertThat().statusCode(equalTo(200));
statusCode
in interface ValidatableResponseOptions<ValidatableResponse,Response>
statusCode
in class ValidatableResponseOptionsImpl
expectedStatusCode
- The expected status code matcher.public ValidatableResponse statusCode(int expectedStatusCode)
ValidatableResponseOptions
get("/something").then().assertThat().statusCode(200);This is the same as:
get("/something").then().assertThat().statusCode(equalTo(200));
statusCode
in interface ValidatableResponseOptions<ValidatableResponse,Response>
statusCode
in class ValidatableResponseOptionsImpl
expectedStatusCode
- The expected status code.public ValidatableResponse statusLine(org.hamcrest.Matcher expectedStatusLine)
ValidatableResponseOptions
expect().statusLine(equalTo("No Content")).when().get("/something");
statusLine
in interface ValidatableResponseOptions<ValidatableResponse,Response>
statusLine
in class ValidatableResponseOptionsImpl
expectedStatusLine
- The expected status line matcher.public ValidatableResponse statusLine(String expectedStatusLine)
ValidatableResponseOptions
expect().statusLine("No Content").when().get("/something");This is the same as:
expect().statusLine(equalTo("No Content")).when().get("/something");
statusLine
in interface ValidatableResponseOptions<ValidatableResponse,Response>
statusLine
in class ValidatableResponseOptionsImpl
expectedStatusLine
- The expected status line.public ValidatableResponse headers(Map expectedHeaders)
ValidatableResponseOptions
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"); get("/something").then().assertThat().headers(expectedHeaders);
You can also use Hamcrest matchers:
Map expectedHeaders = new HashMap(); expectedHeaders.put("Content-Type", containsString("charset=UTF-8")); expectedHeaders.put("Content-Length", "160"); get("/something").then().assertThat().headers(expectedHeaders);
headers
in interface ValidatableResponseOptions<ValidatableResponse,Response>
headers
in class ValidatableResponseOptionsImpl
expectedHeaders
- The Map of expected response headerspublic ValidatableResponse headers(String firstExpectedHeaderName, Object firstExpectedHeaderValue, Object... expectedHeaders)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache and Content-Encoding=gzip:
get("/something").then().assertThat().headers("Pragma", "no-cache", "Content-Encoding", "gzip");
You can also use Hamcrest matchers:
get("/something").then().assertThat().headers("Content-Type", containsString("application/json"), "Pragma", equalsTo("no-cache"));and you can even mix string matching and hamcrest matching:
get("/something").then().assertThat().headers("Content-Type", containsString("application/json"), "Pragma", "no-cache");
headers
in interface ValidatableResponseOptions<ValidatableResponse,Response>
headers
in class ValidatableResponseOptionsImpl
firstExpectedHeaderName
- The name of the first headerfirstExpectedHeaderValue
- The value of the first headerexpectedHeaders
- A list of expected "header name" - "header value" pairs.public ValidatableResponse header(String headerName, org.hamcrest.Matcher expectedValueMatcher)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
get("/something").then().assertThat().header("Pragma", containsString("no"));
You can also expect several headers:
get("/something").then().assertThat().header("Pragma", equalsTo("no-cache")).and().header("Content-Encoding", containsString("zip"));Also take a look at
ValidatableResponseOptions.headers(String, Object, Object...)
)} for a short version of passing multiple headers.
header
in interface ValidatableResponseOptions<ValidatableResponse,Response>
header
in class ValidatableResponseOptionsImpl
headerName
- The name of the expected headerexpectedValueMatcher
- The Hamcrest matcher that must conform to the valuepublic ValidatableResponse header(String headerName, ResponseAwareMatcher r)
ValidatableResponseOptions
For example imagine that a POST to resource "/x" returns "201 Created" and sets a Location header
that should end with "/x/{id}" where {id}
is present in the response body:
{ "id" : 5 }To verify that the Location header ends with "/x/{id}" you can do like this:
given().param("id", 1).body(..).post("/x").then().assertThat().header("Location", response -> endsWith("/x/" + response.path("id")));
header
in interface ValidatableResponseOptions<ValidatableResponse,Response>
header
in class ValidatableResponseOptionsImpl
headerName
- The name of the expected headerr
- The Hamcrest matcher that must conform to the valuepublic ValidatableResponse header(String headerName, String expectedValue)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contains header Pragma=no-cache:
get("/something").then().assertThat().header("Pragma", "no-cache");
You can also expect several headers:
get("/something").then().assertThat().header("Pragma", "no-cache").and().header("Content-Encoding", "gzip");Also take a look at
ValidatableResponseOptions.headers(String, Object, Object...)
for a short version of passing multiple headers.
header
in interface ValidatableResponseOptions<ValidatableResponse,Response>
header
in class ValidatableResponseOptionsImpl
headerName
- The name of the expected headerexpectedValue
- The value of the expected headerpublic ValidatableResponse header(String headerName, java.util.function.Function f, org.hamcrest.Matcher matcher)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contains header Content-Length: 500 and you want to validate that the length must always be less than 600:
when(). get("/something"). then(). header("Content-Length", Integer::parseInt, lessThan(600));
header
in interface ValidatableResponseOptions<ValidatableResponse,Response>
header
in class ValidatableResponseOptionsImpl
headerName
- The name of the expected headerf
- Map the header to another value type before exposing it to the Hamcrest matchermatcher
- The Hamcrest matcher that must conform to the valuepublic ValidatableResponse cookies(Map expectedCookies)
ValidatableResponseOptions
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"); get("/something").then().assertThat().cookies(expectedCookies);
You can also use Hamcrest matchers:
Map expectedCookies = new HashMap(); expectedCookies.put("cookieName1", containsString("Value1")); expectedCookies.put("cookieName2", "cookieValue2"); get("/something").then().assertThat().cookies(expectedCookies);
cookies
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookies
in class ValidatableResponseOptionsImpl
expectedCookies
- A Map of expected response cookiespublic ValidatableResponse cookie(String cookieName)
ValidatableResponseOptions
cookie
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookie
in class ValidatableResponseOptionsImpl
cookieName
- the cookie to validate that it existspublic ValidatableResponse cookies(String firstExpectedCookieName, Object firstExpectedCookieValue, Object... expectedCookieNameValuePairs)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contains cookies cookieName1=cookieValue1 and cookieName2=cookieValue2:
get("/something").then().assertThat().cookies("cookieName1", "cookieValue1", "cookieName2", "cookieValue2");
You can also use Hamcrest matchers:
get("/something").then().assertThat().cookies("cookieName1", containsString("Value1"), "cookieName2", equalsTo("cookieValue2"));and you can even mix string matching and hamcrest matching:
get("/something").then().assertThat().cookies("cookieName1", containsString("Value1"), "cookieName2", "cookieValue2");
cookies
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookies
in class ValidatableResponseOptionsImpl
firstExpectedCookieName
- The name of the first cookiefirstExpectedCookieValue
- The value of the first cookieexpectedCookieNameValuePairs
- A list of expected "cookie name" - "cookie value" pairs.public ValidatableResponse cookie(String cookieName, org.hamcrest.Matcher expectedValueMatcher)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1
get("/something").then().assertThat().cookie("cookieName1", containsString("Value1"));
You can also expect several cookies:
get("/something").then().assertThat().cookie("cookieName1", equalsTo("cookieValue1")).and().cookie("cookieName2", containsString("Value2"));Also take a look at
ValidatableResponseOptions.cookies(String, Object, Object...)
for a short version of passing multiple cookies.
cookie
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookie
in class ValidatableResponseOptionsImpl
cookieName
- The name of the expected cookieexpectedValueMatcher
- The Hamcrest matcher that must conform to the valuepublic ValidatableResponse cookie(String cookieName, Object expectedValue)
ValidatableResponseOptions
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1:
get("/something").then().assertThat().cookie("cookieName1", "cookieValue1");
You can also expect several cookies:
get("/something").then().assertThat().cookie("cookieName1", "cookieValue1").and().cookie("cookieName2", "cookieValue2");Also take a look at
ValidatableResponseOptions.cookies(String, Object, Object...)
for a short version of passing multiple cookies.
cookie
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookie
in class ValidatableResponseOptionsImpl
cookieName
- The name of the expected cookieexpectedValue
- The value of the expected cookiepublic ValidatableResponse cookie(String cookieName, DetailedCookieMatcher detailedCookieMatcher)
ValidatableResponseOptions
DetailedCookieMatcher
).
E.g. expect that the response of the GET request to "/something" contain cookie cookieName1=cookieValue1
get("/something").then().assertThat() .detailedCookie("cookieName1", detailedCookie().value("cookieValue1").secured(true));
You can also expect several cookies:
get("/something").then().assertThat().detailedCookie("cookieName1", detailedCookie().value("cookieValue1").secured(true)) .and().detailedCookie("cookieName2", detailedCookie().value("cookieValue2").secured(false));
cookie
in interface ValidatableResponseOptions<ValidatableResponse,Response>
cookie
in class ValidatableResponseOptionsImpl
cookieName
- The name of the expected cookiedetailedCookieMatcher
- The Hamcrest matcher that must conform to the cookiepublic ValidatableResponse rootPath(String rootPath)
ValidatableResponseOptions
get(..).then(). body("x.y.firstName", is(..)). body("x.y.lastName", is(..)). body("x.y.age", is(..)). body("x.y.gender", is(..)).you can use a root path and do:
get(..).then(). rootPath("x.y"). body("firstName", is(..)). body("lastName", is(..)). body("age", is(..)). body("gender", is(..));Note that this method is exactly the same as
ValidatableResponseOptions.root(String)
.rootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
rootPath
in class ValidatableResponseOptionsImpl
rootPath
- The root path to use.public ValidatableResponse rootPath(String rootPath, List arguments)
ValidatableResponseOptions
ValidatableResponseOptions.root(String, java.util.List)
.rootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
rootPath
in class ValidatableResponseOptionsImpl
rootPath
- The root path to use.arguments
- A list of arguments. The path and arguments follows the standard formatting syntax of Java.ValidatableResponseOptions.rootPath(String)
public ValidatableResponse root(String rootPath, List arguments)
ValidatableResponseOptions
ValidatableResponseOptions.rootPath(String, java.util.List)
.root
in interface ValidatableResponseOptions<ValidatableResponse,Response>
root
in class ValidatableResponseOptionsImpl
rootPath
- The root path to use.arguments
- The list of substitution arguments. The path and arguments follows the standard formatting syntax of Java..ValidatableResponseOptions.rootPath(String)
public ValidatableResponse root(String rootPath)
ValidatableResponseOptions
get(..).then(). body("x.y.firstName", is(..)). body("x.y.lastName", is(..)). body("x.y.age", is(..)). body("x.y.gender", is(..));you can use a root and do:
get(..).then(). root("x.y"). body("firstName", is(..)). body("lastName", is(..)). body("age", is(..)). body("gender", is(..)).Note that this method is exactly the same as
ValidatableResponseOptions.rootPath(String)
but slightly shorter.root
in interface ValidatableResponseOptions<ValidatableResponse,Response>
root
in class ValidatableResponseOptionsImpl
rootPath
- The root path to use.public ValidatableResponse noRoot()
ValidatableResponseOptions
get(..).then(). root("x.y"). body("firstName", is(..)). body("lastName", is(..)). noRoot() body("z.something1", is(..)). body("w.something2", is(..));This is the same as calling
rootPath("")
but more expressive.
Note that this method is exactly the same as ValidatableResponseOptions.noRootPath()
but slightly shorter.noRoot
in interface ValidatableResponseOptions<ValidatableResponse,Response>
noRoot
in class ValidatableResponseOptionsImpl
ValidatableResponseOptions.root(String)
public ValidatableResponse noRootPath()
ValidatableResponseOptions
get(..).then(). rootPath("x.y"). body("firstName", is(..)). body("lastName", is(..)). noRootPath() body("z.something1", is(..)). body("w.something2", is(..)).This is the same as calling
rootPath("")
but more expressive.
Note that this method is exactly the same as ValidatableResponseOptions.noRoot()
but slightly more expressive.noRootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
noRootPath
in class ValidatableResponseOptionsImpl
ValidatableResponseOptions.rootPath(String)
public ValidatableResponse appendRootPath(String pathToAppend)
ValidatableResponseOptions
get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). body("name.firstName", is(..)). body("name.lastName", is(..));you can use a append root and do:
get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("name"). body("firstName", is(..)). body("lastName", is(..));
appendRootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
appendRootPath
in class ValidatableResponseOptionsImpl
pathToAppend
- The root path to append.public ValidatableResponse appendRootPath(String pathToAppend, List arguments)
ValidatableResponseOptions
String namePath = "name"; get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). body(namePath + "first", is(..)). body(namePath + "last", is(..)).you can use a append root and do:
String namePath = "name"; get(..).then(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("%s", withArgs(namePath)). body("first", is(..)). body("last", is(..)).
appendRootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
appendRootPath
in class ValidatableResponseOptionsImpl
pathToAppend
- The root path to append. The path and arguments follows the standard formatting syntax of Java.public ValidatableResponse detachRootPath(String pathToDetach)
ValidatableResponseOptions
when(). get(..); then(). root("x.y"). body("age", is(..)). body("gender", is(..)). root("x"). body("firstName", is(..)). body("lastName", is(..)).you can use a append root and do:
when(). get(..); then(). root("x.y"). body("age", is(..)). body("gender", is(..)). detachRoot("y"). body("firstName", is(..)). body("lastName", is(..)).
detachRootPath
in interface ValidatableResponseOptions<ValidatableResponse,Response>
detachRootPath
in class ValidatableResponseOptionsImpl
pathToDetach
- The root path to detach.public ValidatableResponse contentType(ContentType contentType)
ValidatableResponseOptions
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. ValidatableResponseOptions.body(String, org.hamcrest.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.
contentType
in interface ValidatableResponseOptions<ValidatableResponse,Response>
contentType
in class ValidatableResponseOptionsImpl
contentType
- The content type of the response.public ValidatableResponse contentType(String contentType)
ValidatableResponseOptions
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. ValidatableResponseOptions.body(String, org.hamcrest.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.
contentType
in interface ValidatableResponseOptions<ValidatableResponse,Response>
contentType
in class ValidatableResponseOptionsImpl
contentType
- The content type of the response.public ValidatableResponse contentType(org.hamcrest.Matcher contentType)
ValidatableResponseOptions
contentType
.contentType
in interface ValidatableResponseOptions<ValidatableResponse,Response>
contentType
in class ValidatableResponseOptionsImpl
contentType
- The expected content type of the response.public ValidatableResponse body(org.hamcrest.Matcher matcher, org.hamcrest.Matcher... additionalMatchers)
ValidatableResponseOptions
// Validate that the response body (content) contains the string "winning-numbers" get("/lotto").then().assertThat().body(containsString("winning-numbers")); // Validate that the response body (content) contains the string "winning-numbers" and "winners" get("/lotto").then().assertThat().body(containsString("winning-numbers"), containsString("winners"));
body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
matcher
- The hamcrest matcher that must response body must match.additionalMatchers
- Optionally additional hamcrest matchers that must return true
.public ValidatableResponse body(String path, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
ValidatableResponseOptions
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:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));You can also verify that e.g. one of the the winning numbers is 45.
get("/lotto").then().assertThat().body("lotto.winning-numbers", hasItem(45));Or both at the same time:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5)).and().body("lotto.winning-numbers", hasItem(45));or a slightly short version:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5), "lotto.winning-numbers", hasItem(45));
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:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John"));To verify both the first name and last name you can do like this:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe"));Or the slightly shorter version of:
get("/xml").then().assertThat().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe"));
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.
body
in interface ValidatableResponseOptions<ValidatableResponse,Response>
body
in class ValidatableResponseOptionsImpl
path
- The body pathmatcher
- The hamcrest matcher that must response body must match.additionalKeyMatcherPairs
- Optionally additional hamcrest matchers that must return true
.public ValidatableResponse content(String path, List arguments, org.hamcrest.Matcher matcher, Object... additionalKeyMatcherPairs)
content
in class ValidatableResponseOptionsImpl
public ExtractableResponse extract()
ValidatableResponseOptions
title
returns the following JSON
{ "title" : "My Title", "_links": { "self": { "href": "/title" }, "next": { "href": "/title?page=2" } } }and you want to validate that content type is equal to
JSON
and the title is equal to My Title
but you also want to extract the link to the "next" title to use that in a subsequent request. This is how:
String nextTitleLink = given(). param("param_name", "param_value"). when(). get("/title"). then(). contentType(JSON). body("title", equalTo("My Title")). extract(). path("_links.next.href"); get(nextTitleLink). ..
extract
in interface ValidatableResponseOptions<ValidatableResponse,Response>
extract
in class ValidatableResponseOptionsImpl
ExtractableResponse
.public ValidatableResponseLogSpec log()
ValidatableResponseOptions
ValidatableResponseLogSpec
that allows you to log different parts of the Response
.
This is mainly useful for debug purposes when writing your tests.log
in interface ValidatableResponseOptions<ValidatableResponse,Response>
log
in class ValidatableResponseOptionsImpl
public ValidatableResponse status()
ValidatableResponseLogSpec
status
in interface ValidatableResponseLogSpec
status
in class ValidatableResponseOptionsImpl
public ValidatableResponse ifError()
ValidatableResponseLogSpec
ifError
in interface ValidatableResponseLogSpec
ifError
in class ValidatableResponseOptionsImpl
public ValidatableResponse ifStatusCodeIsEqualTo(int statusCode)
ValidatableResponseLogSpec
statusCode
.ifStatusCodeIsEqualTo
in interface ValidatableResponseLogSpec
ifStatusCodeIsEqualTo
in class ValidatableResponseOptionsImpl
statusCode
- The status codepublic ValidatableResponse ifStatusCodeMatches(org.hamcrest.Matcher matcher)
ValidatableResponseLogSpec
matcher
ifStatusCodeMatches
in interface ValidatableResponseLogSpec
ifStatusCodeMatches
in class ValidatableResponseOptionsImpl
matcher
- The hamcrest matcherpublic ValidatableResponse body()
ValidatableResponseLogSpec
body
in interface ValidatableResponseLogSpec
body
in class ValidatableResponseOptionsImpl
public ValidatableResponse body(boolean shouldPrettyPrint)
ValidatableResponseLogSpec
body
in interface ValidatableResponseLogSpec
body
in class ValidatableResponseOptionsImpl
shouldPrettyPrint
- true
if the body should be pretty-printed, false
otherwise.public ValidatableResponse all(boolean shouldPrettyPrint)
ValidatableResponseLogSpec
all
in interface ValidatableResponseLogSpec
all
in class ValidatableResponseOptionsImpl
shouldPrettyPrint
- true
if the body should be pretty-printed, false
otherwise.public ValidatableResponse everything()
ValidatableResponseLogSpec
everything
in interface ValidatableResponseLogSpec
everything
in class ValidatableResponseOptionsImpl
public ValidatableResponse everything(boolean shouldPrettyPrint)
ValidatableResponseLogSpec
everything
in interface ValidatableResponseLogSpec
everything
in class ValidatableResponseOptionsImpl
shouldPrettyPrint
- true
if the body should be pretty-printed, false
otherwise.public ValidatableResponse headers()
ValidatableResponseLogSpec
headers
in interface ValidatableResponseLogSpec
headers
in class ValidatableResponseOptionsImpl
public ValidatableResponse cookies()
ValidatableResponseLogSpec
cookies
in interface ValidatableResponseLogSpec
cookies
in class ValidatableResponseOptionsImpl
public ValidatableResponse ifValidationFails()
ValidatableResponseLogSpec
ifValidationFails
in interface ValidatableResponseLogSpec
ifValidationFails
in class ValidatableResponseOptionsImpl
public ValidatableResponse ifValidationFails(LogDetail logDetail)
ValidatableResponseLogSpec
ifValidationFails
in interface ValidatableResponseLogSpec
ifValidationFails
in class ValidatableResponseOptionsImpl
logDetail
- The log detailpublic ValidatableResponse ifValidationFails(LogDetail logDetail, boolean shouldPrettyPrint)
ValidatableResponseLogSpec
ifValidationFails
in interface ValidatableResponseLogSpec
ifValidationFails
in class ValidatableResponseOptionsImpl
logDetail
- The log detailshouldPrettyPrint
- true
if the body should be pretty-printed, false
otherwise.public groovy.lang.MetaClass getMetaClass()
getMetaClass
in interface groovy.lang.GroovyObject
public void setMetaClass(groovy.lang.MetaClass mc)
setMetaClass
in interface groovy.lang.GroovyObject
Copyright © 2010–2020. All rights reserved.