REST-assured 2发送文字到接口

获取token


https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT

#java
package date811;
import io.restassured.RestAssured.*;
import io.restassured.http.ContentType;
import io.restassured.matcher.RestAssuredMatchers.*;
import io.restassured.response.Response;
import org.hamcrest.Matchers.*;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
//assertThat方法一定要静态导入
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
public class GetToken {
    @Test
    public void getToken(){
        /**
         * 获取access token
         */
        String tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
        String corpId = "xxxxxx";
        String corpSecret = "xxxxxxx";
        Response res = given().param("corpid",corpId).param("corpsecret",corpSecret).get(tokenUrl).prettyPeek();
        String token_id = res.getBody().jsonPath().getString("access_token");
        res.then().statusCode(equalTo(200));
        assertThat(token_id,notNullValue());
    }
}

发送文字到接口

#java
package date811;
import io.restassured.RestAssured.*;
import io.restassured.http.ContentType;
import io.restassured.matcher.RestAssuredMatchers.*;
import io.restassured.response.Response;
import org.hamcrest.Matchers.*;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
//assertThat方法一定要静态导入
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
public class GetToken {
    @Test
    public void postMessage(){
        /**
         * 获取access token
         */
        String tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
        String corpId = "xxxxxx";
        String corpSecret = "xxxxxxxxxx";
        Response res = given().param("corpid",corpId).param("corpsecret",corpSecret).get(tokenUrl).prettyPeek();
        String token_id = res.getBody().jsonPath().getString("access_token");
        res.then().statusCode(equalTo(200));
        assertThat(token_id,notNullValue());

        /**
         * 发送消息
         *     {
         *        "touser" : "UserID1|UserID2|UserID3",
         *        "toparty" : "PartyID1|PartyID2",
         *        "totag" : "TagID1 | TagID2",
         *        "msgtype" : "text",
         *        "agentid" : 1,
         *        "text" : {
         *            "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
         *        },
         *        "safe":0
         *     }
         */
        
        String req ="    {\n" +
                "       \"toparty\" : \"1\",\n" +
                "       \"msgtype\" : \"text\",\n" +
                "       \"agentid\" : 1,\n" +
                "       \"text\" : {\n" +
                "           \"content\" : \"你的快递已到,请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>,聪明避开排队。\"\n" +
                "       },\n" +
                "       \"safe\":0\n" +
                "    }";
        /**
         * 传参有2中方法:1,将参数加入URL 2.使用queryParam传入参数
         */
        String post_url1 = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
        String post_url2 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token_id;
        
        System.out.println("方法1");
        given().contentType(ContentType.JSON).queryParam("access_token",token_id).body(req).post(post_url1).prettyPeek();
        System.out.println("方法2");
        given().contentType(ContentType.JSON).body(req).post(post_url2).prettyPeek();
    }
}

猜你喜欢

转载自www.cnblogs.com/csj2018/p/9460653.html