首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RestAssured - TestNG测试RestfulAPI抛出连接超时错误

RestAssured - TestNG测试RestfulAPI抛出连接超时错误
EN

Stack Overflow用户
提问于 2019-10-10 04:36:29
回答 3查看 1.2K关注 0票数 1

我正在使用带有RestAssured框架的TestNG来测试RestAPI。

一旦执行命中httpRequest.request这一行,就会抛出连接超时错误

我是不是漏掉了什么?它没有抛出任何语法错误。

代码语言:javascript
复制
   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());

    }   

} 

}

EN

回答 3

Stack Overflow用户

发布于 2019-10-10 05:30:22

请这样使用

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-10-10 07:12:37

你可以重构你的协议,如下所示。我无法复制连接超时错误,因为主机在我的本地上不可用

代码语言:javascript
复制
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());

        }


    }

}

输出

获取未知主机预期错误:,因为您提供的主机名不正确

票数 0
EN

Stack Overflow用户

发布于 2019-10-15 18:26:52

请点击下面的链接。我已经用代码解释过了:

https://stackoverflow.com/questions/58343693/timeout-error-in-restassured-whereas-the-service-giving-response-in-postman-soap/58392345#58392345

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58311927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档