zoukankan      html  css  js  c++  java
  • 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" : "你的快递已到,请携带工卡前往邮件中心领取。
    出发前可查看<a href="http://work.weixin.qq.com">邮件中心视频实况</a>,聪明避开排队。"
             *        },
             *        "safe":0
             *     }
             */
            
            String req ="    {
    " +
                    "       "toparty" : "1",
    " +
                    "       "msgtype" : "text",
    " +
                    "       "agentid" : 1,
    " +
                    "       "text" : {
    " +
                    "           "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
    " +
                    "       },
    " +
                    "       "safe":0
    " +
                    "    }";
            /**
             * 传参有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();
        }
    }
    
    

  • 相关阅读:
    Linux内核之 I/O多路复用
    Linux内核之 页高速缓存与页回写
    Linux内核之 块I/O层及I/O调度
    Linux内核之 文件I/O
    C++雾中风景15:聊聊让人抓狂的Name Mangling
    音频数据增强及python实现
    深度学习中“过拟合”的产生原因和解决方法
    资料-资源(不定期更新)
    论文翻译:2020_Acoustic Echo Cancellation Challenge Datasets And Testingframework
    语音信号处理概念
  • 原文地址:https://www.cnblogs.com/csj2018/p/9460653.html
Copyright © 2011-2022 走看看