zoukankan      html  css  js  c++  java
  • RestAssured

    配置MAVEN

            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>rest-assured</artifactId>
            </dependency>
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>json-schema-validator</artifactId>
                <version>3.0.2</version>
            </dependency>
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>spring-mock-mvc</artifactId>
                <version>3.0.6</version>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-library</artifactId>
            </dependency>
    

    发送json请求1:

        RequestSpecification request = RestAssured.given().header("Content-Type", "application/json");
        JSONObject requestParams = new JSONObject();
        JSONObject childObject1= new JSONObject();
        JSONObject childObject2 = new JSONObject();
        requestParams.put("contactClass", "ZWSS"); 
        requestParams.put("contactActivity", "0039");
        requestParams.put("contractAccountNumber", "210024144291");
        requestParams.put("text", "Please rate the overall ease of using the website to initiate or make your service request");
        requestParams.put("contactType", "Z1");
        requestParams.put("contactDirection", "1");
    
        childObject1.put("question", "0001");
        childObject1.put("answer", "01");
        childObject1.put("question", "0002");
        childObject1.put("answer", "02");
    
        JSONArray jsonArray = new JSONArray();
    
        jsonArray.put(childObject1);
        jsonArray.put(childObject2);
        requestParams.put("addInfo", jsonArray);
    
        request.body(requestParams.toString());
        Response response = request.post("https://api-dev.adp.com/api/*/*/*");
    

    发送Json请求2

    import com.github.openjson.JSONArray;
    import com.github.openjson.JSONObject;
    import com.westone.cx.performance.common.AccountConst;
    import io.restassured.RestAssured;
    import io.restassured.config.SSLConfig;
    import io.restassured.response.Response;
    import io.restassured.specification.RequestSpecification;
    import org.hamcrest.Matchers.*;
    public class RestAssuredSampler {
        public static void main(String[] args) {
            RestAssured.useRelaxedHTTPSValidation();
            String requestParams = "{"aaa":"11111","bbb":"22222"}";
            RequestSpecification request = RestAssured.given()
    //                .config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
                    .header("Content-Type", "application/json");
            request.body(requestParams);
            Response response = request.post("http://url");
            response.prettyPrint();
        }
    }
    

    Json响应

    given().
                    headers(headers).
                    contentType("application/json").
                    body(jsonString).
                    when().
                    post(url).
                    then().
                    log().body().
                    assertThat().
                    statusCode(200).
                    body("$.size()",equalTo(1)).
                    body("[0].contactMethods.size()",equalTo(2));
    

    Reference

    https://github.com/RookieTester/rest-assured-doc/blob/master/2016-12-12-【接口测试】rest-assured用户手册中文版.markdown#JSON(使用JsonPath)

    java api

    http://static.javadoc.io/io.rest-assured/rest-assured/3.0.1/index.html

  • 相关阅读:
    谷歌浏览器(Google Chrome)开发调试详细介绍
    Js调试技巧
    Spring框架集成FreeMarker
    Spring MVC + freemarker实现半自动静态化
    Django学习路4_数据库添加元素,读取及显示到网页上
    数据库设计基础知识
    Django创建简单数据库
    list 和 [ ] 的功能不相同
    Django坑_01
    爬虫流程复习3
  • 原文地址:https://www.cnblogs.com/liehen2046/p/11689276.html
Copyright © 2011-2022 走看看