RestAssured(java的api测试工具)
1. 定义
--REST Assured是一个开源的java的api测试工具
--REST Assured是一个Java库,它提供了一种领域特定语言(DSL),用于为RESTful api编写强大的、可维护的测试
--支持post,get,put,delete,options,patch和head请求及验证这些请求的响应
2. REST Assured的使用
--Maven configuration
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.0</version>
</dependency>
--类中静态导入方法,以提高使用rest-assured的效率
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import static io.restassured.module.jsv.JsonSchemaValidator.*;
--返回的响应一般有两种格式 json / xml
--指定请求数据
(1)指定路径参数
RequestSpecification httpRequest = RestAssured.given().
baseUri("http://localhost:1080/test01").
pathParam("type", "GUID").pathParam("cam", 30);
(2)指定参数
RequestSpecification httpRequest = RestAssured.given().
baseUri("http://localhost:1080/test01").
param("param1","value1"). param("param2","value2"); /
formParam("formParamName","value1").queryParam("queryParamName","value2");
(3)Headers
RequestSpecification httpRequest = RestAssured.given().
header("MyHeader","Something"); /
headers("MyHeader","something","MyOtherHeader","SomethingElse");
(4)cookies
RequestSpecification httpRequest = RestAssured.given().
cookie("username","john"); / cookie("username","john","yan");
(5)content type
RequestSpecification httpRequest = RestAssured.given().
contentType(ContentType.TEXT); / contentType("application/json");
(6)request body
RequestSpecification httpRequest = RestAssured.given().
body("some body"); / request().body("some body");
(7)身份认证
given().auth().basic("username", "password"). .. 每个请求设置身份验证
RestAssured.authentication = basic("username", "password"); 所有请求定义身份验证
--获得响应数据
(1)简单get请求,默认为body信息
String json = get("/lotto").asString();
//无头部信息
private ValidatableResponse response;
response = httpRequest.when().get("/idv-entity/{entity}/saml-token-type/{token}/customer-id-type/{type}/customer-id/{guid}/cam-level/{cam}").then().log().all();
//(有头部信息)
private Response response;
response = httpRequest.when().get("/idv-entity/{entity}/saml-token-type/{token}/customer-id-type/{type}/customer-id/{guid}/cam-level/{cam}");
System.out.println(response.asString());
//无头部信息(常用)默认body信息
(2)get headers
private Response response;
response = httpRequest.when().get("/id");
System.out.println(response.getHeaders()); / System.out.println(response.getHeaders());
(3)get cookies
private Response response;
response = httpRequest.when().get("/id");
System.out.println(response.getCookies()); / (response.getCookie("cookiename")); / (response.then().extract().cookies());
(4)get status line
private Response response;
response = httpRequest.when().get("/id");
System.out.println(response.getStatusLine()); / (response.then().extract().statusLine());
(5)get status code
private Response response;
response = httpRequest.when().get("/id");
System.out.println(response.getStatusCode()); / (response.then().extract().statusCode());
(6)get body
System.out.println(response.getBody().asString()); / (response.then().extract().body().asString());
--验证响应数据
前提:private ValidatableResponse response;
(1)response body
response.then().body("lotto.lottoId",equalTo(5));
response.then().body("lotto.winners.winnerId",hasItems(23,54));
lotto.winners.winnerId为json路径
(2)headers
response.then().assertThat().header("Access-Control-Allow-Credentials","true");
(3)cookies
response.then().assertThat().cookie("name","value");
(4)content-type
response.then().assertThat().contentType(ContentType.JSON);
(5)statusline
response.then().assertThat().statusLine("HTTP/1.1 500 Internal Server Error");
response.then().assertThat().statusLine(containString("500 Internal Server Error"));
(6)json schema
response.then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json");
(7)measuring response time
response.then().time(lessThan(2L),SECONDS);
(8)statuscode
response.then().assertThat().statusCode(SC_INTERNAL_SERVER_ERROR);
HttpStatus---
SC_OK = 200; HttpStatus.SC_OK
SC_CREATED = 201;
SC_NO_CONTENT = 204;
SC_NOT_MODIFIED = 304;
SC_BAD_REQUEST = 400;
SC_UNAUTHORIZED = 401;
SC_FORBIDDEN = 403;
SC_NOT_FOUND = 404;
SC_CONFLICT = 409;
SC_INTERNAL_SERVER_ERROR = 500;
3. 其他
--get请求
(1)参数直接在路径中(路径参数)
http://localhost:1080/idv-entity/malaysia/saml-token-type/SAML/customer-id-type/GUID/customer-id/c2c540803b4511e9a2c2006196/cam-level/30
(2)参数也可直接在路径中(非路径参数)
http://localhost:1080/?username=linyan&password=123456
(非路径参数也可在postman中params中设置,效果一样)
(3)headers参数设置
--post请求
body参数设置
--postman中Params和Body的区别
Params处设置的变量请求时,会显示在url后,通过问号传参
Body里设置的参数则是接口真正请求时发的参数,不会出现在地址栏
--get和post的区别
(1)get请求的参数放在url中,post请求的参数放在body中,so post更安全
(2)get请求有长度限制,post请求无长度限制
(3)当刷新服务器或回退时对get请求无影响,而post会重新提交请求
(4)get请求可被缓存,post请求不会被缓存
--响应码
1xx:服务器收到请求,需请求者继续执行操作
200:请求成功并处理
3xx:重定向,需进一步操作完成请求
400:请求语法错误
403:无访问权限
404:请求资源不存在
500:服务器内部错误
502:网关失效
504:网关请求超时