ITPub博客

首页 > 应用开发 > Java > Rest-Assured

Rest-Assured

Java 作者:孤独先生 时间:2020-09-19 22:22:59 0 删除 编辑

public class HttpDriver {

  // 增加http的配置文件

  private static String propertyFile = "src/main/resources/http/httpconf.properties";


  static {

    setRestAssuredConfig();

  }


  // Rest-Assured配置

  private static void setRestAssuredConfig() {

    String[] keyList = new String[] { "http.connection.timeout", "http.socket.timeout",

        "http.connection.manager.timeout" };


    Map<String, Object> httpClientParams = new HashMap<String, Object>();


    PropertiesUtil.loadProperties(propertyFile);

    for (int i = 0; i < keyList.length; i++) {

      httpClientParams.put(keyList[i], PropertiesUtil.getValue(keyList[i]));

    }


    config = RestAssured.config().httpClient(HttpClientConfig.httpClientConfig().addParams(httpClientParams));

    config = RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("UTF-8"));


    String host = PropertiesUtil.getValue("proxy.host");

    Integer port = Integer.valueOf(PropertiesUtil.getValue("proxy.port"));

    if (!StringUtils.isBlank(host) && port.equals(null)) {

      RestAssured.proxy(host, port);

    } else if (StringUtils.isBlank(host) && !port.equals(null)) {

      RestAssured.proxy(port);

    } else if (!StringUtils.isBlank(host) && port.equals(null)) {

      RestAssured.proxy(host);

    }


    RestAssured.replaceFiltersWith(new RequestLoggingFilter(), new ResponseLoggingFilter());

  }


  public static Response doPostWithNothing(String path) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, null, null, null, null);

  }


  public static Response doPostWithQueryParams(String path, Map<String, String> queryParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, null, queryParams, null, null);

  }


  public static Response doPostWithHeaders(String path, Map<String, String> headers) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, headers, null, null, null);

  }


  public static Response doPostWithFormParams(String path, Map<String, Object> formParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, null, null, formParams, null);

  }


  public static Response doPostWithHeadersAndFormParams(String path, Map<String, String> headers,

      Map<String, Object> formParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, headers, null, formParams, null);

  }


  public static Response doPostWithHeadersAndQueryParamsAndFormParams(String path, Map<String, String> headers,

      Map<String, String> queryParams, Map<String, Object> formParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, headers, queryParams, formParams, null);

  }


  public static Response doPostWithBodyParams(String path, Object bodyParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, null, null, null, bodyParams);

  }


  public static Response doPostWithHeadersAndBodyParams(String path, Map<String, String> headers, Object bodyParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, headers, null, null, bodyParams);

  }


  public static Response doPostWithHeadersAndQueryParamsAndBodyParams(String path, Map<String, String> headers,

      Map<String, String> queryParams, Object bodyParams) {

    return doHttpRequest(path, HttpConstants.POST_METHOD, headers, queryParams, null, bodyParams);

  }


  public static Response doGetWithNothing(String path) {

    return doHttpRequest(path, HttpConstants.GET_METHOD, null, null, null, null);

  }


  public static Response doGetWithHeaders(String path, Map<String, String> headers) {

    return doHttpRequest(path, HttpConstants.GET_METHOD, headers, null, null, null);

  }


  public static Response doGetWithQueryParams(String path, Map<String, String> queryParams) {

    return doHttpRequest(path, HttpConstants.GET_METHOD, null, queryParams, null, null);

  }


  public static Response doGetWithHeadersAndParams(String path, Map<String, String> headers,

      Map<String, String> queryParams) {

    return doHttpRequest(path, HttpConstants.GET_METHOD, headers, queryParams, null, null);

  }


  private static Response doHttpRequest(String path, String method, Map<String, String> headers,

      Map<String, String> queryParams, Map<String, Object> formParams, Object body) {

    Response response = null;


    boolean isSerial = false;


    if (formParams == null && body != null) {

      isSerial = true;

    }


    if (isSerial) {

      if (headers != null && queryParams != null && formParams != null) {

        response = given().headers(headers).queryParams(queryParams).formParams(formParams).request(method,

            path);

      } else if (headers != null && queryParams != null && formParams == null) {

        response = given().headers(headers).queryParams(queryParams).request(method, path);

      } else if (headers != null && queryParams == null && formParams != null) {

        response = given().headers(headers).formParams(formParams).request(method, path);

      } else if (headers != null && queryParams == null && formParams == null) {

        response = given().headers(headers).request(method, path);

      } else if (headers == null && queryParams != null && formParams != null) {

        response = given().queryParams(queryParams).formParams(formParams).request(method, path);

      } else if (headers == null && queryParams != null && formParams == null) {

        response = given().queryParams(queryParams).request(method, path);

      } else if (headers == null && queryParams == null && formParams != null) {

        response = given().formParams(formParams).request(method, path);

      } else if (headers == null && queryParams == null && formParams == null) {

        response = given().request(method, path);

      }

    } else {

      if (headers != null && queryParams != null && formParams != null) {

        response = given().headers(headers).queryParams(queryParams).body(formParams).request(method, path);

      } else if (headers != null && queryParams != null && formParams == null) {

        response = given().headers(headers).queryParams(queryParams).request(method, path);

      } else if (headers != null && queryParams == null && formParams != null) {

        response = given().headers(headers).body(formParams).request(method, path);

      } else if (headers != null && queryParams == null && formParams == null) {

        response = given().headers(headers).request(method, path);

      } else if (headers == null && queryParams != null && formParams != null) {

        response = given().queryParams(queryParams).body(formParams).request(method, path);

      } else if (headers == null && queryParams != null && formParams == null) {

        response = given().queryParams(queryParams).request(method, path);

      } else if (headers == null && queryParams == null && formParams != null) {

        response = given().body(formParams).request(method, path);

      } else if (headers == null && queryParams == null && formParams == null) {

        response = given().request(method, path);

      }

    }


    return response;

  }

}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69936314/viewspace-2722602/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 缺陷评估规范
请登录后发表评论 登录
全部评论

注册时间:2019-06-27

  • 博文量
    6
  • 访问量
    2536