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