前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于java的直线型接口测试框架初探

基于java的直线型接口测试框架初探

作者头像
FunTester
发布2019-09-10 11:32:55
4720
发布2019-09-10 11:32:55
举报
文章被收录于专栏:FunTesterFunTester

在使用java语言作为接口测试的过程中,发现java语言的简洁性的确不如脚本语言,如python,很多功能python一行代码几个方法就能搞定,java需要几行,而且有时候并不利于理解。最近接触到了一个词“直线型”代码。看了之后有所感觉,重新写了一个直线型代码风格的接口请求框架。

源码如下:

代码语言:javascript
复制
package com.fun.frame.httpclient

import com.fun.base.bean.RequestInfo
import com.fun.config.RequestType
import net.sf.json.JSONObject
import org.apache.commons.lang3.StringUtils
import org.apache.http.Header
import org.apache.http.client.methods.HttpRequestBase
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * 重写FanLibrary,使用面对对象思想
 */
public class FunRequest extends FanLibrary {

    static Logger logger = LoggerFactory.getLogger(FunRequest.class)

    /**
     * 请求类型,true为get,false为post
     */
    RequestType requestType

    /**
     * 请求对象
     */
    HttpRequestBase request

    /**
     * host地址
     */
    String host

    /**
     * 接口地址
     */
    String apiName

    /**
     * 请求地址,如果为空则由host和apiname拼接
     */
    String uri

    /**
     * header集合
     */
    List<Header> headers = new ArrayList<>()

    /**
     * get参数
     */
    JSONObject args = new JSONObject()

    /**
     * post参数
     */
    JSONObject params = new JSONObject()

    /**
     * json参数
     */
    JSONObject json = new JSONObject()

    /**
     * 构造方法
     *
     * @param requestType
     */
    private FunRequest(RequestType requestType) {
        this.requestType = requestType
    }

    /**
     * 获取get对象
     *
     * @return
     */
    public static FunRequest isGet() {
        new FunRequest(RequestType.GET)
    }

    /**
     * 获取post对象
     *
     * @return
     */
    public static FunRequest isPost() {
        new FunRequest(RequestType.POST)
    }

    /**
     * 设置host
     *
     * @param host
     * @return
     */
    public FunRequest setHost(String host) {
        this.host = host
        this
    }

    /**
     * 设置接口地址
     *
     * @param apiName
     * @return
     */
    public FunRequest setApiName(String apiName) {
        this.apiName = apiName
        this
    }

    /**
     * 设置uri
     *
     * @param uri
     * @return
     */
    public FunRequest setUri(String uri) {
        this.uri = uri
        this
    }

    /**
     * 添加get参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addArgs(Object key, Object value) {
        args.put(key, value)
        this
    }

    /**
     * 添加post参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addParam(Object key, Object value) {
        params.put(key, value)
        this
    }

    /**
     * 添加json参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addJson(Object key, Object value) {
        json.put(key, value)
        this
    }

    /**
     * 添加header
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addHeader(Object key, Object value) {
        headers << getHeader(key.toString(), value.toString())
        this
    }

    /**
     * 添加header
     *
     * @param header
     * @return
     */
    public FunRequest addHeader(Header header) {
        headers.add(header)
        this
    }

    /**
     * 批量添加header
     *
     * @param header
     * @return
     */
    public FunRequest addHeader(List<Header> header) {
        header.each { h -> headers << h }
        this
    }

    /**
     * 增加header中cookies
     *
     * @param cookies
     * @return
     */
    public FunRequest addCookies(JSONObject cookies) {
        headers << getCookies(cookies)
        this
    }

    /**
     * 获取请求响应,兼容相关参数方法,不包括file
     *
     * @return
     */
    public JSONObject getResponse() {
        if (StringUtils.isEmpty(uri))
            uri = host + apiName
        switch (requestType) {
            case RequestType.GET:
                request = FanLibrary.getHttpGet(uri, args)
                break
            case RequestType.POST:
                request = !params.isEmpty() ? FanLibrary.getHttpPost(uri + changeJsonToArguments(args), params) : !json.isEmpty() ? getHttpPost(uri + changeJsonToArguments(args), json.toString()) : getHttpPost(uri + changeJsonToArguments(args))
                break
        }
        headers.each { x -> request.addHeader(x) }
        return getHttpResponse(request)
    }


    /**
     * 获取请求对象
     *
     * @return
     */
    public HttpRequestBase getRequest() {
        logger.debug("请求信息:{}",new RequestInfo(this.request).toString())
        this.request
    }

    @Override
    public String toString() {
        JSONObject.fromObject(this).toString()
    }
}

使用方法如下:

代码语言:javascript
复制
    public static void main(String[] args) {
        JSONObject response = FunRequest.isGet()
                .setHost("www.funtester.cn")
                .setApiName("/test")
                .addArgs("uname", "FunTester")
                .addArgs("passoword", "FunTester")
                .addArgs("type", "FunTester")
                .addHeader("token", "FunTester")
                .addCookies(getJson("login=false"))
                .getResponse();
        output(response);

        FanLibrary.testOver();
    }

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

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

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

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

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