基於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

相關文章
相關標籤/搜索