赞
踩
需要用到的包使用Maven依赖导入:
- <!-- REST assured -->
- <dependency>
- <groupId>io.rest-assured</groupId>
- <artifactId>rest-assured</artifactId>
- <version>4.4.0</version>
- <scope>test</scope>
- </dependency>
-
- <!--Hamcrest-->
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-core</artifactId>
- <version>1.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-library</artifactId>
- <version>1.3</version>
- <scope>test</scope>
- </dependency>
-
- <!-- jackson-->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.13.4.1</version>
- </dependency>
- <!-- Jayway Json-Path -->
- <dependency>
- <groupId>com.jayway.jsonpath</groupId>
- <artifactId>json-path</artifactId>
- <version>2.6.0</version>
- <scope>test</scope>
- </dependency>
-
- <!-- Json Schema Validator-->
- <dependency>
- <groupId>io.rest-assured</groupId>
- <artifactId>json-schema-validator</artifactId>
- <version>4.4.0</version>
- <scope>test</scope>
- </dependency>
GET与POST方法的用法:
- import static io.restassured.RestAssured.given;
- import org.junit.jupiter.api.Test;
-
- public class TestRestAssured {
-
- @Test
- void fun() {
- given()
- .when()
- .get("https://httpbin.org/ip")
- .then()
- .log().all()
- .statusCode(200);
- }
-
- @Test
- void test_post() {
- given().param("username","hogwarts")
- .log().all()
- .when().post("https://httpbin.org/post")
- .then()
- .statusCode(200).log().all();
- }
- }
-
Assertion断言用法:.statusCode(需要验证的状态码) .body(需要验证的响应体中的内容)
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static org.hamcrest.Matchers.equalTo;
-
-
- public class TestAssertionStatusCode {
-
- @Test
- void testStatusCode() {
- given()
- .when()
- .get("https://httpbin.org/get")
- .then()
- .statusCode(200)
- .log().all();
- }
- }
-
-
- public class TestAssertionBody {
- @Test
- void testBody(){
- given()
- .when().get("https://httpbin.org/get")
- .then().log().all()
- .body("origin", equalTo("222.178.202.83"));
- }
- }
请求体中包含Json数据:given().body()中可以传递json字符串也可以传递json对象
- import io.restassured.RestAssured;
- import org.junit.jupiter.api.Test;
- import java.util.HashMap;
-
-
- public class TestJsonStr {
- @Test
- void testJson() {
- String jsonStr = "{\"username\":\"hogwarts\"}";
- RestAssured
- .given()
- .contentType("application/json")
- .body(jsonStr)
- .when()
- .post("https://httpbin.org/post")
- .then().log().all().statusCode(200);
- }
- }
-
-
- public class TestJsonObj {
- @Test
- void testJson(){
- HashMap<String,String> jsonObj = new HashMap<String,String>() ;
- jsonObj.put("username","hogwarts");
- given()
- .contentType("application/json")
- .body(jsonObj)
- .when()
- .post("https://httpbin.org/post")
- .then()
- .log().all().statusCode(200);
- }
- }
-
使用JsonSchema来断言:
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
-
-
- public class TestJsonSchema {
-
- @Test
- void testJsonSchema() {
- given()
- .when()
- .get("https://httpbin.org/get")
- .then()
- .log().all()
- .assertThat()
- .body(matchesJsonSchemaInClasspath("schemademo.json"));
- }
- }
JsonSchema文件使用Instantly parse JSON in any language | quicktype
网站自动生成,将需要转换的Json粘贴进去就可以生成对应的JsonSchema(如图所示)。
在target/classes中创建新的json文件,然后将生成的JsonSchema粘贴进去。
就可以使用
.body(matchesJsonSchemaInClasspath("schemademo.json"));
对响应体中的json进行检查是否符合JsonSchema
使用JsonPath解析响应的数据:(可以使用在线JsonPath解析网站练习:在线JSONPath解析器 (lddgo.net))(JsonPath语法:参考JsonPath基本用法 - 拓荒者FF - 博客园 (cnblogs.com))
- import com.jayway.jsonpath.JsonPath;
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static org.junit.jupiter.api.Assertions.assertEquals;
-
-
- public class TestJsonPath {
- @Test
- void testJsonResp() {
- // HashMap<String, String> json = new HashMap<String, String>();
- // json.put("hello", "hogwarts");
- String resp = given()
- .header("Hello", "Hogwarts")
- .when().get("https://httpbin.org/get")
- .then()
- .log().all()
- .statusCode(200)
- .extract().response().asString();
- //解析响应数据
- String word = JsonPath.read(resp,"$.headers.Hello");
- System.out.println(word);
- // 断言
- assertEquals("Hogwarts",word);
- }
- }
设置RestAssured代理:(HTTPS代理需要使用RestAssured.useRelaxedHTTPSValidation()这意味着,无论SSL证书是否无效,你都将信任所有host)
- import io.restassured.RestAssured;
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static io.restassured.specification.ProxySpecification.host;
-
-
- public class TestProxy {
- @Test
- void testProxy(){
- RestAssured.proxy = host("localhost").withPort(8888);
- given()//.proxy("localhost",8888)
- .when().get("http://httpbin.org/get")
- .then().log().all().statusCode(200);
- }
- }
-
- public class TestHttpsProxy {
- @Test
- void testHttpsProxy(){
- RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888);
- RestAssured.useRelaxedHTTPSValidation();
- given()
- // .proxy(8888)
- // .relaxedHTTPSValidation()
- .when()
- .get("https://httpbin.org/get")
- .then()
- .log().all()
- .statusCode(200);
- }
- }
使用RestAssured传入header信息:
- import io.restassured.RestAssured;
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static io.restassured.specification.ProxySpecification.host;
-
-
- public class TestHeader {
- @Test
- void testHeader(){
- RestAssured.proxy = host("localhost").withPort(8888);
- RestAssured.useRelaxedHTTPSValidation();
- given().header("User-Agent","Hogwarts")
- .when().get("https://httpbin.org/get")
- .then().log().all().statusCode(200);
- }
- }
响应头中出现我们传入的User-Agent:
使用RestAssured传入cokkie信息:
- import io.restassured.RestAssured;
- import io.restassured.specification.ProxySpecification;
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
-
-
- public class TestCookie {
- @Test
- void testCookie() {
-
- RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888);
- RestAssured.useRelaxedHTTPSValidation();
- given().log().headers()
- //.header("cokkie","mycookie=sdsd")
- .cookie("cookie1","sdsd")
- .when()
- .get("https://httpbin.org/get")
- .then()
- .log().all()
- .statusCode(200);
- }
- }
响应头中出现我们传入的Cokkie:
请求体中传入表单请求:
- import io.restassured.RestAssured;
- import io.restassured.specification.ProxySpecification;
- import org.junit.jupiter.api.Test;
- import static io.restassured.RestAssured.given;
- import static io.restassured.specification.ProxySpecification.host;
-
-
- public class TestForm {
- @Test
- void testForm() {
- RestAssured.proxy = host("localhost").withPort(8888);
- RestAssured.useRelaxedHTTPSValidation();
- given()
- .log().headers()
- .log().body()
- // .formParam("myPram", "hogwarts")
- .formParams("username","hogwarts","password","123456")
- .when()
- .post("https://httpbin.org/post")
- .then()
- .statusCode(200);
- }
- }
使用Charles抓包发现响应数据中有Form。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。