前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用alertover发送获取响应失败的通知消息

利用alertover发送获取响应失败的通知消息

作者头像
FunTester
发布2019-09-10 11:24:45
1.4K0
发布2019-09-10 11:24:45
举报
文章被收录于专栏:FunTester

本人在做接口自动化时候,因为服务器不稳定造成可能的用例失败,但这个失败表象只是在获取响应实体的json对象时为空,在后期排查问题时可能造成困扰,所以特意加了一个获取响应失败的通知,目的就是即使了解到服务器异常。暂时用的是免费的alertover,用了很久,简单可靠是它的优点,后续会加入微信提醒。分享代码,供大家参考。

下面是获取响应实体的json对象的方法(可忽略某一些封装方法):

代码语言:javascript
复制
    /**
     * 获取响应实体
     * <p>会自动设置cookie,但是需要各个项目再自行实现cookie管理</p>
     * <p>该方法只会处理文本信息,对于文件处理可以调用两个过期的方法解决</p>
     *
     * @param request 请求对象
     * @return 返回json类型的对象
     */
    public static JSONObject getHttpResponse(HttpRequestBase request) {
        if (!isRightRequest(request)) return new JSONObject();
        beforeRequest(request);
        JSONObject res = new JSONObject();
        RequestInfo requestInfo = new RequestInfo(request);
        if (HEADER_KEY) output("===========request header===========", Arrays.asList(request.getAllHeaders()));
        long start = Time.getTimeStamp();
        try (CloseableHttpResponse response = ClientManage.httpsClient.execute(request)) {
            long end = Time.getTimeStamp();
            long elapsed_time = end - start;
            if (HEADER_KEY) output("===========response header===========", Arrays.asList(response.getAllHeaders()));
            int status = getStatus(response, res);
            JSONObject setCookies = afterResponse(response);
            String content = getContent(response);
            int data_size = content.length();
            res.putAll(getJsonResponse(content, setCookies));
            int code = iBase == null ? -2 : iBase.checkCode(res, requestInfo);
            if (!iBase.isRight(res))
                new AlertOver("响应状态码错误:" + status, "状态码错误:" + status, requestInfo.getUrl(), requestInfo).sendSystemMessage();
            MySqlTest.saveApiTestDate(requestInfo, data_size, elapsed_time, status, getMark(), code, LOCAL_IP, COMPUTER_USER_NAME);
        } catch (Exception e) {
            logger.warn("获取请求相应失败!", e);
            if (!SysInit.isBlack(requestInfo.getHost()))
                new AlertOver("接口请求失败", requestInfo.toString(), requestInfo.getUrl(), requestInfo).sendSystemMessage();
        } finally {
            HEADER_KEY = false;
            if (!SysInit.isBlack(requestInfo.getHost())) {
                if (requests.size() > 9) requests.removeFirst();
                boolean add = requests.add(request);
            }
        }
        return res;
    }

下面是alertover类的代码,比较简单:

代码语言:javascript
复制
package com.fun.utils.message;

import com.fun.frame.httpclient.FanLibrary;
import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IMessage;
import com.fun.db.mysql.MySqlTest;
import com.fun.config.SysInit;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AlertOver extends FanLibrary implements IMessage {

    private static Logger logger = LoggerFactory.getLogger(AlertOver.class);

    String title;

    String content;

    String murl;

    RequestInfo requestInfo;

    private static String system = "s-7e93ec02-1308-480c-bc11-a7260c14";//系统异常

    private static String function = "s-7e3b7ea5-b4b0-4479-a0e3-bce6c830";//功能异常

    private static String business = "s-466a191a-cbb8-4164-b8be-9779bb88";//业务异常

    private static String remind = "s-f49ac5bc-008b-4b11-890e-6715ef89";//提醒推送

    private static String code = "s-490d0fc6-35cc-4430-9f87-09cdeb05";//程序异常

    private static final String testGroup = "g-4eefc0ad-19af-4b1c-9d0b-ef87be15";

    public AlertOver() {
        this("test title", "test content!");
    }

    public AlertOver(String title, String content) {
        this.title = title;
        this.content = content + LINE + "发送源:" + COMPUTER_USER_NAME;
    }

    public AlertOver(String title, String content, String url) {
        this(title, content);
        this.murl = url;
    }

    public AlertOver(String title, String content, String url, RequestInfo requestInfo) {
        this(title, content);
        this.murl = url;
        this.requestInfo = requestInfo;
    }

    /**
     * 发送系统异常
     */
    public void sendSystemMessage() {
        if (SysInit.isBlack(murl)) return;
        sendMessage(system);
        MySqlTest.saveAlertOverMessage(requestInfo, "system", title, LOCAL_IP, COMPUTER_USER_NAME);
        logger.info("发送系统错误提醒,title:{},ip:{},computer:{}", title, LOCAL_IP, COMPUTER_USER_NAME);
    }

    /**
     * 发送功能异常
     */
    public void sendFunctionMessage() {
        sendMessage(function);
    }

    /**
     * 发送业务异常
     */
    public void sendBusinessMessage() {
        sendMessage(business);
    }

    /**
     * 发送程序异常
     */
    public void sendCodeMessage() {
        sendMessage(code);
    }

    /**
     * 提醒推送
     */
    public void sendRemindMessage() {
        sendMessage(remind);
    }

    /**
     * 发送消息
     *
     * @return
     */
    public void sendMessage(String source) {
        if (SysInit.isBlack(murl)) return;
        String url = "https://api.alertover.com/v1/alert";
        String receiver = testGroup;//测试组ID
        JSONObject jsonObject = new JSONObject();// 新建json数组
        jsonObject.put("frame", source);// 添加发送源id
        jsonObject.put("receiver", receiver);// 添加接收组id
        jsonObject.put("content", content);// 发送内容
        jsonObject.put("title", title);// 发送标题
        jsonObject.put("url", murl);// 发送标题
        jsonObject.put("sound", "pianobar");// 发送声音
        logger.debug("消息详情:{}", jsonObject.toString());
        HttpPost httpPost = getHttpPost(url, jsonObject);
        取消发送
        getHttpResponse(httpPost);
    }
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档