作者:会说话de狗尾草 | 来源:互联网 | 2023-01-11 20:15
我正在使用RestAssured 2.8.0并且我正在尝试设置自己的超时(对于网关超时),所以如果我在X毫秒后没有得到响应我想要中止.
我试过了:
public static ValidatableResponse postWithConnectionConfig(String url, String body, RequestSpecification requestSpecification, ResponseSpecification responseSpecification) {
ConnectionConfig.CloseIdleConnectionConfig closeIdleCOnnectionConfig= new ConnectionConfig.CloseIdleConnectionConfig(1L, TimeUnit.MILLISECONDS);
ConnectionConfig cOnnectionConfig= new ConnectionConfig(closeIdleConnectionConfig);
RestAssuredConfig restAssuredCOnfig= new RestAssuredConfig().connectionConfig(connectionConfig);
return given().specification(requestSpecification)
.body(body)
.config(restAssuredConfig)
.post(url)
.then()
.specification(responseSpecification);
}
要么
ConnectionConfig cOnnectionConfig= new ConnectionConfig()
.closeIdleConnectionsAfterEachResponseAfter(10L, TimeUnit.MILLISECONDS);
RestAssuredConfig restAssuredCOnfig= new RestAssuredConfig().connectionConfig(connectionConfig);
我也尝试添加
.queryParam("SO_TIMEOUT", 10)
要么
.queryParam("CONNECTION_MANAGER_TIMEOUT", 10)
似乎没什么用.它不会中止我的查询
1> Luciano van ..:
您可以通过设置HTTP客户端参数来配置超时:
RestAssuredConfig cOnfig= RestAssured.config()
.httpClient(HttpClientConfig.httpClientConfig()
.setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000)
.setParam(CoreConnectionPNames.SO_TIMEOUT, 1000));
given().config(config).post("http://localhost:8884");
CoreConnectionPNames似乎已被弃用.有没有新的方法来配置超时?
2> Tillerino..:
由于CoreConnectionPNames
不推荐使用,因此这是一种较新的方法。这适用于Apache HTTP客户端4.5.3:
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
...
RequestConfig requestCOnfig= RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();
HttpClientConfig httpClientFactory = HttpClientConfig.httpClientConfig()
.httpClientFactory(() -> HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.build());
RestAssured.cOnfig= RestAssured
.config()
.httpClient(httpClientFactory);
您应该按照[此处](https://github.com/rest-assured/rest-assured/issues/497)的方式通过这种方式获取“ ClassCastException”。请放心,不支持使用外部httpClient。当前,您必须产生一个“ AbstractHttpClient”