com.jayway.restassured.specification
Interface RequestSpecification

All Superinterfaces:
RequestSender
All Known Subinterfaces:
FilterableRequestSpecification
All Known Implementing Classes:
RequestSpecificationImpl

public interface RequestSpecification
extends RequestSender

Allows you to specify how the request will look like.


Method Summary
 RequestSpecification and()
          Syntactic sugar, e.g.
 AuthenticationSpecification auth()
          A slightly short version of authentication().
 AuthenticationSpecification authentication()
          If you need to specify some credentials when performing a request.
 RequestSpecification body(byte[] body)
          Specify a byte array request body that'll be sent with the request.
 RequestSpecification body(String body)
          Specify a String request body (such as e.g.
 RequestSpecification content(byte[] content)
          Specify a byte array request content that'll be sent with the request.
 RequestSpecification content(String content)
          Specify a String request content (such as e.g.
 RequestSpecification contentType(groovyx.net.http.ContentType contentType)
          Specify the content type of the request.
 RequestSpecification contentType(String contentType)
          Specify the content type of the request.
 RequestSpecification cookie(String cookieName)
          Specify a cookie with no value that'll be sent with the request e.g:
 RequestSpecification cookie(String cookieName, String value)
          Specify a cookie that'll be sent with the request e.g:
 RequestSpecification cookies(Map<String,String> cookies)
          Specify the cookies that'll be sent with the request as Map e.g:
 RequestSpecification cookies(String cookieName, String... cookieNameValuePairs)
          Specify the cookies that'll be sent with the request.
 ResponseSpecification expect()
          Returns the response specification so that you can setup the expectations on the response.
 RequestSpecification filter(Filter filter)
          Add a filter that will be used in the request.
 RequestSpecification filters(List<Filter> filters)
          Add filters that will be used in the request.
 RequestSpecification given()
          Syntactic sugar, e.g.
 RequestSpecification header(String headerName, String headerValue)
          Specify a header that'll be sent with the request e.g:
 RequestSpecification headers(Map<String,String> headers)
          Specify the headers that'll be sent with the request as Map e.g:
 RequestSpecification headers(String headerName, String... headerNameValuePairs)
          Specify the headers that'll be sent with the request.
 RequestSpecification log()
          Log (i.e.
 RequestSpecification logOnError()
          Log (i.e.
 RequestSpecification param(String parameterName, List<String> parameterValues)
          A slightly shorter version of parameter(String, java.util.List) }.
 RequestSpecification param(String parameterName, String parameterValue, String... additionalParameterValues)
          A slightly shorter version of parameter(String, String, String...).
 RequestSpecification parameter(String parameterName, List<String> parameterValues)
          Specify a multi-value parameter that'll be sent with the request e.g:
 RequestSpecification parameter(String parameterName, String parameterValue, String... additionalParameterValues)
          Specify a parameter that'll be sent with the request e.g:
 RequestSpecification parameters(Map<String,String> parametersMap)
          Specify the parameters that'll be sent with the request as Map e.g:
 RequestSpecification parameters(String parameterName, String... parameterNameValuePairs)
          Specify the parameters that'll be sent with the request.
 RequestSpecification params(Map<String,String> parametersMap)
          A slightly shorter version of parameters(Map).
 RequestSpecification params(String parameterName, String... parameterNameValuePairs)
          A slightly shorter version of parameters(String, String...).
 RequestSpecification port(int port)
          Specify the port of the URI.
 RequestSpecification queryParam(String parameterName, List<String> parameterValues)
          A slightly shorter version of queryParameter(String, java.util.List).
 RequestSpecification queryParam(String parameterName, String parameterValue, String... additionalParameterValues)
          A slightly shorter version of queryParameter(String, String, String...).
 RequestSpecification queryParameter(String parameterName, List<String> parameterValues)
          Specify a multi-value query parameter that'll be sent with the request e.g:
 RequestSpecification queryParameter(String parameterName, String parameterValue, String... additionalParameterValues)
          Specify a query parameter that'll be sent with the request.
 RequestSpecification queryParameters(Map<String,String> parametersMap)
          Specify the query parameters that'll be sent with the request.
 RequestSpecification queryParameters(String parameterName, String... parameterNameValuePairs)
          Specify the query parameters that'll be sent with the request.
 RequestSpecification queryParams(Map<String,String> parametersMap)
          A slightly shorter version of queryParams(java.util.Map).
 RequestSpecification queryParams(String parameterName, String... parameterNameValuePairs)
          A slightly shorter version of queryParameters(String, String...).
 RequestSpecification request()
          Syntactic sugar, e.g.
 ResponseSpecification response()
          Returns the response specification so that you can setup the expectations on the response.
 RequestSpecification spec(RequestSpecification requestSpecificationToMerge)
          Add request data from a pre-defined specification.
 RequestSpecification specification(RequestSpecification requestSpecificationToMerge)
          Add request data from a pre-defined specification.
 RequestSpecification that()
          Syntactic sugar, e.g.
 ResponseSpecification then()
          Returns the response specification so that you can setup the expectations on the response.
 RequestSpecification when()
          Syntactic sugar, e.g.
 RequestSpecification with()
          Syntactic sugar, e.g.
 
Methods inherited from interface com.jayway.restassured.specification.RequestSender
delete, get, head, post, put
 

Method Detail

body

RequestSpecification body(String body)
Specify a String request body (such as e.g. JSON or XML) that'll be sent with the request. This works for the POST and PUT methods only. Trying to do this for the other http methods will cause an exception to be thrown.

Example of use:

 given().body("{ \"message\" : \"hello world\"}").then().expect().body(equalTo("hello world")).when().post("/json");
 
This will POST a request containing JSON to "/json" and expect that the response body equals to "hello world".

Note that body(String) and content(String) are the same except for the syntactic difference.

Parameters:
body - The body to send.
Returns:
The request specification

body

RequestSpecification body(byte[] body)
Specify a byte array request body that'll be sent with the request. This only works for the POST http method. Trying to do this for the other http methods will cause an exception to be thrown.

Example of use:

 byte[] someBytes = ..
 given().body(someBytes).then().expect().body(equalTo("hello world")).when().post("/json");
 
This will POST a request containing someBytes to "/json" and expect that the response body equals to "hello world".

Note that body(byte[]) and content(byte[]) are the same except for the syntactic difference.

Parameters:
body - The body to send.
Returns:
The request specification

content

RequestSpecification content(String content)
Specify a String request content (such as e.g. JSON or XML) that'll be sent with the request. This works for the POST and PUT methods only. Trying to do this for the other http methods will cause an exception to be thrown.

Example of use:

 given().content("{ \"message\" : \"hello world\"}").then().expect().content(equalTo("hello world")).when().post("/json");
 
This will POST a request containing JSON to "/json" and expect that the response content equals to "hello world".

Note that body(String) and content(String) are the same except for the syntactic difference.

Parameters:
content - The content to send.
Returns:
The request specification

content

RequestSpecification content(byte[] content)
Specify a byte array request content that'll be sent with the request. This only works for the POST http method. Trying to do this for the other http methods will cause an exception to be thrown.

Example of use:

 byte[] someBytes = ..
 given().content(someBytes).then().expect().content(equalTo("hello world")).when().post("/json");
 
This will POST a request containing someBytes to "/json" and expect that the response content equals to "hello world".

Note that body(byte[]) and content(byte[]) are the same except for the syntactic difference.

Parameters:
content - The content to send.
Returns:
The request specification

cookies

RequestSpecification cookies(String cookieName,
                             String... cookieNameValuePairs)
Specify the cookies that'll be sent with the request. This is done by specifying the cookies in name-value pairs, e.g:
 given().cookies("username", "John", "token", "1234").then().expect().body(equalTo("username, token")).when().get("/cookie");
 
This will send a GET request to "/cookie" with two cookies:
  1. username=John
  2. token=1234
and expect that the response body is equal to "username, token".

Parameters:
cookieName - The name of the first cookie
cookieNameValuePairs - The value of the first cookie followed by additional cookies in name-value pairs.
Returns:
The request specification

cookies

RequestSpecification cookies(Map<String,String> cookies)
Specify the cookies that'll be sent with the request as Map e.g:
 Map<String, String> cookies = new HashMap<String, String>();
 cookies.put("username", "John");
 cookies.put("token", "1234");
 given().cookies(cookies).then().expect().body(equalTo("username, token")).when().get("/cookie");
 
This will send a GET request to "/cookie" with two cookies:
  1. username=John
  2. token=1234
and expect that the response body is equal to "username, token".

Parameters:
cookies - The Map containing the cookie names and their values to set in the request.
Returns:
The request specification

cookie

RequestSpecification cookie(String cookieName,
                            String value)
Specify a cookie that'll be sent with the request e.g:

 given().cookie("username", "John").and().expect().body(equalTo("username")).when().get("/cookie");
 
This will set the cookie username=John in the GET request to "/cookie".

You can also specify several cookies like this:

 given().cookie("username", "John").and().cookie("password", "1234").and().expect().body(equalTo("username")).when().get("/cookie");
 

Parameters:
cookieName - The cookie cookieName
value - The cookie value
Returns:
The request specification
See Also:
cookies(String, String...)

cookie

RequestSpecification cookie(String cookieName)
Specify a cookie with no value that'll be sent with the request e.g:

 given().cookie("some_cookie"").and().expect().body(equalTo("x")).when().get("/cookie");
 
This will set the cookie some_cookie in the GET request to "/cookie".

Parameters:
cookieName - The cookie cookieName
Returns:
The request specification
See Also:
cookies(String, String...)

parameters

RequestSpecification parameters(String parameterName,
                                String... parameterNameValuePairs)
Specify the parameters that'll be sent with the request. This is done by specifying the parameters in name-value pairs, e.g:
 given().parameters("username", "John", "token", "1234").then().expect().body(equalTo("username, token")).when().get("/parameters");
 
This will send a GET request to "/parameters" with two parameters:
  1. username=John
  2. token=1234
and expect that the response body is equal to "username, token".

Parameters:
parameterName - The name of the first parameter
parameterNameValuePairs - The value of the first parameter followed by additional parameters in name-value pairs.
Returns:
The request specification

parameters

RequestSpecification parameters(Map<String,String> parametersMap)
Specify the parameters that'll be sent with the request as Map e.g:
 Map<String, String> parameters = new HashMap<String, String>();
 parameters.put("username", "John");
 parameters.put("token", "1234");
 given().parameters(parameters).then().expect().body(equalTo("username, token")).when().get("/cookie");
 
This will send a GET request to "/cookie" with two parameters:
  1. username=John
  2. token=1234
and expect that the response body is equal to "username, token".

Parameters:
parametersMap - The Map containing the parameter names and their values to send with the request.
Returns:
The request specification

parameter

RequestSpecification parameter(String parameterName,
                               String parameterValue,
                               String... additionalParameterValues)
Specify a parameter that'll be sent with the request e.g:

 given().parameter("username", "John").and().expect().body(equalTo("username")).when().get("/cookie");
 
This will set the parameter username=John in the GET request to "/cookie".

You can also specify several parameters like this:

 given().parameter("username", "John").and().parameter("password", "1234").and().expect().body(equalTo("username")).when().get("/cookie");
 

Parameters:
parameterName - The parameter key
parameterValue - The parameter value
additionalParameterValues - Additional parameter values if you want to specify multiple values for the same parameter
Returns:
The request specification
See Also:
parameters(String, String...)

parameter

RequestSpecification parameter(String parameterName,
                               List<String> parameterValues)
Specify a multi-value parameter that'll be sent with the request e.g:

 given().parameter("cars", asList("Volvo", "Saab"))..;
 
This will set the parameter cars=Volvo and cars=Saab.

Parameters:
parameterName - The parameter key
parameterValues - The parameter values
Returns:
The request specification

params

RequestSpecification params(String parameterName,
                            String... parameterNameValuePairs)
A slightly shorter version of parameters(String, String...).

Parameters:
parameterName - The name of the first parameter
parameterNameValuePairs - The value of the first parameter followed by additional parameters in name-value pairs.
Returns:
The request specification
See Also:
parameters(String, String...)

params

RequestSpecification params(Map<String,String> parametersMap)
A slightly shorter version of parameters(Map).

Parameters:
parametersMap - The Map containing the parameter names and their values to send with the request.
Returns:
The request specification
See Also:
parameters(Map)

param

RequestSpecification param(String parameterName,
                           String parameterValue,
                           String... additionalParameterValues)
A slightly shorter version of parameter(String, String, String...).

Parameters:
parameterName - The parameter key
parameterValue - The parameter value
additionalParameterValues - Additional parameter values if you want to specify multiple values for the same parameter
Returns:
The request specification
See Also:
parameter(String, String, String...)

param

RequestSpecification param(String parameterName,
                           List<String> parameterValues)
A slightly shorter version of parameter(String, java.util.List) }.

Parameters:
parameterName - The parameter key
parameterValues - The parameter values
Returns:
The request specification

queryParameters

RequestSpecification queryParameters(String parameterName,
                                     String... parameterNameValuePairs)
Specify the query parameters that'll be sent with the request. Note that this method is the same as parameters(String, String...) for all http methods except for POST where parameters(String, String...) sets the form parameters and this method sets the query parameters.

Parameters:
parameterName - The name of the first parameter
parameterNameValuePairs - The value of the first parameter followed by additional parameters in name-value pairs.
Returns:
The request specification

queryParameters

RequestSpecification queryParameters(Map<String,String> parametersMap)
Specify the query parameters that'll be sent with the request. Note that this method is the same as parameters(Map) for all http methods except for POST where parameters(Map) sets the form parameters and this method sets the query parameters.

Parameters:
parametersMap - The Map containing the parameter names and their values to send with the request.
Returns:
The request specification

queryParameter

RequestSpecification queryParameter(String parameterName,
                                    String parameterValue,
                                    String... additionalParameterValues)
Specify a query parameter that'll be sent with the request. Note that this method is the same as parameter(String, String, String...) for all http methods except for POST where parameter(String, String, String...) adds a form parameter and this method sets a query parameter.

Parameters:
parameterName - The parameter key
parameterValue - The parameter value
additionalParameterValues - Additional parameter values if you want to specify multiple values for the same parameter
Returns:
The request specification
See Also:
parameter(String, String, String...)

queryParameter

RequestSpecification queryParameter(String parameterName,
                                    List<String> parameterValues)
Specify a multi-value query parameter that'll be sent with the request e.g:

 given().queryParameter("cars", asList("Volvo", "Saab"))..;
 
This will set the parameter cars=Volvo and cars=Saab.

Note that this method is the same as parameter(String, java.util.List) for all http methods except for POST where parameter(String, java.util.List) adds a form parameter and this method sets a query parameter.

Parameters:
parameterName - The parameter key
parameterValues - The parameter values
Returns:
The request specification

queryParams

RequestSpecification queryParams(String parameterName,
                                 String... parameterNameValuePairs)
A slightly shorter version of queryParameters(String, String...).

Parameters:
parameterName - The name of the first parameter
parameterNameValuePairs - The value of the first parameter followed by additional parameters in name-value pairs.
Returns:
The request specification
See Also:
queryParameters(String, String...)

queryParams

RequestSpecification queryParams(Map<String,String> parametersMap)
A slightly shorter version of queryParams(java.util.Map).

Parameters:
parametersMap - The Map containing the parameter names and their values to send with the request.
Returns:
The request specification
See Also:
queryParams(java.util.Map)

queryParam

RequestSpecification queryParam(String parameterName,
                                String parameterValue,
                                String... additionalParameterValues)
A slightly shorter version of queryParameter(String, String, String...).

Parameters:
parameterName - The parameter key
parameterValue - The parameter value
additionalParameterValues - Additional parameter values if you want to specify multiple values for the same parameter
Returns:
The request specification
See Also:
parameter(String, String, String...)

queryParam

RequestSpecification queryParam(String parameterName,
                                List<String> parameterValues)
A slightly shorter version of queryParameter(String, java.util.List).

Parameters:
parameterName - The parameter key
parameterValues - The parameter values
Returns:
The request specification
See Also:
queryParam(String, java.util.List)

headers

RequestSpecification headers(String headerName,
                             String... headerNameValuePairs)
Specify the headers that'll be sent with the request. This is done by specifying the headers in name-value pairs, e.g:
 given().headers("headerName1", "headerValue1", "headerName2", "headerValue2").then().expect().body(equalTo("something")).when().get("/headers");
 
This will send a GET request to "/headers" with two headers:
  1. headerName1=headerValue1
  2. headerName2=headerValue2
and expect that the response body is equal to "something".

Parameters:
headerName - The name of the first header
headerNameValuePairs - The value of the first header followed by additional headers in name-value pairs.
Returns:
The request specification

headers

RequestSpecification headers(Map<String,String> headers)
Specify the headers that'll be sent with the request as Map e.g:
 Map<String, String> headers = new HashMap<String, String>();
 parameters.put("headerName1", "headerValue1");
 parameters.put("headerName2", "headerValue2");
 given().headers(headers).then().expect().body(equalTo("something")).when().get("/headers");
 
This will send a GET request to "/headers" with two headers:
  1. headerName1=headerValue1
  2. headerName2=headerValue2
and expect that the response body is equal to "something".

Parameters:
headers - The Map containing the header names and their values to send with the request.
Returns:
The request specification

header

RequestSpecification header(String headerName,
                            String headerValue)
Specify a header that'll be sent with the request e.g:

 given().header("username", "John").and().expect().body(equalTo("something")).when().get("/header");
 
This will set the header username=John in the GET request to "/header".

You can also specify several headers like this:

 given().header("username", "John").and().header("zipCode", "12345").and().expect().body(equalTo("something")).when().get("/header");
 

Parameters:
headerName - The header name
headerValue - The header value
Returns:
The request specification
See Also:
headers(String, String...)

contentType

RequestSpecification contentType(groovyx.net.http.ContentType contentType)
Specify the content type of the request.

Parameters:
contentType - The content type of the request
Returns:
The request specification
See Also:
ContentType

contentType

RequestSpecification contentType(String contentType)
Specify the content type of the request.

Parameters:
contentType - The content type of the request
Returns:
The request specification
See Also:
ContentType

authentication

AuthenticationSpecification authentication()
If you need to specify some credentials when performing a request.

Returns:
The authentication specification
See Also:
AuthenticationSpecification

auth

AuthenticationSpecification auth()
A slightly short version of authentication().

Returns:
The authentication specification
See Also:
authentication(), AuthenticationSpecification

port

RequestSpecification port(int port)
Specify the port of the URI. E.g.

 given().port(8081).and().expect().statusCode(200).when().get("/something");
 
will perform a GET request to http;//localhost:8081/something. It will override the default port of REST assured for this request only.

Note that it's also possible to specify the port like this:

 expect().statusCode(200).when().get("http://localhost:8081/something");
 

Parameters:
port - The port of URI
Returns:
The request specification

spec

RequestSpecification spec(RequestSpecification requestSpecificationToMerge)
Add request data from a pre-defined specification. E.g.
 RequestSpecification requestSpec = new RequestSpecBuilder().addParam("parameter1", "value1").build();

 given().
         spec(requestSpec).
         param("parameter2", "value2").
 when().
        get("/something");
 
This is useful when you want to reuse an entire specification across multiple requests.

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

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

Parameters:
requestSpecificationToMerge - The specification to merge with.
Returns:
the request specification

specification

RequestSpecification specification(RequestSpecification requestSpecificationToMerge)
Add request data from a pre-defined specification. E.g.
 RequestSpecification requestSpec = new RequestSpecBuilder().addParam("parameter1", "value1").build();

 given().
         spec(requestSpec).
         param("parameter2", "value2").
 when().
        get("/something");
 
This is useful when you want to reuse an entire specification across multiple requests.

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

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

Parameters:
requestSpecificationToMerge - The specification to merge with.
Returns:
the request specification

filter

RequestSpecification filter(Filter filter)
Add a filter that will be used in the request.

Parameters:
filter - The filter to add
Returns:
the request specification

filters

RequestSpecification filters(List<Filter> filters)
Add filters that will be used in the request.

Parameters:
filters - The filters to add
Returns:
the request specification

log

RequestSpecification log()
Log (i.e. print to system out) the response body to system out. This is mainly useful for debug purposes when writing your tests. A shortcut for:
 given().filter(ResponseLoggingFilter.responseLogger()). ..
 

Returns:
the request specification

logOnError

RequestSpecification logOnError()
Log (i.e. print to system out) the response body to system out if an error occurs. This is mainly useful for debug purposes when writing your tests. A shortcut for:
 given().filter(ErrorLoggingFilter.errorLogger()). ..
 

Returns:
the request specification

response

ResponseSpecification response()
Returns the response specification so that you can setup the expectations on the response. E.g.
 given().param("name", "value").then().response().body(equalTo("something")).when().get("/something");
 

Returns:
the response specification

and

RequestSpecification and()
Syntactic sugar, e.g.
 expect().body(containsString("OK")).and().body(containsString("something else")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).body(containsString("something else")).when().get("/something");
 

Returns:
the request specification

with

RequestSpecification with()
Syntactic sugar, e.g.
 expect().body(containsString("OK")).and().with().request().parameters("param1", "value1").get("/something");
 
is that same as:
 expect().body(containsString("OK")).and().request().parameters("param1", "value1").get("/something");
 

Returns:
the request specification

then

ResponseSpecification then()
Returns the response specification so that you can setup the expectations on the response. E.g.
 given().param("name", "value").then().body(equalTo("something")).when().get("/something");
 

Returns:
the response specification

expect

ResponseSpecification expect()
Returns the response specification so that you can setup the expectations on the response. E.g.
 given().param("name", "value").and().expect().body(equalTo("something")).when().get("/something");
 

Returns:
the response specification

when

RequestSpecification when()
Syntactic sugar, e.g.
 expect().body(containsString("OK")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Returns:
the request specification

given

RequestSpecification given()
Syntactic sugar, e.g.
 given().param("name1", "value1").and().given().param("name2", "value2").when().get("/something");
 
is that same as:
 given().param("name1", "value1").and().param("name2", "value2").when().get("/something");
 

Returns:
the request specification

that

RequestSpecification that()
Syntactic sugar, e.g.
 expect().that().body(containsString("OK")).when().get("/something");
 
is that same as:
 expect().body(containsString("OK")).get("/something");
 

Returns:
the request specification

request

RequestSpecification request()
Syntactic sugar, e.g.
 given().request().param("name", "John").then().expect().body(containsString("OK")).when().get("/something");
 
is that same as:
 given().param("name", "John").then().expect().body(containsString("OK")).when().get("/something");
 

Returns:
the request specification


Copyright © 2010-2011. All Rights Reserved.