|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jayway.restassured.RestAssured
public class RestAssured
REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE and HEAD requests and to verify the response of these requests. Usage examples:
{ "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:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");or perhaps you want to check that the winnerId's are 23 and 54:
expect().body("lotto.winners.winnerId", hasItems(23, 54)).when().get("/lotto");
<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").expect().body("greeting.firstName", equalTo("John")).when().post("/greetXML");If you want to verify both firstName and lastName you may do like this:
with().parameters("firstName", "John", "lastName", "Doe").expect().body("greeting.firstName", equalTo("John")).and().body("greeting.lastName", equalTo("Doe")).when().post("/greetXML");or a little shorter:
with().parameters("firstName", "John", "lastName", "Doe").expect().body("greeting.firstName", equalTo("John"), "greeting.lastName", equalTo("Doe")).when().post("/greetXML");
expect().body(hasXPath("/greeting/firstName", containsString("Jo"))).given().parameters("firstName", "John", "lastName", "Doe").when().post("/greetXML");or
expect().body(hasXPath("/greeting/firstName[text()='John']")).with().parameters("firstName", "John", "lastName", "Doe").post("/greetXML");
given().cookie("username", "John").then().expect().body(equalTo("username")).when().get("/cookie");
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")). ..
expect().statusCode(200). .. expect().statusLine("something"). .. expect().statusLine(containsString("some")). ..
expect().header("headerName", "headerValue"). .. expect().headers("headerName1", "headerValue1", "headerName2", "headerValue2"). .. expect().headers("headerName1", "headerValue1", "headerName2", containsString("Value2")). ..
expect().contentType(ContentType.JSON). ..
expect().body(equalsTo("something")). .. expect().content(equalsTo("something")). .. // Same as above
given().auth().basic("username", "password").expect().statusCode(200).when().get("/secured/hello");Other supported schemes are OAuth and certificate authentication.
given().port(80). ..or simply:
.. when().get("http://myhost.org:80/doSomething");You can also change the default base URI and port for all subsequent requests:
RestAssured.baseURI = "http://myhost.org"; RestAssured.port = 80;This means that a request like e.g.
get("/hello")
goes to: http://myhost.org:8080/helloRestAssured.reset();
In order to use REST assured effectively it's recommended to statically import methods from the following classes:
Field Summary | |
---|---|
static java.lang.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 int |
DEFAULT_PORT
|
static java.lang.String |
DEFAULT_URI
|
static int |
port
The port that's used by REST assured when is left out of the specified URI when making a request. |
Constructor Summary | |
---|---|
RestAssured()
|
Method Summary | |
---|---|
static ResponseSpecification |
expect()
Start building the response part of the test specification. |
static RequestSpecification |
given()
Start building the request part of the test specification. |
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 void |
reset()
Reset the baseURI and port to their default values of "http://localhost" and 8080. |
static RequestSpecification |
with()
Start building the request part of the test specification. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_URI
public static final int DEFAULT_PORT
public static java.lang.String baseURI
public static int port
Constructor Detail |
---|
public RestAssured()
Method Detail |
---|
public 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").expect().body("greeting.firstName", equalTo("John")).when().post("/greetXML");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 RequestSpecification given()
given().parameters("firstName", "John", "lastName", "Doe").expect().body("greeting.firstName", equalTo("John")).when().post("/greetXML");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 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 void reset()
baseURI
and port
to their default values of "http://localhost" and 8080.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |