zoukankan      html  css  js  c++  java
  • 将HTTP请求对象转成curl命令行

    之前写过一些关于复制浏览器中的请求做性能测试的文章:

    基本思路是复制浏览器请求为curl命令行,然后解析命令行组装成HttpRequestBase对象,然后结合FunTester性能测试框架进行测试。

    这次反过来,我写了一个将HttpRequestBase对象转成curl命令行形式的方法,用于在不同服务器上迅速重试请求,还可以通过一些参数的控制,了解HTTP请求过程的时间消耗情况。

    思路如下:1、将HttpRequestBase对象转成funrequest对象;2、然后将funrequest对象的属性拼接成curl命令。

    步骤一

    /**
         * 从requestbase对象从初始化funrequest
         * @param base
         * @return
         */
        static FunRequest initFromRequest(HttpRequestBase base) {
            FunRequest request = null
            String method = base.getMethod()
            RequestType requestType = RequestType.getRequestType(method)
            String uri = base.getURI().toString()
            List<Header> headers = Arrays.asList(base.getAllHeaders())
            if (requestType == requestType.GET) {
                request = isGet().setUri(uri).addHeaders(headers)
            } else if (requestType == RequestType.POST) {
                HttpPost post = (HttpPost) base
                HttpEntity entity = post.getEntity()
                String value = entity.getContentType().getValue()
                String content = null
                try {
                    content = EntityUtils.toString(entity)
                } catch (IOException e) {
                    logger.error("解析响应失败!", e)
                    fail()
                }
                if (value.equalsIgnoreCase(HttpClientConstant.ContentType_TEXT.getValue()) || value.equalsIgnoreCase(HttpClientConstant.ContentType_JSON.getValue())) {
                    request = isPost().setUri(uri).addHeaders(headers).addJson(JSONObject.parseObject(content))
                } else if (value.equalsIgnoreCase(HttpClientConstant.ContentType_FORM.getValue())) {
                    request = isPost().setUri(uri).addHeaders(headers).addParams(getJson(content.split("&")))
                }
            } else {
                RequestException.fail("不支持的请求类型!")
            }
            return request
        }
    

    步骤二

    /**
         * 将请求对象转成curl命令行
         * @return
         */
        String toCurl() {
            StringBuffer curl = new StringBuffer("curl -w HTTPcode%{http_code}:代理返回code%{http_connect}:数据类型%{content_type}:DNS解析时间%{time_namelookup}:%{time_redirect}:连接建立完成时间%{time_pretransfer}:连接时间%{time_connect}:开始传输时间%{time_starttransfer}:总时间%{time_total}:下载速度%{speed_download}:speed_upload%{speed_upload} ")
            if (requestType == RequestType.GET) curl << " -G"
            headers.each {
                curl << " -H '${it.getName()}:${it.getValue().replace(SPACE_1,EMPTY)}'"
            }
            switch (requestType) {
                case RequestType.GET:
                    args.each {
                        curl << " -d '${it.key}=${it.value}'"
                    }
                    break
                case RequestType.POST:
                    if (!params.isEmpty()) {
                        curl << " -H Content-Type:application/x-www-form-urlencoded"
                        params.each {
                            curl << " -F '${it.key}=${it.value}'"
                        }
                    }
                    if (!json.isEmpty()) {
                        curl << " -H "Content-Type:application/json"" //此处多余,防止从外部构建curl命令
                        json.each {
                            curl << " -d '${it.key}=${it.value}'"
                        }
                    }
                    break
                default:
                    break
            }
            curl << " ${uri}"
    //        curl << " --compressed" //这里防止生成多个curl请求,批量生成有用
            curl.toString()
        }
    

    测试服务代码

    这是用moco API封装过的框架的一部分。

        public static void main(String[] args) {
            def server = getServer(getLogMonitor("1.log"))
            server.get(urlOnly("/test")).response(obRes(Result.success(getJson("324324=32432"))))
            server.post(and(urlOnly("/post"),eqForm("a","a"))).response(textRes("234"))
            server.response(MocoResponse.textRes("hello word"))
            def run = run(server)
    
            waitForKey("fan")
            run.stop()
        }
    
    

    测试结果

    控制台输出的curl命令行:curl -w HTTPcode%{http_code}:代理返回code%{http_connect}:数据类型%{content_type}:DNS解析时间%{time_namelookup}:%{time_redirect}:连接建立完成时间%{time_pretransfer}:连接时间%{time_connect}:开始传输时间%{time_starttransfer}:总时间%{time_total}:下载速度%{speed_download}:speed_upload%{speed_upload} -H 'Content-Type:application/x-www-form-urlencoded;charset=UTF-8' -H 'X-Requested-With:XMLHttpRequest' -H 'User-Agent:Mozilla/5.0(Macintosh;IntelMacOSX10_14_4)AppleWebKit/537.36(KHTML,likeGecko)Chrome/74.0.3729.108Safari/537.36' -H 'Connection:keep-alive' -H Content-Type:application/x-www-form-urlencoded -F 'a=a' http://localhost:12345/post

    响应结果234


    FunTester腾讯云社区钦定年度作者,非著名测试开发er,欢迎关注。

  • 相关阅读:
    Flex实现页面多态state对象
    Flex精华摘要使用AS脚本
    JMeter最常用的三种类型的压力测试
    工作流简介
    Android模拟器使用模拟SD卡
    交大研究生,就一个字牛
    Google code上利用SVN托管代码
    主流浏览器内核概览
    Android开发之ADB使用
    Redis入门教程
  • 原文地址:https://www.cnblogs.com/FunTester/p/14510058.html
Copyright © 2011-2022 走看看