• REST-assured 3发送图片


    上传图片,需要media_id,从上传临时素材获取:https://work.weixin.qq.com/api/doc#10112

    https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
    参数 :

    • access_token :调用接口凭证
    • type :媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)

    1.封装image

    #java
    package date811;
    
    import java.util.Map;
    
    public class WXTextImage {
        /**
         *     {
         *        "touser" : "UserID1|UserID2|UserID3",
         *        "toparty" : "PartyID1|PartyID2",
         *        "totag" : "TagID1 | TagID2",
         *        "msgtype" : "image",
         *        "agentid" : 1,
         *        "image" : {
         *             "media_id" : "MEDIA_ID"
         *        },
         *        "safe":0
         *     }
         */
        private String touser;
        private String toparty;
        private String totag;
        private String msgtype;
        private Integer agentid;
        private Map<String,String> image;
        private Integer safe;
    
        public String getTouser() {
            return touser;
        }
    
        public void setTouser(String touser) {
            this.touser = touser;
        }
    
        public String getToparty() {
            return toparty;
        }
    
        public void setToparty(String toparty) {
            this.toparty = toparty;
        }
    
        public String getTotag() {
            return totag;
        }
    
        public void setTotag(String totag) {
            this.totag = totag;
        }
    
        public String getMsgtype() {
            return msgtype;
        }
    
        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }
    
        public Integer getAgentid() {
            return agentid;
        }
    
        public void setAgentid(Integer agentid) {
            this.agentid = agentid;
        }
    
        public Map<String, String> getImage() {
            return image;
        }
    
        public void setImage(Map<String, String> image) {
            this.image = image;
        }
    
        public Integer getSafe() {
            return safe;
        }
    
        public void setSafe(Integer safe) {
            this.safe = safe;
        }
    }
    

    2.在公共类中获取media_id

    #java
    package date811;
    
    import io.restassured.response.Response;
    import org.testng.annotations.Test;
    
    import java.io.File;
    
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertThat;
    import static io.restassured.RestAssured.given;
    
    public class WXUtil {
        /*
        公共类
         */
        private static String corpID = "xxx";
        private static String corpSecret = "xxxxx";
        private static String tokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
        //获取access_token的方法
        public static String getToken(){
            Response res = given().param("corpid",corpID).
                    param("corpsecret",corpSecret).get(tokenURL).prettyPeek();
            String token_text = res.getBody().jsonPath().getString("access_token");
            assertNotNull(token_text);
            return token_text;
        }
        //获取media_id
        private static String uploadURL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload";
        public static String getMedia(){
            String token = getToken();
            Response res = given().queryParam("access_token",token)
                    .queryParam("type","image")
                    .multiPart("media",new File("image/粘贴图片.png"))
                    .post(uploadURL).prettyPeek();
            String mediaId = res.getBody().jsonPath().getString("media_id");
            assertNotNull(mediaId);
            return mediaId;
        }
    
    }
    
    

    3.执行类

    #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 java.util.HashMap;
    import java.util.Map;
    
    import static io.restassured.RestAssured.given;
    import static io.restassured.RestAssured.useRelaxedHTTPSValidation;
    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 post_message() {
            String token_text = WXUtil.getToken();
            String postURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
            WXTextImage wx = new WXTextImage();
            wx.setAgentid(0);
            wx.setSafe(0);
            wx.setToparty("0");
            wx.setMsgtype("image");
            Map<String,String> text = new HashMap<>();
            text.put("media_id",WXUtil.getMedia());
            wx.setImage(text);
            given().contentType(ContentType.JSON)
                    .body(wx)
                    .queryParam("access_token",token_text)
                    .post(postURL).prettyPrint()
        }
    }
    
    

  • 相关阅读:
    MySQL my.cnf详解
    函数:sleep-exit-wait
    fork-小实验
    OS-进程调度
    CET-4流程
    SDK和API的区别
    生活-金钱管理-不是理财
    算法设计与分析:Strassen矩阵乘法
    力扣:二进制加法求和
    算法设计与分析:大整数乘法
  • 原文地址:https://www.cnblogs.com/csj2018/p/9461129.html
Copyright © 2020-2023  润新知