RestAssured-TestNG测试RestfulAPI引发连接超时错误

问题描述 投票:0回答:2

我正在将TestNG与RestAssured框架一起使用以测试RestAPI。

执行到达此行的那一刻,httpRequest.request,它会引发连接超时错误

我在该行中缺少任何内容吗?它没有引发任何语法错误。

   import org.testng.annotations.BeforeMethod;
   import static io.restassured.RestAssured.ntlm; 
   import static io.restassured.RestAssured.basic; 
   import org.testng.annotations.Test;
   import io.restassured.RestAssured;
   import io.restassured.http.Method;
   import io.restassured.response.Response;
   import io.restassured.specification.RequestSpecification;


  public class RestApi_Incidents {

@BeforeMethod
 public void beforeMethod() {
    System.out.println("before method");

}

@Test
void GetIncidentAPI(){      

    try{


    RestAssured.baseURI = "https://xxx/api/data/v8.2";
     RestAssured.port = 80;
     RestAssured.basePath = "/incident";
     RestAssured.authentication = basic("userid", "pwd!");
     //RestAssured.authentication = ntlm("uid", "pws!", null, "uat");   

     RequestSpecification httpRequest = RestAssured.given();

      Response response =httpRequest.get();
    }
    catch (Exception ex){

        System.out.println(ex.toString());

    }   

} 

}

testng rest-assured
2个回答
0
投票

请这样使用

RestAssured.baseURI = "https://xxx/api/data/v8.2/";
 RestAssured.port = 80;
 RestAssured.basePath = "/incidents/resource/HealthCheckApp";
 RestAssured.authentication = basic("username", "password");
 RestAssured.authentication = ntlm("myuserid", null, null, "uat");   
 RequestSpecification httpRequest = RestAssured.given();
 Response response =httpRequest.get("/DetailsView");

0
投票

您可以按照以下说明重构您的协议。我无法复制连接超时错误,因为本地主机上没有主机

package api.application.zippopotam;

import io.restassured.authentication.AuthenticationScheme;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.internal.http.HTTPBuilder;
import org.testng.annotations.BeforeMethod;

import static io.restassured.RestAssured.ntlm;

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;


public class RestApi_Incidents {

    public static RequestSpecification requestSpecification ;

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("before method");



        requestSpecification = new RequestSpecBuilder().
                                                setBaseUri("https://xxx/api/data/v8.2/incidents").
                                                setRelaxedHTTPSValidation().
                                                setBasePath("HealthCheckApp/DetailsView").build()
                                                .auth().basic("userid", "pwd!");

    }

    @Test
    void GetIncidentAPI(){

        try{


            Response aresponse =  RestAssured.
                    given().
                    spec(requestSpecification).
                    when().
                    get().
                    then().
                    extract().
                    response();

            System.out.println("before getBody");

            String aresponseBody = aresponse.getBody().asString();

            System.out.println("response is " + aresponseBody);
        }
        catch (Exception ex){

            System.out.println(ex.toString());

        }


    }

}

输出

获取未知的主机期望错误:您提供的主机名不正确

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.