In practice Performance Test Framework 2nd Edition In the process, I implemented a concurrent object creation for a single HttpRequestBase object. Previously, I used to use a unique HttpRequestBase object for multithreaded requests. At present, this is OK, but in order to prevent unexpected use of BUG and uniform concurrent construction methods in the future, I attempted to copy an HttpRequestBase object.The reason is that the previously encapsulated deep copy methods do not apply to the implementation classes of HttpRequestBase objects such as httpget and httppost because the Serializable interface is not implemented.So we have written a copy method of HttpRequestBase object for your reference.
Here is the code for the FunRequest class, with the deep copy of the static method at the end.
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 /** * Rewrite FanLibrary, using object-oriented thinking */ public class FunRequest extends FanLibrary implements Serializable,Cloneable { private static final long serialVersionUID = -4153600036943378727L; static Logger logger = LoggerFactory.getLogger(FunRequest.class) /** * Request type, true get, false post */ RequestType requestType /** * Request Object */ HttpRequestBase request /** * host address */ String host /** * Interface Address */ String apiName /** * Request address, spliced by host and apiname if empty */ String uri /** * header aggregate */ List<Header> headers = new ArrayList<>() /** * get parameter */ JSONObject args = new JSONObject() /** * post Parameters, Form */ JSONObject params = new JSONObject() /** * json parameter */ JSONObject json = new JSONObject() /** * Construction method * * @param requestType */ private FunRequest(RequestType requestType) { this.requestType = requestType } /** * Get get object * * @return */ static FunRequest isGet() { new FunRequest(RequestType.GET) } /** * Get post object * * @return */ static FunRequest isPost() { new FunRequest(RequestType.POST) } /** * Set host * * @param host * @return */ FunRequest setHost(String host) { this.host = host this } /** * Set interface address * * @param apiName * @return */ FunRequest setApiName(String apiName) { this.apiName = apiName this } /** * Setting uri * * @param uri * @return */ FunRequest setUri(String uri) { this.uri = uri this } /** * Add get parameter * * @param key * @param value * @return */ FunRequest addArgs(Object key, Object value) { args.put(key, value) this } /** * Add post parameter * * @param key * @param value * @return */ FunRequest addParam(Object key, Object value) { params.put(key, value) this } /** * Add json parameter * * @param key * @param value * @return */ FunRequest addJson(Object key, Object value) { json.put(key, value) this } /** * Add header * * @param key * @param value * @return */ FunRequest addHeader(Object key, Object value) { headers << getHeader(key.toString(), value.toString()) this } /** * Add header * * @param header * @return */ public FunRequest addHeader(Header header) { headers.add(header) this } /** * Add header s in bulk * * @param header * @return */ FunRequest addHeader(List<Header> header) { header.each {h -> headers << h} this } /** * Increase cookies in header * * @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 } /** * Get request response, compatible with related parameter methods, excluding file * * @return */ JSONObject getResponse() { return getHttpResponse(request == null ? getRequest() : request) } /** * Get Request Object * * @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("Request information:{}", 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("Failed to parse response!", 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("Unsupported request type!"); } } }
- Solemn statement: The article was first published under the public number FunTester, which prohibits third parties (except Tencent Cloud) from reproducing and publishing it.
Selected Technical Articles
- java line of code to print heart
- Chinese Language Version of Linux Performance Monitoring Software netdata
- Performance Test Framework 2nd Edition
- How to have a pleasant performance test on the Linux command line interface
- Graphic HTTP Brain Map
- Automatically turn swagger documents into test code
- Five lines of code to build a static blog
- Preliminary Study on Test Framework of Linear Interface Based on java
- Selenium 4.0 Alpha Update Log
- Selenium 4.0 Alpha Update Practice
Selected non-technical articles
- Why choose software testing as your career path?
- Programming thinking written to everyone
- 7 Skills to Become an Automated Test
- How to Introduce Automated Testing in DevOps
- Summary of Web-side automated test failures
- How to Introduce Automated Testing in DevOps
- How testers become agents of change
- Skills for writing test cases
- Common excuses for testers