【问题标题】:Timeout error in RestAssured whereas the service giving response in postman/soapUIRestAssured 中的超时错误,而服务在 postman/soapUI 中给出响应
【发布时间】:2019-10-11 14:46:20
【问题描述】:

此代码给出超时错误,而服务在 postman/soapUI 中给出响应

我正在尝试自动化其余服务。该服务运行良好的soapUI,而在restAssured 中进行自动化时会出现超时错误。

import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class AddUsers {

    @Test

    public void addUsers()

    {

        RestAssured.baseURI = "http://reqres.in";

        given().header("Content-Type","application/json").body("{\r\n" + 
                "    \"name\": \"Mallik\",\r\n" + 
                "    \"job\": \"TestLead\"\r\n" + 
                "}").when().post("/api/users");



    }


}

【问题讨论】:

    标签: java rest-assured rest-assured-jsonpath


    【解决方案1】:

    您需要使用 https。我得到了正确的输出

        RestAssured.baseURI = "https://reqres.in";
        Response resp = given().header("Content-Type", "application/json")
                .body("{\n" + "    \"name\": \"Mallik\",\n" + "    \"job\": \"leader\"\n" + "}").when()
                .post("/api/users");
        System.out.println(resp.getStatusCode());
        System.out.println(resp.asString());
    

    【讨论】:

      【解决方案2】:

      尝试使用以下内容。我为 addUsers 和 getUsers 添加了代码。我建议您为传递 POST 端点的主体创建 POJO 类。

      import org.testng.annotations.Test;
      import io.restassured.RestAssured;
      import io.restassured.http.ContentType;
      import io.restassured.response.Response;
      import net.serenitybdd.rest.SerenityRest;
      
      public class Test11 {
      
          static Response response;
      
          @Test
          public void addUsers() {
              RestAssured.baseURI = "http://reqres.in";
              response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
                      .log().all()
                      .when().body("{\r\n" + 
                              "    \"name\": \"Mallik\",\r\n" + 
                              "    \"job\": \"TestLead\"\r\n" + 
                              "}")
                      .post("/api/users")
                      .then().log().all().extract().response();
          }
      
          @Test
          public void getUsers() {
              RestAssured.baseURI = "http://reqres.in";
              response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
                      .log().all()
                      .when()
                      .get("/api/users?id=3")
                      .then().log().all().extract().response();
          }
      }
      

      如果这解决了您的问题,请告诉我。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,一旦我配置了代理主机名和端口,它就得到了解决:

        RestAssured.given(restAssuredSpecBuilder.build()).proxy("http.proxy.domain.com",portNumber)
        

        【讨论】:

          猜你喜欢
          • 2020-02-17
          • 2019-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多