当前位置:   article > 正文

RestAssured 入门使用_restassured5.3.2使用proxyspecification

restassured5.3.2使用proxyspecification

需要用到的包使用Maven依赖导入:

  1. <!-- REST assured -->
  2. <dependency>
  3. <groupId>io.rest-assured</groupId>
  4. <artifactId>rest-assured</artifactId>
  5. <version>4.4.0</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <!--Hamcrest-->
  9. <dependency>
  10. <groupId>org.hamcrest</groupId>
  11. <artifactId>hamcrest-core</artifactId>
  12. <version>1.3</version>
  13. <scope>test</scope>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.hamcrest</groupId>
  17. <artifactId>hamcrest-library</artifactId>
  18. <version>1.3</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <!-- jackson-->
  22. <dependency>
  23. <groupId>com.fasterxml.jackson.core</groupId>
  24. <artifactId>jackson-databind</artifactId>
  25. <version>2.13.4.1</version>
  26. </dependency>
  27. <!-- Jayway Json-Path -->
  28. <dependency>
  29. <groupId>com.jayway.jsonpath</groupId>
  30. <artifactId>json-path</artifactId>
  31. <version>2.6.0</version>
  32. <scope>test</scope>
  33. </dependency>
  34. <!-- Json Schema Validator-->
  35. <dependency>
  36. <groupId>io.rest-assured</groupId>
  37. <artifactId>json-schema-validator</artifactId>
  38. <version>4.4.0</version>
  39. <scope>test</scope>
  40. </dependency>

GET与POST方法的用法:

  1. import static io.restassured.RestAssured.given;
  2. import org.junit.jupiter.api.Test;
  3. public class TestRestAssured {
  4. @Test
  5. void fun() {
  6. given()
  7. .when()
  8. .get("https://httpbin.org/ip")
  9. .then()
  10. .log().all()
  11. .statusCode(200);
  12. }
  13. @Test
  14. void test_post() {
  15. given().param("username","hogwarts")
  16. .log().all()
  17. .when().post("https://httpbin.org/post")
  18. .then()
  19. .statusCode(200).log().all();
  20. }
  21. }

Assertion断言用法:.statusCode(需要验证的状态码)  .body(需要验证的响应体中的内容)

  1. import org.junit.jupiter.api.Test;
  2. import static io.restassured.RestAssured.given;
  3. import static org.hamcrest.Matchers.equalTo;
  4. public class TestAssertionStatusCode {
  5. @Test
  6. void testStatusCode() {
  7. given()
  8. .when()
  9. .get("https://httpbin.org/get")
  10. .then()
  11. .statusCode(200)
  12. .log().all();
  13. }
  14. }
  15. public class TestAssertionBody {
  16. @Test
  17. void testBody(){
  18. given()
  19. .when().get("https://httpbin.org/get")
  20. .then().log().all()
  21. .body("origin", equalTo("222.178.202.83"));
  22. }
  23. }

请求体中包含Json数据:given().body()中可以传递json字符串也可以传递json对象

  1. import io.restassured.RestAssured;
  2. import org.junit.jupiter.api.Test;
  3. import java.util.HashMap;
  4. public class TestJsonStr {
  5. @Test
  6. void testJson() {
  7. String jsonStr = "{\"username\":\"hogwarts\"}";
  8. RestAssured
  9. .given()
  10. .contentType("application/json")
  11. .body(jsonStr)
  12. .when()
  13. .post("https://httpbin.org/post")
  14. .then().log().all().statusCode(200);
  15. }
  16. }
  17. public class TestJsonObj {
  18. @Test
  19. void testJson(){
  20. HashMap<String,String> jsonObj = new HashMap<String,String>() ;
  21. jsonObj.put("username","hogwarts");
  22. given()
  23. .contentType("application/json")
  24. .body(jsonObj)
  25. .when()
  26. .post("https://httpbin.org/post")
  27. .then()
  28. .log().all().statusCode(200);
  29. }
  30. }

使用JsonSchema来断言:

  1. import org.junit.jupiter.api.Test;
  2. import static io.restassured.RestAssured.given;
  3. import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
  4. public class TestJsonSchema {
  5. @Test
  6. void testJsonSchema() {
  7. given()
  8. .when()
  9. .get("https://httpbin.org/get")
  10. .then()
  11. .log().all()
  12. .assertThat()
  13. .body(matchesJsonSchemaInClasspath("schemademo.json"));
  14. }
  15. }

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)

  1. import com.jayway.jsonpath.JsonPath;
  2. import org.junit.jupiter.api.Test;
  3. import static io.restassured.RestAssured.given;
  4. import static org.junit.jupiter.api.Assertions.assertEquals;
  5. public class TestJsonPath {
  6. @Test
  7. void testJsonResp() {
  8. // HashMap<String, String> json = new HashMap<String, String>();
  9. // json.put("hello", "hogwarts");
  10. String resp = given()
  11. .header("Hello", "Hogwarts")
  12. .when().get("https://httpbin.org/get")
  13. .then()
  14. .log().all()
  15. .statusCode(200)
  16. .extract().response().asString();
  17. //解析响应数据
  18. String word = JsonPath.read(resp,"$.headers.Hello");
  19. System.out.println(word);
  20. // 断言
  21. assertEquals("Hogwarts",word);
  22. }
  23. }

 设置RestAssured代理:(HTTPS代理需要使用RestAssured.useRelaxedHTTPSValidation()这意味着,无论SSL证书是否无效,你都将信任所有host)

  1. import io.restassured.RestAssured;
  2. import org.junit.jupiter.api.Test;
  3. import static io.restassured.RestAssured.given;
  4. import static io.restassured.specification.ProxySpecification.host;
  5. public class TestProxy {
  6. @Test
  7. void testProxy(){
  8. RestAssured.proxy = host("localhost").withPort(8888);
  9. given()//.proxy("localhost",8888)
  10. .when().get("http://httpbin.org/get")
  11. .then().log().all().statusCode(200);
  12. }
  13. }
  14. public class TestHttpsProxy {
  15. @Test
  16. void testHttpsProxy(){
  17. RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888);
  18. RestAssured.useRelaxedHTTPSValidation();
  19. given()
  20. // .proxy(8888)
  21. // .relaxedHTTPSValidation()
  22. .when()
  23. .get("https://httpbin.org/get")
  24. .then()
  25. .log().all()
  26. .statusCode(200);
  27. }
  28. }

使用RestAssured传入header信息:

  1. import io.restassured.RestAssured;
  2. import org.junit.jupiter.api.Test;
  3. import static io.restassured.RestAssured.given;
  4. import static io.restassured.specification.ProxySpecification.host;
  5. public class TestHeader {
  6. @Test
  7. void testHeader(){
  8. RestAssured.proxy = host("localhost").withPort(8888);
  9. RestAssured.useRelaxedHTTPSValidation();
  10. given().header("User-Agent","Hogwarts")
  11. .when().get("https://httpbin.org/get")
  12. .then().log().all().statusCode(200);
  13. }
  14. }

响应头中出现我们传入的User-Agent: 

  

使用RestAssured传入cokkie信息:

  1. import io.restassured.RestAssured;
  2. import io.restassured.specification.ProxySpecification;
  3. import org.junit.jupiter.api.Test;
  4. import static io.restassured.RestAssured.given;
  5. public class TestCookie {
  6. @Test
  7. void testCookie() {
  8. RestAssured.proxy = ProxySpecification.host("localhost").withPort(8888);
  9. RestAssured.useRelaxedHTTPSValidation();
  10. given().log().headers()
  11. //.header("cokkie","mycookie=sdsd")
  12. .cookie("cookie1","sdsd")
  13. .when()
  14. .get("https://httpbin.org/get")
  15. .then()
  16. .log().all()
  17. .statusCode(200);
  18. }
  19. }

 响应头中出现我们传入的Cokkie: 

 

 

请求体中传入表单请求:

  1. import io.restassured.RestAssured;
  2. import io.restassured.specification.ProxySpecification;
  3. import org.junit.jupiter.api.Test;
  4. import static io.restassured.RestAssured.given;
  5. import static io.restassured.specification.ProxySpecification.host;
  6. public class TestForm {
  7. @Test
  8. void testForm() {
  9. RestAssured.proxy = host("localhost").withPort(8888);
  10. RestAssured.useRelaxedHTTPSValidation();
  11. given()
  12. .log().headers()
  13. .log().body()
  14. // .formParam("myPram", "hogwarts")
  15. .formParams("username","hogwarts","password","123456")
  16. .when()
  17. .post("https://httpbin.org/post")
  18. .then()
  19. .statusCode(200);
  20. }
  21. }

使用Charles抓包发现响应数据中有Form。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/869782
推荐阅读
相关标签
  

闽ICP备14008679号