利用restassured实现http/https接口请求

话不多说,直接上代码:

package com.nuanshui.frms.test.utils.http;

import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;

import java.net.URL;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.useRelaxedHTTPSValidation;

public  class RequestUtil {
    public static Response sendpostWithHttp(String surl, String str) throws Exception{
        String msg=null;
        URL url = new URL(surl);
        Response response = given().log().all().
                header("accept", "application/json").
                contentType("application/json").
                body(str).
                then().
                when().
                post(url);
        response.getBody().prettyPrint();

        return response;
    }
    public static ValidatableResponse sendgetWithHttp(String surl, String str) throws Exception{
        URL url = new URL(surl);
        ValidatableResponse response = given()
                .log().all()
                .queryParam(str)
                .when()
                .get(surl)
                .then()
                .log().all();
        return response;
    }
    public static Response sendpostWithHttps(String surl, String str) throws Exception{
        URL url = new URL(surl);
        useRelaxedHTTPSValidation();
        Response response = given().log().all().
                header("accept", "application/json").
                contentType("application/json").
                body(str).
                then().
                statusCode(200).
                when().
                post(url);
        response.getBody().prettyPrint();
        return response;
    }
    public static ValidatableResponse sendgetWithHttps(String surl, String str) throws Exception{
        URL url = new URL(surl);
        useRelaxedHTTPSValidation();
        ValidatableResponse response = given()
                .log().all()
                .queryParam(str)
                .when()
                .get(surl)
                .then()
                .log().all()
                .statusCode(200);
        return response;
    }
}

以上就是对http/https中的get和post进行请求的接口封装,是不是比httpclient的封装实现起来更简洁易懂呢?一起来学习restassurd把~

posted @ 2018-11-13 17:51  蜗小稂  阅读(2693)  评论(0编辑  收藏  举报