REST-assured方便 REST 服务测试的 JAVA DSL
REST-assured 用于方便 REST 服务测试的 JAVA DSL。
举例如下:
Here's an example of how to make a GET request and validate the JSON or XML response:
get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));
Get and verify all winner ids:
get("/lotto").then().assertThat().body("lotto.winners.winnerId", hasItems(23, 54));
Using parameters:
given(). param("key1", "value1"). param("key2", "value2"). when(). post("/somewhere"). then(). body(containsString("OK"));
Using X-Path (XML only):
given(). parameters("firstName", "John", "lastName", "Doe"). when(). post("/greetMe"). then(). body(hasXPath("/greeting/firstName[text()='John']")).
Need authentication? REST Assured provides several authentication mechanisms:
given().auth().basic(username, password).when().get("/secured").then().statusCode(200);
Getting and parsing a response body:
// Example with JsonPath String json = get("/lotto").asString() List<String> winnderIds = from(json).get("lotto.winners.winnerId"); // Example with XmlPath String xml = post("/shopping").andReturn().body().asString() Node category = from(xml).get("shopping.category[0]");
REST Assured支持任何HTTP方法,但显式支持POST,GET,PUT,DELETE,OPTIONS,PATCH和HEAD,并且包括轻松指定和验证参数,标头,cookie和主体。
评论