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();
        }
    }
    
    

  • 相关阅读:
    20080619 SQL SERVER 输入 NULL 的快捷键
    20090406 Adobe的“此产品的许可已停止工作”错误的解决办法
    20080908 Office Powerpoint 2007 不能输入中文的解决办法
    20080831 ClearGertrude Blog Skin 's cnblogs_code class
    20080603 Facebook 平台正式开放
    20080519 安装 Microsoft SQL Server 2000 时提示 创建挂起的文件操作
    test
    Linux—fork函数学习笔记
    SOA的设计理念
    Why BCP connects to SQL Server instance which start with account of Network Service fail?
  • 原文地址:https://www.cnblogs.com/csj2018/p/9460653.html
Copyright © 2011-2022 走看看