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

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

源码以下:python

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()
    }
}

使用方法以下:apache

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();
    }

技术类文章精选

  1. java一行代码打印心形
  2. Linux性能监控软件netdata中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在Linux命令行界面愉快进行性能测试
  6. 图解HTTP脑图
  7. 如何测试几率型业务接口
  8. httpclient处理多用户同时在线
  9. 将swagger文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient如何处理302重定向
  12. 基于java的直线型接口测试框架初探

非技术文章精选

  1. 为何选择软件测试做为职业道路?
  2. 成为杰出Java开发人员的10个步骤
  3. 写给全部人的编程思惟
  4. 自动化测试的障碍

公众号地图 ☢️ 一块儿来~FunTester

相关文章
相关标签/搜索