【测试平台系列】第一章 手撸压力机(十二)-初步实现提取功能

简介: 上一章节,我们主要实现了基础的并发测试场景的能力。本章节,我们实现一下,如何对响应进行提取,使用正则/json对响应信息提取,并赋值给我们定义的变量。

上一章节,我们主要实现了基础的并发测试场景的能力。本章节,我们实现一下,如何对响应进行提取,使用正则/json对响应信息提取,并赋值给我们定义的变量。
首先定义一个提取的数据结构。model目录下新建withdraw.go


// Package model -----------------------------
// @file      : withdraw.go
// @author    : 被测试耽误的大厨
// @contact   : 13383088061@163.com
// @time      : 2023/8/15 15:59
// -------------------------------------------
package model

type Withdraws struct {
        WithdrawList []*Withdraw json:"withdraw_list,omitempty" // 提取式一个列表
}

type Withdraw struct {
        IsEnable   bool        json:"is_enable,omitempty"  // 是否启用
        Key        string      json:"key,omitempty"        // 赋值给->变量名
        Type       int32       json:"type,omitempty"       // 提取类型: 0: 正则表达式    1: json  2:提取响应头    3:提取响应码 默认是0
        Index      int         json:"index,omitempty"      // 需要提取指的下标
        Value      interface{} json:"value,omitempty"      // 提取出来的值
        Expression string      json:"expression,omitempty" // 表达式
        ValueType  string      json:"value_type,omitempty" // 提取出来的值的类型
}

我们针对提取类型,定义一下常量global/constant/constant.go

// 关联提取类型
const (
        RegExtract    = 0 // 正则提取
        JsonExtract   = 1 // json提取
        HeaderExtract = 2 // 提取header
        CodeExtract   = 3 // 响应码提取
)

然后,我们实现一下Withdraw结构体的提取方法model/withdraw.go

// http请求提取
func (wd *Withdraw) WithdrawHttp(resp *fasthttp.Response, variable *sync.Map) {
        // 定义一个任意类型的变量,用来接收提取的值
        var value interface{}
        // 根据提取类型判断
        switch wd.Type {
        case constant.JsonExtract:
                var str string
                if resp != nil {
                        str = string(resp.Body())
                }
                // json.Withdraw,自己实现的json提取方法
                value = wd.jsonWithdraw(str)
        case constant.CodeExtract:
                if resp != nil {
                        value = resp.StatusCode()
                } else {
                        value = ""
                }
        case constant.HeaderExtract:
                var str string
                if resp != nil {
                        str = resp.Header.String()
                }
                value = wd.jsonWithdraw(str)
        default:
                var str string
                if resp != nil {
                        str = string(resp.Body())
                }
                value = wd.regexWithdraw(str)
        }
        variable.Store(wd.Key, value)
}

// json提取
func (wd *Withdraw) jsonWithdraw(source string) interface{} {
        gq := gjson.Get(source, wd.Expression)
        return gq.Value()
}

// 正则提取
func (wd *Withdraw) regexWithdraw(source string) (value interface{}) {
        if wd.Expression == "" || source == "" {
                value = ""
                return
        }
        value = tools.FindAllDestStr(source, wd.Expression)
        if value == nil || len(value.([][]string)) < 1 {
                value = ""
        } else {
                if len(value.([][]string)[0]) <= 1 {
                        value = value.([][]string)[0][0]
                } else {
                        value = value.([][]string)[0][1]
                }

        }
        return
}

// header提取,使用正则表达式提取
func (wd *Withdraw) headerWithdraw(source string) (value interface{}) {
        if wd.Expression == "" || source == "" {
                return ""
        }
        return tools.MatchString(source, wd.Expression, wd.Index)
}

这样,我们有提取的时候,直接根据提取类型,去使用Withdraws的不同的方法即可。
下面,我们完成http请求的关联提取,首先给HttpRequest结构体添加Withdraws字段,使用Withdraws指针类型

// HttpRequest http请求的结构
type HttpRequest struct {
        Url                string             json:"url"               // 接口uri
        Method             string             json:"method"            // 接口方法,Get Post Update...
        Timeout            int64              json:"timeout,omitempty" // 请求超时时长,默认为30秒,防止请求阻塞
        Body               *Body              json:"body,omitempty"    // 请求提
        Headers            []Header           json:"headers,omitempty" // 接口请求头
        Querys             []Query            json:"querys,omitempty"  // get请求时的url
        Cookies            []Cookie           json:"cookies,omitempty" // cookie
        Withdraws          *Withdraws         json:"withdraws,omitempty"
        HttpClientSettings HttpClientSettings json:"http_client_settings" // http客户端配置
}

然后,我们实现HttpRequest结构体的withdraw方法

// http提取
func (hr *HttpRequest) withdraw(resp *fasthttp.Response, variableMap *sync.Map) {
        if hr.Withdraws == nil {
                return
        }
        if hr.Withdraws.WithdrawList == nil {
                return
        }
        for _, withdraw := range hr.Withdraws.WithdrawList {
                if !withdraw.IsEnable {
                        continue
                }
                withdraw.WithdrawHttp(resp, variableMap)
        }
}

因为我们需要将提取出来的值存放到一个公共的地方,给其他接口去使用,所以我们使用一个带锁的sync.Map来存储提取出来的变量,这样就避免了map的并发安全问题。
修改HttpRequest结构体的Request方法,修改后如下,http_request.go:

func (hr *HttpRequest) Request(response *TestObjectResponse, variableMap *sync.Map) {
        // 使用fasthttp 协程池

        // 新建一个http请求
        req := fasthttp.AcquireRequest()
        defer fasthttp.ReleaseRequest(req)
        // 新建一个http响应接受服务端的返回
        resp := fasthttp.AcquireResponse()
        defer fasthttp.ReleaseResponse(resp)

        // 新建一个http的客户端, newHttpClient是一个方法,在下面
        client := newHttpClient(hr.HttpClientSettings)

        // 设置请求方法
        hr.methodInit(req)

        // 设置header
        hr.headerInit(req)
        // 设置query
        hr.queryInit(req)
        // 设置cookie
        hr.cookieInit(req)

        // 设置url
        hr.urlInit(req)

        // 设置body
        hr.bodyInit(req)

        // 设置请求超时时间
        hr.setReqTimeout(req)

        // 记录开始时间
        startTime := time.Now()
        // 开始请求
        err := client.Do(req, resp)
        // 计算响应时间差值
        requestTime := time.Since(startTime)
        response.RequestTime = requestTime.Milliseconds()
        response.Code = resp.StatusCode()
        if err != nil {
                response.Response = err.Error()
        } else {
                // 如果响应中有压缩类型,防止压缩类型出现乱码
                switch string(resp.Header.ContentEncoding()) {
                case "br", "deflate", "gzip":
                        b, _ := resp.BodyUncompressed()
                        response.Response = string(b)
                default:
                        response.Response = string(resp.Body())
                }
        }
    // 关联提取
        hr.withdraw(resp, variableMap)
}

修改TestObject对象的Dispose方法如下:

// Dispose 测试对象的处理函数,在go语言中 Dispose方法是TestObject对象的方法,其他对象不能使用
func (to TestObject) Dispose(response *TestObjectResponse, variableMap *sync.Map) {
   switch to.ObjectType {
   case HTTP1: // 由于我们有个多类型,为了方便统计,我们定义好变量,直接进行比对即可
      to.HttpRequest.Request(response, variableMap)
      return
   }
   return
}

修改TestScene对象的Dispose方法如下:

func (testScene TestScene) Dispose() {
   // 从testScene.TestObjects的最外层开始循环
   variableMap := &sync.Map{}
   for _, testObject := range testScene.TestObjects {
      response := &TestObjectResponse{
         Name:       testObject.Name,
         Id:         testObject.Id,
         SceneId:    testScene.Id,
         ItemId:     testObject.ItemId,
         ObjectType: testObject.ObjectType,
      }
      testObject.Dispose(response, variableMap)
      // 从一层级的list中读取每个TestObject对象
   }

}

修改/run/testObject/接口方法,分别添加一个全局sync.Map

func RunTestObject(c *gin.Context) {

        // 声明一个TO对象
        var testObject model.TestObject

        // 接收json格式的请求数据
        err := c.ShouldBindJSON(&testObject)
        id := uuid.New().String()
        // 如果请求格式错误
        if err != nil {
                log.Logger.Error("请求数据格式错误", err.Error())
                common.ReturnResponse(c, http.StatusBadRequest, id, "请求数据格式错误!", err.Error())
                return
        }

        // 使用json包解析以下TO对象, 解析出来为[]byte类型
        requestJson, _ := json.Marshal(testObject)
        // 打印以下日志, 使用fmt.Sprintf包格式花数据,%s 表示string(requestJson)为字符串类型,如果不确定类型,可以使用%v表示
        log.Logger.Debug(fmt.Sprintf("测试对象: %s", string(requestJson)))

        response := model.TestObjectResponse{
                Name:       testObject.Name,
                Id:         testObject.Id,
                ObjectType: testObject.ObjectType,
                ItemId:     testObject.ItemId,
                TeamId:     testObject.TeamId,
        }

        variableMap := &sync.Map{}
        // 开始处理TO
        testObject.Dispose(&response, variableMap)
        variableMap.Range(func(key, value any) bool {
                log.Logger.Debug(fmt.Sprintf("提取出来:   key: %s, value: %v ", key, value))
                return true
        })
        // 返回响应消息
        common.ReturnResponse(c, http.StatusOK, id, "请求成功!", response)
        return
}

现在启动我们的服务,我们使用下面的body调用我们的/run/testObject/接口:

接口:  127.0.0.1:8002/engine/run/testObject/
{
    "name": "百度",
    "id": "12312312312312",
    "object_type": "HTTP1.1",
    "item_id": "12312312312312",
    "team_id": "1231231231231",
    "http_request": {
        "url": "http://www.baidu.com",
        "method": "GET",
        "request_timeout": 5,
        "headers": [],
        "querys": [],
        "cookies": [],
        "http_client_settings": {},
        "withdraws": {
            "withdraw_list": [
                {
                    "is_enable": true,
                    "key": "name",
                    "type": 0,
                    "expression": "<meta name=\"description\" content=\"(.*?)\">"
                }
            ]
        }
    }

}

打印的结果如下:

2023-08-17T11:48:46.961+0800    DEBUG   service/object_api.go:43        测试对象: {"name":"百度","id":"12312312312312","object_type":"HTTP1.1","item_id":"12312312312312","team_id":"1231231231231","http_request":{"url":"http://www.baidu.com","method":"GET","withdraws":{"withdraw_list":[{"is_enable":true,"key","expression":"\u003cmeta name=\"description\" content=\"(.*?)\"\u003e"}]},"http_client_settings":{"name":"","no_default_user_agent_header":false,"max_conns_per_host":0,"max_idle_conn_duration":0,"max_conn_duration":0,"read_timeout":0,"write_timeout":0,"disable_header_names_normalizing":false,"disable_path_normalizing":false,"advanced_options":{"tls":{"is_verify":false,"verify_type":0,"ca_cert":""}}}}}
2023-08-17T11:48:47.244+0800    DEBUG   service/object_api.go:57        提取出来:   key: name, value: 全球领先的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。

可以看到我们提取出来的值为:“全球领先的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果”,并将其赋值给name变量。

相关文章
|
24天前
|
人工智能 搜索推荐 Serverless
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
59 7
使用金庸的著作,来测试阿里通义千问最新开放的长文档处理功能
|
1月前
|
Java
【Java每日一题】— —第二十一题:编程把现实生活的手机事物映射成一个标准类Phone,并定义一个测试类PhoneDemo测试Phone类的功能
【Java每日一题】— —第二十一题:编程把现实生活的手机事物映射成一个标准类Phone,并定义一个测试类PhoneDemo测试Phone类的功能
37 0
|
5天前
|
人工智能 监控 数据处理
【AI大模型应用开发】【LangSmith: 生产级AI应用维护平台】1. 快速上手数据集与测试评估过程
【AI大模型应用开发】【LangSmith: 生产级AI应用维护平台】1. 快速上手数据集与测试评估过程
20 0
|
1月前
|
缓存 运维 Serverless
应用研发平台EMAS产品常见问题之测试检查更新没有反应如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
|
1月前
|
机器学习/深度学习 人工智能 监控
视觉智能平台常见问题之体验产品的美颜测试关掉如何解决
视觉智能平台是利用机器学习和图像处理技术,提供图像识别、视频分析等智能视觉服务的平台;本合集针对该平台在使用中遇到的常见问题进行了收集和解答,以帮助开发者和企业用户在整合和部署视觉智能解决方案时,能够更快地定位问题并找到有效的解决策略。
24 1
|
2月前
|
人工智能 安全
外汇MT5/MT4交易所平台系统开发测试版/案例设计/策略步骤/功能需求/源码程序
When developing the MT5/MT4 foreign exchange documentary trading system, the following functions and intelligence can also be considered:
|
2月前
|
测试技术
Lim测试平台测试报告说明
Lim测试平台测试报告说明
32 2
|
2月前
|
SQL 测试技术 数据库连接
Lim接口测试平台-接口测试功能详解
Lim接口测试平台-接口测试功能详解
46 1
|
2月前
|
SQL 监控 测试技术
Lim测试平台变量使用规则介绍
Lim测试平台变量使用规则介绍
28 0
|
2月前
|
测试技术
使用Lim测试平台快速完成批量造数
使用Lim测试平台快速完成批量造数
31 1

热门文章

最新文章