public class ResponseSpecBuilder extends Object
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); RequestSpecification requestSpec = new RequestSpecBuilder().addParam("parameter1", "value1").build(); given(responseSpec, requestSpec).post("/something");or
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); expect(). spec(responseSpec). body("x.y.z", equalTo("something")). when(). get("/something");
Constructor and Description |
---|
ResponseSpecBuilder() |
Modifier and Type | Method and Description |
---|---|
ResponseSpecBuilder |
addResponseSpecification(ResponseSpecification specification)
Merge this builder with settings from another specification.
|
ResponseSpecBuilder |
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.
|
ResponseSpecBuilder |
appendRootPath(String pathToAppend,
List<Argument> 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.
|
ResponseSpecification |
build()
Build the response specification.
|
ResponseSpecBuilder |
detachRootPath(String pathToDetach)
Detach the given path from the root path.
|
ResponseSpecBuilder |
expectBody(org.hamcrest.Matcher<?> matcher)
Expect that the response body conforms to one or more Hamcrest matchers.
|
ResponseSpecBuilder |
expectBody(String path,
List<Argument> arguments,
org.hamcrest.Matcher<?> matcher)
Same as
expectBody(String, org.hamcrest.Matcher) expect that you can pass arguments to the path. |
ResponseSpecBuilder |
expectBody(String path,
org.hamcrest.Matcher<?> matcher)
Expect that the JSON or XML response content conforms to one or more Hamcrest matchers.
|
ResponseSpecBuilder |
expectContent(org.hamcrest.Matcher<?> matcher)
Deprecated.
Use
expectBody(Matcher) instead |
ResponseSpecBuilder |
expectContent(String path,
List<Argument> arguments,
org.hamcrest.Matcher<?> matcher)
Deprecated.
Use
expectBody(String, List, Matcher) instead |
ResponseSpecBuilder |
expectContent(String path,
org.hamcrest.Matcher<?> matcher)
Deprecated.
Use
expectBody(String, Matcher) instead |
ResponseSpecBuilder |
expectContentType(ContentType contentType)
Set the response content type to be
contentType . |
ResponseSpecBuilder |
expectContentType(String contentType)
Set the response content type to be
contentType . |
ResponseSpecBuilder |
expectCookie(String cookieName)
Expect that a cookie exist in the response, regardless of value (it may have no value at all).
|
ResponseSpecBuilder |
expectCookie(String cookieName,
org.hamcrest.Matcher<String> expectedValueMatcher)
Expect that a response cookie matches the supplied cookie name and hamcrest matcher.
|
ResponseSpecBuilder |
expectCookie(String cookieName,
String expectedValue)
Expect that a response cookie matches the supplied name and value.
|
ResponseSpecBuilder |
expectCookies(Map<String,Object> expectedCookies)
Expect that response cookies matches those specified in a Map.
|
ResponseSpecBuilder |
expectHeader(String headerName,
org.hamcrest.Matcher<String> expectedValueMatcher)
Expect that a response header matches the supplied header name and hamcrest matcher.
|
ResponseSpecBuilder |
expectHeader(String headerName,
String expectedValue)
Expect that a response header matches the supplied name and value.
|
ResponseSpecBuilder |
expectHeaders(Map<String,Object> expectedHeaders)
Expect that response headers matches those specified in a Map.
|
ResponseSpecBuilder |
expectResponseTime(org.hamcrest.Matcher<Long> matcher)
Validate that the response time (in milliseconds) matches the supplied
matcher . |
ResponseSpecBuilder |
expectResponseTime(org.hamcrest.Matcher<Long> matcher,
TimeUnit timeUnit)
Validate that the response time matches the supplied
matcher and time unit. |
ResponseSpecBuilder |
expectStatusCode(int expectedStatusCode)
Expect that the response status code matches an integer.
|
ResponseSpecBuilder |
expectStatusCode(org.hamcrest.Matcher<Integer> expectedStatusCode)
Expect that the response status code matches the given Hamcrest matcher.
|
ResponseSpecBuilder |
expectStatusLine(org.hamcrest.Matcher<String> expectedStatusLine)
Expect that the response status line matches the given Hamcrest matcher.
|
ResponseSpecBuilder |
expectStatusLine(String expectedStatusLine)
Expect that the response status line matches the given String.
|
ResponseSpecBuilder |
noRootPath()
Reset the root path of the response body so that you don't need to write the entire path for each expectation.
|
ResponseSpecBuilder |
registerParser(String contentType,
Parser parser)
Register a content-type to be parsed using a predefined parser.
|
ResponseSpecBuilder |
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.
|
ResponseSpecBuilder |
rootPath(String rootPath,
List<Argument> arguments)
Set the root path of the response body so that you don't need to write the entire path for each expectation.
|
ResponseSpecBuilder |
setDefaultParser(Parser parser)
Register a default predefined parser that will be used if no other parser (registered or pre-defined) matches the response
content-type.
|
@Deprecated public ResponseSpecBuilder expectContent(org.hamcrest.Matcher<?> matcher)
expectBody(Matcher)
insteadmatcher
- The hamcrest matcher that must response content must match.@Deprecated public ResponseSpecBuilder expectContent(String path, org.hamcrest.Matcher<?> matcher)
expectBody(String, Matcher)
instead{ "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:
ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectContent("lotto.lottoId", equalTo(5));
matcher
- The hamcrest matcher that the response content must match.@Deprecated public ResponseSpecBuilder expectContent(String path, List<Argument> arguments, org.hamcrest.Matcher<?> matcher)
expectBody(String, List, Matcher)
insteadexpectContent(String, org.hamcrest.Matcher)
expect that you can pass arguments to the path. This
is useful in situations where you have e.g. pre-defined variables that constitutes the path:
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 path and arguments follows the standard formatting syntax of Java.
Note that withArgs
can be statically imported from the io.restassured.RestAssured
class.
path
- The body pathmatcher
- The hamcrest matcher that must response body must match.expectBody(String, Matcher)
public ResponseSpecBuilder expectStatusCode(org.hamcrest.Matcher<Integer> expectedStatusCode)
expectedStatusCode
- The expected status code matcher.public ResponseSpecBuilder expectStatusCode(int expectedStatusCode)
expectedStatusCode
- The expected status code.public ResponseSpecBuilder expectStatusLine(org.hamcrest.Matcher<String> expectedStatusLine)
expectedStatusLine
- The expected status line matcher.public ResponseSpecBuilder expectStatusLine(String expectedStatusLine)
expectedStatusLine
- The expected status line.public ResponseSpecBuilder expectHeaders(Map<String,Object> expectedHeaders)
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");
You can also use Hamcrest matchers:
Map expectedHeaders = new HashMap(); expectedHeaders.put("Content-Type", containsString("charset=UTF-8")); expectedHeaders.put("Content-Length", "160");
expectedHeaders
- The Map of expected response headerspublic ResponseSpecBuilder expectHeader(String headerName, org.hamcrest.Matcher<String> expectedValueMatcher)
headerName
- The name of the expected headerexpectedValueMatcher
- The Hamcrest matcher that must conform to the valuepublic ResponseSpecBuilder expectHeader(String headerName, String expectedValue)
headerName
- The name of the expected headerexpectedValue
- The value of the expected headerpublic ResponseSpecBuilder expectCookies(Map<String,Object> expectedCookies)
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");
You can also use Hamcrest matchers:
Map expectedCookies = new HashMap(); expectedCookies.put("cookieName1", containsString("Value1")); expectedCookies.put("cookieName2", "cookieValue2");
expectedCookies
- A Map of expected response cookiespublic ResponseSpecBuilder expectCookie(String cookieName, org.hamcrest.Matcher<String> expectedValueMatcher)
E.g. cookieName1=cookieValue1
cookieName
- The name of the expected cookieexpectedValueMatcher
- The Hamcrest matcher that must conform to the valuepublic ResponseSpecBuilder expectCookie(String cookieName, String expectedValue)
cookieName
- The name of the expected cookieexpectedValue
- The value of the expected cookiepublic ResponseSpecBuilder expectCookie(String cookieName)
cookieName
- the cookie to validate that it existspublic ResponseSpecBuilder expectResponseTime(org.hamcrest.Matcher<Long> matcher)
matcher
.matcher
- The matcherpublic ResponseSpecBuilder expectResponseTime(org.hamcrest.Matcher<Long> matcher, TimeUnit timeUnit)
matcher
and time unit.matcher
- The matchertimeUnit
- The timeoutpublic ResponseSpecBuilder rootPath(String rootPath)
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(..);
rootPath
- The root path to use.public ResponseSpecBuilder rootPath(String rootPath, List<Argument> arguments)
rootPath(String)
but also provides a way to defined arguments.rootPath
- The root path to use.arguments
- The arguments.ResponseSpecification.rootPath(String, java.util.List)
public ResponseSpecBuilder appendRootPath(String pathToAppend)
expect(). root("x.y"). body("age", is(..)). body("gender", is(..)). body("name.firstName", is(..)). body("name.lastName", is(..)). when(). get(..);you can use a append root and do:
expect(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("name"). body("firstName", is(..)). body("lastName", is(..)). when(). get(..);
pathToAppend
- The root path to append.public ResponseSpecBuilder appendRootPath(String pathToAppend, List<Argument> arguments)
String namePath = "name"; expect(). root("x.y"). body("age", is(..)). body("gender", is(..)). body(namePath + "first", is(..)). body(namePath + "last", is(..)). when(). get(..);you can use a append root and do:
String namePath = "name"; expect(). root("x.y"). body("age", is(..)). body("gender", is(..)). appendRoot("%s", withArgs(namePath)). body("first", is(..)). body("last", is(..)). when(). get(..);
pathToAppend
- The root path to use. The path and arguments follows the standard formatting syntax of Java.public ResponseSpecBuilder noRootPath()
expect(). root("x.y"). body("firstName", is(..)). body("lastName", is(..)). noRoot() body("z.something1", is(..)). body("w.something2", is(..)). when(). get(..);This is the same as calling
rootPath("")
but more expressive.rootPath(String)
public ResponseSpecBuilder detachRootPath(String pathToDetach)
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(..)).
pathToDetach
- The root path to detach.public ResponseSpecBuilder expectContentType(ContentType contentType)
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. expectBody(String, org.hamcrest.Matcher)
) 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
- The content type of the response.public ResponseSpecBuilder expectContentType(String contentType)
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. expectBody(String, org.hamcrest.Matcher)
) 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
- The content type of the response.public ResponseSpecBuilder expectBody(org.hamcrest.Matcher<?> matcher)
matcher
- The hamcrest matcher that must response content must match.public ResponseSpecBuilder expectBody(String path, org.hamcrest.Matcher<?> matcher)
{ "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:
ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectBody("lotto.lottoId", equalTo(5));
matcher
- The hamcrest matcher that the response content must match.public ResponseSpecBuilder expectBody(String path, List<Argument> arguments, org.hamcrest.Matcher<?> matcher)
expectBody(String, org.hamcrest.Matcher)
expect that you can pass arguments to the path. This
is useful in situations where you have e.g. pre-defined variables that constitutes the path:
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 path and arguments follows the standard formatting syntax of Java.
Note that withArgs
can be statically imported from the io.restassured.RestAssured
class.
path
- The body pathmatcher
- The hamcrest matcher that must response body must match.expectBody(String, org.hamcrest.Matcher)
public ResponseSpecBuilder addResponseSpecification(ResponseSpecification specification)
specification
- The specification the add.public ResponseSpecBuilder registerParser(String contentType, Parser parser)
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 default by using:
RestAssured.registerParser("application/vnd.uoml+xml, Parser.XML");
contentType
- The content-type to registerparser
- The parser to use when verifying the response.public ResponseSpecBuilder setDefaultParser(Parser parser)
expect().defaultParser(Parser.JSON).when(). ..;You can also specify it for every response by using:
RestAssured.defaultParser(Parser.JSON);
parser
- The parser to use when verifying the response if no other parser is found for the response content-type.public ResponseSpecification build()
Copyright © 2010–2017. All rights reserved.