public class RestAssured extends Object
{ "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] }] } }REST assured can then help you to easily make the GET request and verify the response. E.g. if you want to verify that lottoId is equal to 5 you can do like this:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));or perhaps you want to check that the winnerId's are 23 and 54:
get("/lotto").then().assertThat().body("lotto.winners.winnerId", hasItems(23, 54));
<greeting> <firstName>{params("firstName")}</firstName> <lastName>{params("lastName")}</lastName> </greeting>i.e. it sends back a greeting based on the firstName and lastName parameter sent in the request. You can easily perform and verify e.g. the firstName with REST assured:
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John"));If you want to verify both firstName and lastName you may do like this:
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe"));or a little shorter:
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe"));
given().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body(hasXPath("/greeting/firstName", containsString("Jo")));or
with().parameters("firstName", "John", "lastName", "Doe").post("/greetXML").then().body(hasXPath("/greeting/firstName[text()='John']"));
get("/carRecords").then().assertThat().body(matchesXsd(xsd));DTD example:
get("/videos").then().assertThat().body(matchesDtd(dtd));
matchesXsd
and matchesDtd
are Hamcrest matchers which you can import from RestAssuredMatchers
.
given().cookie("username", "John").when().get("/cookie").then().assertThat().body(equalTo("username"));
given().header("MyHeader", "Something").and(). .. given().headers("MyHeader", "Something", "MyOtherHeader", "SomethingElse").and(). ..
given().contentType(ContentType.TEXT). ..
given().request().body("some body"). .. // Works for POST and PUT requests given().request().body(new byte[]{42}). .. // Works for POST
expect().cookie("cookieName", "cookieValue"). .. expect().cookies("cookieName1", "cookieValue1", "cookieName2", "cookieValue2"). .. expect().cookies("cookieName1", "cookieValue1", "cookieName2", containsString("Value2")). ..
get("/x").then().assertThat().statusCode(200). .. get("/x").then().assertThat().statusLine("something"). .. get("/x").then().assertThat().statusLine(containsString("some")). ..
get("/x").then().assertThat().header("headerName", "headerValue"). .. get("/x").then().assertThat().headers("headerName1", "headerValue1", "headerName2", "headerValue2"). .. get("/x").then().assertThat().headers("headerName1", "headerValue1", "headerName2", containsString("Value2")). ..
get("/x").then().assertThat().contentType(ContentType.JSON). ..
Greeting greeting = get("/greeting").as(Greeting.class);
Greeting greeting = new Greeting(); greeting.setFirstName("John"); greeting.setLastName("Doe"); given().body(greeting).when().post("/greeting");See the javadoc for the body method for more details.
get("/x").then().assertThat().body(equalsTo("something")). .. get("/x").then().assertThat().content(equalsTo("something")). .. // Same as above
given().auth().basic("username", "password").when().get("/secured/hello").then().statusCode(200);Other supported schemes are OAuth and certificate authentication.
given().port(80). ..or simply:
.. when().get("http://myhost.org:80/doSomething");
..when().get("/name?firstName=John&lastName=Doe");
XmlPath
or JsonPath
to
easily parse XML or JSON data from a response.
String xml = post("/greetXML?firstName=John&lastName=Doe").andReturn().asString(); // Now use XmlPath to get the first and last name String firstName = with(xml).get("greeting.firstName"); String lastName = with(xml).get("greeting.lastName"); // or a bit more efficiently: XmlPath xmlPath = new XmlPath(xml).setRootPath("greeting"); String firstName = xmlPath.get("firstName"); String lastName = xmlPath.get("lastName");
String json = get("/lotto").asString(); // Now use JsonPath to get data out of the JSON body int lottoId = with(json).getInt("lotto.lottoId); ListwinnerIds = with(json).get("lotto.winners.winnerId"); // or a bit more efficiently: JsonPath jsonPath = new JsonPath(json).setRootPath("lotto"); int lottoId = jsonPath.getInt("lottoId"); List winnerIds = jsonPath.get("winners.winnerId");
RestAssured.registerParser(<content-type>, <parser>);E.g. to register that content-type
'application/custom'
should be parsed using the XML parser do:
RestAssured.registerParser("application/custom", Parser.XML);You can also unregister a parser using:
RestAssured.unregisterParser("application/custom");If can also specify a default parser for all content-types that do not match a pre-defined or registered parser. This is also useful if the response doesn't contain a content-type at all:
RestAssured.defaultParser = Parser.JSON;
ResponseSpecBuilder
and RequestSpecBuilder
like this:
RequestSpecification requestSpec = new RequestSpecBuilder().addParameter("parameter1", "value1").build(); ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); given(). spec(requestSpec). when(). get("/something"); then(). spec(responseSpec). body("x.y.z", equalTo("something"));
given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(302)). ..will log/print the response body to after each request.
RestAssured.baseURI = "http://myhost.org"; RestAssured.port = 80; RestAssured.basePath = "/resource"; RestAssured.authentication = basic("username", "password"); RestAssured.rootPath = "store.book";This means that a request like e.g.
get("/hello")
goes to: http://myhost.org:8080/resource/hello
which basic authentication credentials "username" and "password". See rootPath
for more info about setting the root paths, filters(java.util.List)
for setting
default filtersRestAssured.reset();
In order to use REST assured effectively it's recommended to statically import methods from the following classes:
Modifier and Type | Field and Description |
---|---|
static AuthenticationScheme |
authentication
Set an authentication scheme that should be used for each request.
|
static String |
basePath
A base path that's added to the
baseURI by REST assured when making requests. |
static String |
baseURI
The base URI that's used by REST assured when making requests if a non-fully qualified URI is used in the request.
|
static RestAssuredConfig |
config
Define a configuration for e.g.
|
static AuthenticationScheme |
DEFAULT_AUTH |
static String |
DEFAULT_BODY_ROOT_PATH |
static String |
DEFAULT_PATH |
static int |
DEFAULT_PORT |
static String |
DEFAULT_SESSION_ID_VALUE |
static String |
DEFAULT_URI |
static boolean |
DEFAULT_URL_ENCODING_ENABLED |
static Parser |
defaultParser
Specify a default parser.
|
static int |
port
The port that's used by REST assured when it's left out of the specified URI when making a request.
|
static ProxySpecification |
proxy
Specify a default proxy that REST Assured will use for all requests (unless overridden by individual tests).
|
static RequestSpecification |
requestSpecification
Specify a default request specification that will be sent with each request.
|
static ResponseSpecification |
responseSpecification
Specify a default response specification that will be sent with each request.
|
static String |
rootPath
Set the default root path of the response body so that you don't need to write the entire path for each expectation.
|
static String |
sessionId
Set the default session id value that'll be used for each request.
|
static int |
UNDEFINED_PORT |
static boolean |
urlEncodingEnabled
Specifies if Rest Assured should url encode the URL automatically.
|
Constructor and Description |
---|
RestAssured() |
Modifier and Type | Method and Description |
---|---|
static AuthenticationScheme |
basic(String userName,
String password)
Create a http basic authentication scheme.
|
static AuthenticationScheme |
certificate(String certURL,
String password)
Sets a certificate to be used for SSL authentication.
|
static AuthenticationScheme |
certificate(String certURL,
String password,
CertificateAuthSettings certificateAuthSettings)
Sets a certificate to be used for SSL authentication.
|
static AuthenticationScheme |
certificate(String trustStorePath,
String trustStorePassword,
String keyStorePath,
String keyStorePassword,
CertificateAuthSettings certificateAuthSettings)
Sets a certificate to be used for SSL authentication.
|
static RestAssuredConfig |
config() |
static Response |
delete()
Perform a DELETE request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
delete(String path,
Map<String,?> pathParams)
Perform a DELETE request to a
path . |
static Response |
delete(String path,
Object... pathParams)
Perform a DELETE request to a
path . |
static Response |
delete(URI uri)
Perform a DELETE request to a
uri . |
static Response |
delete(URL url)
Perform a DELETE request to a
url . |
static AuthenticationScheme |
digest(String userName,
String password)
Use http digest authentication.
|
static void |
enableLoggingOfRequestAndResponseIfValidationFails()
Enable logging of both the request and the response if REST Assureds test validation fails with log detail equal to
LogDetail.ALL . |
static void |
enableLoggingOfRequestAndResponseIfValidationFails(LogDetail logDetail)
Enable logging of both the request and the response if REST Assureds test validation fails with the specified log detail.
|
static ResponseSpecification |
expect()
Start building the response part of the test io.restassured.specification.
|
static List<Filter> |
filters() |
static void |
filters(Filter filter,
Filter... additionalFilters)
Add default filters to apply to each request.
|
static void |
filters(List<Filter> filters)
Add default filters that will be applied to each request.
|
static AuthenticationScheme |
form(String userName,
String password)
Use form authentication.
|
static AuthenticationScheme |
form(String userName,
String password,
FormAuthConfig config)
Use form authentication with the supplied configuration.
|
static Response |
get()
Perform a GET request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
get(String path,
Map<String,?> pathParams)
Perform a GET request to a
path . |
static Response |
get(String path,
Object... pathParams)
Perform a GET request to a
path . |
static Response |
get(URI uri)
Perform a GET request to a
uri . |
static Response |
get(URL url)
Perform a GET request to a
url . |
static RequestSpecification |
given()
Start building the request part of the test io.restassured.specification.
|
static RequestSpecification |
given(RequestSpecification requestSpecification)
When you're only interested in supplying a predefined request specification without a response specification then you can use this method.
|
static RequestSender |
given(RequestSpecification requestSpecification,
ResponseSpecification responseSpecification)
When you have long specifications it can be better to split up the definition of response and request specifications in multiple lines.
|
static Response |
head()
Perform a HEAD request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
head(String path,
Map<String,?> pathParams)
Perform a HEAD request to a
path . |
static Response |
head(String path,
Object... pathParams)
Perform a HEAD request to a
path . |
static Response |
head(URI uri)
Perform a HEAD request to a
uri . |
static Response |
head(URL url)
Perform a HEAD request to a
url . |
static void |
keyStore(File pathToJks,
String password)
Use a keystore located on the file-system.
|
static void |
keyStore(String password)
Uses the user default keystore stored in @{user.home}/.keystore
*
|
static void |
keyStore(String pathToJks,
String password)
Apply a keystore for all requests
|
static AuthenticationScheme |
ntlm(String userName,
String password,
String workstation,
String domain)
Create a NTLM authentication scheme.
|
static AuthenticationScheme |
oauth(String consumerKey,
String consumerSecret,
String accessToken,
String secretToken)
Excerpt from the HttpBuilder docs:
OAuth sign the request. |
static AuthenticationScheme |
oauth(String consumerKey,
String consumerSecret,
String accessToken,
String secretToken,
OAuthSignature signature)
Excerpt from the HttpBuilder docs:
OAuth sign the request. |
static AuthenticationScheme |
oauth2(String accessToken)
OAuth sign the request.
|
static AuthenticationScheme |
oauth2(String accessToken,
OAuthSignature signature)
OAuth sign the request.
|
static Response |
options()
Perform a OPTIONS request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
options(String path,
Map<String,?> pathParams)
Perform a OPTIONS request to a
path . |
static Response |
options(String path,
Object... pathParams)
Perform a OPTIONS request to a
path . |
static Response |
options(URI uri)
Perform a OPTIONS request to a
uri . |
static Response |
options(URL url)
Perform a OPTIONS request to a
url . |
static Response |
patch()
Perform a PATCH request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
patch(String path,
Map<String,?> pathParams)
Perform a PATCH request to a
path . |
static Response |
patch(String path,
Object... pathParams)
Perform a PATCH request to a
path . |
static Response |
patch(URI uri)
Perform a PATCH request to a
uri . |
static Response |
patch(URL url)
Perform a PATCH request to a
url . |
static Response |
post()
Perform a POST request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
post(String path,
Map<String,?> pathParams)
Perform a POST request to a
path . |
static Response |
post(String path,
Object... pathParams)
Perform a POST request to a
path . |
static Response |
post(URI uri)
Perform a POST request to a
uri . |
static Response |
post(URL url)
Perform a POST request to a
url . |
static PreemptiveAuthProvider |
preemptive()
Return the http preemptive authentication specification for setting up preemptive authentication requests.
|
static void |
proxy(int port)
Instruct REST Assured to connect to a proxy on the specified port on localhost.
|
static void |
proxy(ProxySpecification proxySpecification)
Instruct REST Assured to connect to a proxy using a
ProxySpecification . |
static void |
proxy(String host)
Instruct REST Assured to connect to a proxy on the specified host on port
8888 . |
static void |
proxy(String host,
int port)
Instruct REST Assured to connect to a proxy on the specified host and port.
|
static void |
proxy(String host,
int port,
String scheme)
Instruct REST Assured to connect to a proxy on the specified port on localhost with a specific scheme.
|
static void |
proxy(URI uri)
Instruct REST Assured to connect to a proxy using a URI.
|
static Response |
put()
Perform a PUT request to the statically configured path (by default
http://localhost:8080 ). |
static Response |
put(String path,
Object... pathParams)
Perform a PUT request to a
path . |
static Response |
put(URI uri)
Perform a PUT request to a
uri . |
static Response |
put(URL url)
Perform a PUT request to a
url . |
static void |
registerParser(String contentType,
Parser parser)
Register a custom content-type to be parsed using a predefined parser.
|
static void |
replaceFiltersWith(Filter filter,
Filter... additionalFilters)
Sets the default filters to apply to each request.
|
static void |
replaceFiltersWith(List<Filter> filters)
Sets the default filters to apply to each request.
|
static Response |
request(Method method)
Perform a request to the pre-configured path (by default
http://localhost:8080 ). |
static Response |
request(Method method,
String path,
Object... pathParams)
Perform a HTTP request to a
path . |
static Response |
request(Method method,
URI uri)
Perform a request to a
uri . |
static Response |
request(Method method,
URL url)
Perform a request to a
url . |
static Response |
request(String method)
Perform a custom HTTP request to the pre-configured path (by default
http://localhost:8080 ). |
static Response |
request(String method,
String path,
Object... pathParams)
Perform a custom HTTP request to a
path . |
static Response |
request(String method,
URI uri)
Perform a custom HTTP request to a
uri . |
static Response |
request(String method,
URL url)
Perform a custom HTTP request to a
url . |
static void |
reset()
Resets the
baseURI , basePath , port , authentication and rootPath ,
filters(java.util.List) , requestSpecification , responseSpecification ,
urlEncodingEnabled , config , sessionId and proxy to their default values of "http://localhost", "", -1,
no authentication , <empty string>, null , null ,
<empty list>, null , null , none , true , new RestAssuredConfig() , null and null . |
static void |
trustStore(File pathToJks,
String password)
Use a trust store located on the file-system.
|
static void |
trustStore(KeyStore truststore)
Specify a trust store that'll be used for HTTPS requests.
|
static void |
trustStore(String pathToJks,
String password)
The following documentation is taken from https://github.com/jgritman/httpbuilder/wiki/SSL:
|
static void |
unregisterParser(String contentType)
Unregister the parser associated with the provided content-type
|
static void |
useRelaxedHTTPSValidation()
Use relaxed HTTP validation with protocol .
|
static void |
useRelaxedHTTPSValidation(String protocol)
Use relaxed HTTP validation with a specific protocol.
|
static RequestSender |
when()
Start building the DSL expression by sending a request without any parameters or headers etc.
|
static RequestSpecification |
with()
Start building the request part of the test io.restassured.specification.
|
static List<Argument> |
withArgs(Object firstArgument,
Object... additionalArguments)
Create a list of arguments that can be used to create parts of the path in a body/content expression.
|
static List<Argument> |
withNoArgs()
Create a list of no arguments that can be used to create parts of the path in a response specification for JSON, XML or HTML validation.
|
public static final String DEFAULT_URI
public static final String DEFAULT_BODY_ROOT_PATH
public static final int DEFAULT_PORT
public static final int UNDEFINED_PORT
public static final String DEFAULT_PATH
public static final AuthenticationScheme DEFAULT_AUTH
public static final boolean DEFAULT_URL_ENCODING_ENABLED
public static final String DEFAULT_SESSION_ID_VALUE
public static String baseURI
public static int port
public static String basePath
public static boolean urlEncodingEnabled
RestAssured.baseURI = "https://jira.atlassian.com"; RestAssured.port = 443; RestAssured.urlEncodingEnabled = false; // Because "query" is already url encoded String query = "project%20=%20BAM%20AND%20issuetype%20=%20Bug"; String response = get("/rest/api/2.0.alpha1/search?jql={q}",query).andReturn().asString(); ...The
query
is already url encoded so you need to disable Rest Assureds url encoding to prevent double encoding.public static AuthenticationScheme authentication
given().auth().none()..
public static RestAssuredConfig config
new RestAssuredConfig()
). E.g.
RestAssured.config = config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));
config()
can be statically imported from RestAssuredConfig
.
public static String rootPath
get(..).then().assertThat(). 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:
RestAssured.rootPath = "x.y"; get(..).then().assertThat(). body("firstName", is(..)). body("lastName", is(..)). body("age", is(..)). body("gender", is(..)).
public static RequestSpecification requestSpecification
RestAssured.requestSpecification = new RequestSpecBuilder().addParameter("parameter1", "value1").build();means that for each request by Rest Assured "parameter1" will be equal to "value1".
public static Parser defaultParser
public static ResponseSpecification responseSpecification
RestAssured.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();means that for each response Rest Assured will assert that the status code is equal to 200.
public static String sessionId
SessionConfig
so it'll
override the session id value previously defined there (if any). If you need to change the sessionId cookie name you need to configure and supply the SessionConfig
to
RestAssured.config
.public static ProxySpecification proxy
RestAssured.proxy = host("127.0.0.1").withPort(8888);where
host
is statically imported from ProxySpecification.host(String)
.public static void filters(List<Filter> filters)
filters
- The filter listpublic static void filters(Filter filter, Filter... additionalFilters)
filter
- The filter to addadditionalFilters
- An optional array of additional filters to addpublic static void replaceFiltersWith(List<Filter> filters)
filters
- The filter listpublic static void replaceFiltersWith(Filter filter, Filter... additionalFilters)
filter
- The filter to setadditionalFilters
- An optional array of additional filters to setpublic static ResponseSpecification expect()
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");will expect that the response body for the GET request to "/lotto" should contain JSON or XML which has a lottoId equal to 5.
public static RequestSpecification with()
with().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().assertThat().body("greeting.firstName", equalTo("John"));will send a POST request to "/greetXML" with request parameters firstName=John and lastName=Doe and expect that the response body containing JSON or XML firstName equal to John. The only difference between
with()
and given()
is syntactical.public static List<Argument> withArgs(Object firstArgument, Object... additionalArguments)
String someSubPath = "else"; int index = 1; when().get().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().assertThat(). 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.
public static List<Argument> withNoArgs()
get("/jsonStore").then(). root("store.%s", withArgs("book")). body("category.size()", equalTo(4)). appendRoot("%s.%s", withArgs("author", "size()")). body(withNoArgs(), equalTo(4));
public static RequestSpecification given()
given().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML").then().body("greeting.firstName", equalTo("John"));will send a POST request to "/greetXML" with request parameters firstName=John and lastName=Doe and expect that the response body containing JSON or XML firstName equal to John. The only difference between
with()
and given()
is syntactical.public static RequestSender when()
when(). get("/x"). then(). body("x.y.z1", equalTo("Z1")). body("x.y.z2", equalTo("Z2"));
Note that if you need to add parameters, headers, cookies or other request properties use the given()
method.
public static RequestSender given(RequestSpecification requestSpecification, ResponseSpecification responseSpecification)
RequestSpecification requestSpecification = with().parameters("firstName", "John", "lastName", "Doe"); ResponseSpecification responseSpecification = expect().body("greeting", equalTo("Greetings John Doe")); given(requestSpecification, responseSpecification).get("/greet");This will perform a GET request to "/greet" and verify it according to the
responseSpecification
.public static RequestSpecification given(RequestSpecification requestSpecification)
RequestSpecification requestSpecification = with().parameters("firstName", "John", "lastName", "Doe"); given(requestSpecification).get("/greet"). ..;This will perform a GET request to "/greet" and without any validation (only a static response specification has been configured).
public static Response get(String path, Object... pathParams)
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.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);
.public static Response get(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response post(String path, Object... pathParams)
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.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);
.public static Response post(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response put(String path, Object... pathParams)
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.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);
.public static Response delete(String path, Object... pathParams)
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.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);
.public static Response delete(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response head(String path, Object... pathParams)
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.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);
.public static Response head(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response patch(String path, Object... pathParams)
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.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);
.public static Response patch(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response options(String path, Object... pathParams)
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.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);
.public static Response options(String path, Map<String,?> pathParams)
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.path
- The path to send the request to.pathParams
- The path parameters.public static Response get(URI uri)
uri
.uri
- The uri to send the request to.public static Response post(URI uri)
uri
.uri
- The uri to send the request to.public static Response put(URI uri)
uri
.uri
- The uri to send the request to.public static Response delete(URI uri)
uri
.uri
- The uri to send the request to.public static Response head(URI uri)
uri
.uri
- The uri to send the request to.public static Response patch(URI uri)
uri
.uri
- The uri to send the request to.public static Response options(URI uri)
uri
.uri
- The uri to send the request to.public static Response get(URL url)
url
.url
- The url to send the request to.public static Response post(URL url)
url
.url
- The url to send the request to.public static Response put(URL url)
url
.url
- The url to send the request to.public static Response delete(URL url)
url
.url
- The url to send the request to.public static Response head(URL url)
url
.url
- The url to send the request to.public static Response patch(URL url)
url
.url
- The url to send the request to.public static Response options(URL url)
url
.url
- The url to send the request to.public static Response get()
http://localhost:8080
).public static Response post()
http://localhost:8080
).public static Response put()
http://localhost:8080
).public static Response delete()
http://localhost:8080
).public static Response head()
http://localhost:8080
).public static Response patch()
http://localhost:8080
).public static Response options()
http://localhost:8080
).public static Response request(Method method)
http://localhost:8080
).method
- The HTTP method to usepublic static Response request(String method)
http://localhost:8080
).method
- The HTTP method to usepublic static Response request(Method method, String path, Object... pathParams)
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.method
- The HTTP method to usepath
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do request(Method.TRACE,"/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static Response request(String method, String path, Object... pathParams)
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.method
- The HTTP method to usepath
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do request("method","/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static Response request(Method method, URI uri)
uri
.method
- The HTTP method to useuri
- The uri to send the request to.public static Response request(Method method, URL url)
url
.method
- The HTTP method to useurl
- The url to send the request to.public static Response request(String method, URI uri)
uri
.method
- The HTTP method to useuri
- The uri to send the request to.public static Response request(String method, URL url)
url
.method
- The HTTP method to useurl
- The url to send the request to.public static AuthenticationScheme basic(String userName, String password)
userName
- The user name.password
- The password.public static AuthenticationScheme ntlm(String userName, String password, String workstation, String domain)
userName
- The user name.password
- The password.workstation
- The NTLM workstation.domain
- The NTLM workstation.public static AuthenticationScheme form(String userName, String password)
Note that the request will be much faster if you also supply a form auth configuration.
userName
- The user name.password
- The password.form(String, String, FormAuthConfig)
public static AuthenticationScheme form(String userName, String password, FormAuthConfig config)
userName
- The user name.password
- The password.config
- The form authentication configpublic static PreemptiveAuthProvider preemptive()
public static AuthenticationScheme certificate(String certURL, String password)
Class.getResource(String)
for how to get a URL from a resource on the classpath.
Uses SSL settings defined in SSLConfig
.
certURL
- URL to a JKS keystore where the certificate is stored.password
- The password for the keystorepublic static AuthenticationScheme certificate(String certURL, String password, CertificateAuthSettings certificateAuthSettings)
Class.getResource(String)
for how to get a URL from a resource
on the classpath.
certURL
- URL to a JKS keystore where the certificate is stored.password
- The password for the keystorecertificateAuthSettings
- More advanced settings for the certificate authenticationpublic static AuthenticationScheme certificate(String trustStorePath, String trustStorePassword, String keyStorePath, String keyStorePassword, CertificateAuthSettings certificateAuthSettings)
Class.getResource(String)
for how to get a URL from a resource
on the classpath.
trustStorePath
- URL to a JKS trust store where the certificate is stored.trustStorePassword
- The password for the trust storekeyStorePath
- URL to a JKS keystore where the certificate is stored.keyStorePassword
- The password for the keystorecertificateAuthSettings
- More advanced settings for the certificate authenticationpublic static AuthenticationScheme digest(String userName, String password)
userName
- The user name.password
- The password.public static AuthenticationScheme oauth(String consumerKey, String consumerSecret, String accessToken, String secretToken)
consumerKey
- consumerSecret
- accessToken
- secretToken
- public static AuthenticationScheme oauth(String consumerKey, String consumerSecret, String accessToken, String secretToken, OAuthSignature signature)
consumerKey
- consumerSecret
- accessToken
- secretToken
- signature
- public static AuthenticationScheme oauth2(String accessToken)
accessToken
- The access token to usepublic static AuthenticationScheme oauth2(String accessToken, OAuthSignature signature)
accessToken
- signature
- public static void registerParser(String contentType, Parser parser)
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:
RestAssured.registerParser("application/custom, Parser.XML");
contentType
- The content-type to registerparser
- The parser to use when verifying the response.public static void unregisterParser(String contentType)
contentType
- The content-type associated with the parser to unregister.public static void reset()
baseURI
, basePath
, port
, authentication
and rootPath
,
filters(java.util.List)
, requestSpecification
, responseSpecification
,
urlEncodingEnabled
, config
, sessionId
and proxy
to their default values of "http://localhost", "", -1,
no authentication
, <empty string>, null
, null
,
<empty list>, null
, null
, none
, true
, new RestAssuredConfig()
, null
and null
.public static void useRelaxedHTTPSValidation()
keyStore(String, String)
or trust store (see trustStore(java.security.KeyStore)
.
This is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().relaxedHTTPSValidation());
public static void useRelaxedHTTPSValidation(String protocol)
keyStore(String, String)
or trust store (see trustStore(java.security.KeyStore)
.
This is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().relaxedHTTPSValidation(<protocol>));
protocol
- The standard name of the requested protocol. See the SSLContext section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard protocol names.public static void enableLoggingOfRequestAndResponseIfValidationFails()
LogDetail.ALL
.
This is just a shortcut for:
RestAssured.config = RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails());
public static void enableLoggingOfRequestAndResponseIfValidationFails(LogDetail logDetail)
This is just a shortcut for:
RestAssured.config = RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
logDetail
- The log detail to show in the logpublic static void keyStore(String pathToJks, String password)
given().keyStore("/truststore_javanet.jks", "test1234"). ..
Note that this is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().keyStore(pathToJks, password));
pathToJks
- The path to the JKS. REST Assured will first look in the classpath and if not found it will look for the JKS in the local file-systempassword
- The store passpublic static void trustStore(String pathToJks, String password)
$ keytool -printcert -file EquifaxSecureGlobaleBusinessCA-1.crt Owner: CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US Issuer: CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US Serial number: 1 Valid from: Mon Jun 21 00:00:00 EDT 1999 until: Sun Jun 21 00:00:00 EDT 2020 Certificate fingerprints: MD5: 8F:5D:77:06:27:C4:98:3C:5B:93:78:E7:D7:7D:9B:CC SHA1: 7E:78:4A:10:1C:82:65:CC:2D:E1:F1:6D:47:B4:40:CA:D9:0A:19:45 Signature algorithm name: MD5withRSA Version: 3 ....Now, import that into a Java keystore file:
$ keytool -importcert -alias "equifax-ca" -file EquifaxSecureGlobaleBusinessCA-1.crt -keystore truststore_javanet.jks -storepass test1234 Owner: CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US Issuer: CN=Equifax Secure Global eBusiness CA-1, O=Equifax Secure Inc., C=US Serial number: 1 Valid from: Mon Jun 21 00:00:00 EDT 1999 until: Sun Jun 21 00:00:00 EDT 2020 Certificate fingerprints: MD5: 8F:5D:77:06:27:C4:98:3C:5B:93:78:E7:D7:7D:9B:CC SHA1: 7E:78:4A:10:1C:82:65:CC:2D:E1:F1:6D:47:B4:40:CA:D9:0A:19:45 Signature algorithm name: MD5withRSA Version: 3 ... Trust this certificate? [no]: yes Certificate was added to keystoreNow you want to use this truststore in your client:
RestAssured.trustSture("/truststore_javanet.jks", "test1234");or
given().trustStore("/truststore_javanet.jks", "test1234"). ..
Note that this is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().trustStore(pathToJks, password));
pathToJks
- The path to the JKS. REST Assured will first look in the classpath and if not found it will look for the JKS in the local file-systempassword
- The store passpublic static void trustStore(KeyStore truststore)
KeyStore
that has been loaded with the password.
If you wish that REST Assured loads the KeyStore store and applies the password (thus making it a trust store) please see some of the
keystore
methods such as keyStore(java.io.File, String)
.truststore
- A pre-loaded KeyStore
.keyStore(String, String)
public static void keyStore(File pathToJks, String password)
keyStore(String, String)
for more details.
* Note that this is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().keyStore(pathToJks, password));
pathToJks
- The path to JKS file on the file-systempassword
- The password for the keystorekeyStore(String, String)
public static void trustStore(File pathToJks, String password)
trustStore(String, String)
for more details.
* Note that this is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().trustStore(pathToJks, password));
pathToJks
- The path to JKS file on the file-systempassword
- The password for the keystorekeyStore(String, String)
public static void keyStore(String password)
Note that this is just a shortcut for:
RestAssured.config = RestAssured.config().sslConfig(sslConfig().keyStore(password));
password
- - Use null for no passwordpublic static void proxy(String host, int port)
host
- The hostname of the proxy to connect to (for example 127.0.0.1
)port
- The port of the proxy to connect to (for example 8888
)public static void proxy(String host)
8888
.host
- The hostname of the proxy to connect to (for example 127.0.0.1
). Can also be a URI represented as a String.proxy(String, int)
public static void proxy(int port)
port
- The port of the proxy to connect to (for example 8888
)proxy(String, int)
public static void proxy(String host, int port, String scheme)
host
- The hostname of the proxy to connect to (for example 127.0.0.1
)port
- The port of the proxy to connect to (for example 8888
)scheme
- The http scheme (http or https)public static void proxy(URI uri)
uri
- The URI of the proxypublic static void proxy(ProxySpecification proxySpecification)
ProxySpecification
.proxySpecification
- The proxy specification to use.RequestSpecification.proxy(ProxySpecification)
public static RestAssuredConfig config()
Copyright © 2010–2023. All rights reserved.