在實踐性能測試框架第二版的過程中,我實現了一個單個HttpRequestBase對象的concurrent對象創建,單之前都是用使用唯一的HttpRequestBase對象進行多線程請求,目前來看是沒有問題的,但為了防止以后出現意外BUG和統一concurrent的構造方法使用,故嘗試拷貝了一個HttpRequestBase對象。原因是因為之前封裝的深拷貝方法對於HttpRequestBase對象的實現類如:httpget和httppost並不適用,因為沒有實現Serializable接口。所以單獨寫了一個HttpRequestBase對象的拷貝方法,供大家參考。
下面是FunRequest
類的代碼,深拷貝的靜態方法在最后。
package com.fun.frame.httpclient
import com.fun.base.bean.RequestInfo
import com.fun.base.exception.RequestException
import com.fun.config.HttpClientConstant
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.HttpEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpRequestBase
import org.apache.http.util.EntityUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* 重寫FanLibrary,使用面對對象思想
*/
public class FunRequest extends FanLibrary implements Serializable,Cloneable {
private static final long serialVersionUID = -4153600036943378727L;
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
*/
static FunRequest isGet() {
new FunRequest(RequestType.GET)
}
/**
* 獲取post對象
*
* @return
*/
static FunRequest isPost() {
new FunRequest(RequestType.POST)
}
/**
* 設置host
*
* @param host
* @return
*/
FunRequest setHost(String host) {
this.host = host
this
}
/**
* 設置接口地址
*
* @param apiName
* @return
*/
FunRequest setApiName(String apiName) {
this.apiName = apiName
this
}
/**
* 設置uri
*
* @param uri
* @return
*/
FunRequest setUri(String uri) {
this.uri = uri
this
}
/**
* 添加get參數
*
* @param key
* @param value
* @return
*/
FunRequest addArgs(Object key, Object value) {
args.put(key, value)
this
}
/**
* 添加post參數
*
* @param key
* @param value
* @return
*/
FunRequest addParam(Object key, Object value) {
params.put(key, value)
this
}
/**
* 添加json參數
*
* @param key
* @param value
* @return
*/
FunRequest addJson(Object key, Object value) {
json.put(key, value)
this
}
/**
* 添加header
*
* @param key
* @param value
* @return
*/
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
*/
FunRequest addHeader(List<Header> header) {
header.each {h -> headers << h}
this
}
/**
* 增加header中cookies
*
* @param cookies
* @return
*/
FunRequest addCookies(JSONObject cookies) {
headers << getCookies(cookies)
this
}
FunRequest setHeaders(List<Header> headers) {
this.headers.addAll(headers)
this
}
FunRequest setArgs(JSONObject args) {
this.args.putAll(args)
this
}
FunRequest setParams(JSONObject params) {
this.params.putAll(params)
this
}
FunRequest setJson(JSONObject json) {
this.json.putAll(json)
this
}
/**
* 獲取請求響應,兼容相關參數方法,不包括file
*
* @return
*/
JSONObject getResponse() {
return getHttpResponse(request == null ? getRequest() : request)
}
/**
* 獲取請求對象
*
* @return
*/
HttpRequestBase getRequest() {
if (request != null) request;
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
}
for (Header header in headers) {
request.addHeader(header)
}
logger.debug("請求信息:{}", new RequestInfo(this.request).toString())
request
}
@Override
String toString() {
JSONObject.fromObject(this).toString()
}
@Override
FunRequest clone() {
def fun = new FunRequest()
fun.setRequest(cloneRequest(getRequest()))
fun
}
static HttpRequestBase cloneRequest(HttpRequestBase base) {
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) {
return FunRequest.isGet().setUri(uri).setHeaders(headers).getRequest();
} else if (requestType == RequestType.POST || requestType == RequestType.FUN) {
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())) {
return FunRequest.isPost().setUri(uri).setHeaders(headers).setJson(JSONObject.fromObject(content)).getRequest();
} else if (value.equalsIgnoreCase(HttpClientConstant.ContentType_FORM.getValue())) {
return FunRequest.isPost().setUri(uri).setHeaders(headers).setParams(getJson(content.split("&"))).getRequest();
}
} else {
RequestException.fail("不支持的請求類型!");
}
}
}
- 鄭重聲明:文章首發於公眾號“FunTester”,禁止第三方(騰訊雲除外)轉載、發表。
技術類文章精選
- java一行代碼打印心形
- Linux性能監控軟件netdata中文漢化版
- 性能測試框架第二版
- 如何在Linux命令行界面愉快進行性能測試
- 圖解HTTP腦圖
- 將swagger文檔自動變成測試代碼
- 五行代碼構建靜態博客
- 基於java的直線型接口測試框架初探
- Selenium 4.0 Alpha更新日志
- Selenium 4.0 Alpha更新實踐