在REST安全中,如何设置超时? [英] In REST Assured, how do I set a timeout?
本文介绍了在REST安全中,如何设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用RestAssured 2.8.0 我正在尝试设置自己的超时时间(用于网关超时), 因此,如果X毫秒后没有收到响应,我想中止.
I'm using RestAssured 2.8.0 and I'm trying to set my own timeout (for gateway timeout), so if I don't get response after X milliseconds I want to abort.
我尝试过:
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)
似乎没有任何作用. 它不会终止我的查询
nothing seem to work. It doesn't abort my query
推荐答案
您可以通过设置HTTP客户端参数来配置超时:
You can configure timeouts by setting HTTP client parameters:
RestAssuredConfig config = RestAssured.config()
.httpClient(HttpClientConfig.httpClientConfig()
.setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000)
.setParam(CoreConnectionPNames.SO_TIMEOUT, 1000));
given().config(config).post("http://localhost:8884");
这篇关于在REST安全中,如何设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文