Preliminary Study on Test Framework of Linear Interface Based on java

In the process of using the java language as an interface test, it is found that the simplicity of the java language is not as simple as that of scripting languages, such as python. Many functions Python can be accomplished in a few ways, one line of code, several lines of java are required, and sometimes it is not easy to understand.Recently I came into contact with a word "straight line" code.Having looked at it, I feel I've rewritten a straight-line code style interface request framework.

The source code is as follows:


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

/**
 * Rewrite FanLibrary, using object-oriented thinking
 */
public class FunRequest extends FanLibrary {

    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 parameter
     */
    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
     */
    public static FunRequest isGet() {
        new FunRequest(RequestType.GET)
    }

    /**
     * Get post object
     *
     * @return
     */
    public static FunRequest isPost() {
        new FunRequest(RequestType.POST)
    }

    /**
     * Set host
     *
     * @param host
     * @return
     */
    public FunRequest setHost(String host) {
        this.host = host
        this
    }

    /**
     * Set interface address
     *
     * @param apiName
     * @return
     */
    public FunRequest setApiName(String apiName) {
        this.apiName = apiName
        this
    }

    /**
     * Setting uri
     *
     * @param uri
     * @return
     */
    public FunRequest setUri(String uri) {
        this.uri = uri
        this
    }

    /**
     * Add get parameter
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addArgs(Object key, Object value) {
        args.put(key, value)
        this
    }

    /**
     * Add post parameter
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addParam(Object key, Object value) {
        params.put(key, value)
        this
    }

    /**
     * Add json parameter
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addJson(Object key, Object value) {
        json.put(key, value)
        this
    }

    /**
     * Add header
     *
     * @param key
     * @param value
     * @return
     */
    public 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
     */
    public FunRequest addHeader(List<Header> header) {
        header.each { h -> headers << h }
        this
    }

    /**
     * Increase cookies in header
     *
     * @param cookies
     * @return
     */
    public FunRequest addCookies(JSONObject cookies) {
        headers << getCookies(cookies)
        this
    }

    /**
     * Get request response, compatible with related parameter methods, excluding 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)
    }


    /**
     * Get Request Object
     *
     * @return
     */
    public HttpRequestBase getRequest() {
        logger.debug("Request information:{}",new RequestInfo(this.request).toString())
        this.request
    }

    @Override
    public String toString() {
        JSONObject.fromObject(this).toString()
    }
}

Use it as follows:

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

Selected Technical Articles

  1. java line of code to print heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Schema Sharing
  4. Performance Test Framework
  5. How to have a pleasant performance test on the Linux command line interface
  6. Graphic HTTP Brain Map
  7. How to test probabilistic business interfaces
  8. httpclient handles multiuser simultaneous online
  9. Automatically turn swagger documents into test code
  10. Five lines of code to build a static blog
  11. How httpclient handles 302 redirects
  12. Preliminary Study on Test Framework of Linear Interface Based on java

Selected non-technical articles

  1. Why choose software testing as your career path?
  2. Ten steps to becoming a great Java developer
  3. Programming thinking written to everyone
  4. Barriers to automated testing

Public Number Map ☢️ Come Together~FunTester

Tags: Programming JSON Java Apache Python

Posted on Thu, 05 Sep 2019 19:50:52 -0700 by simulant