使用 Restassured 测试后端超时 [英] Testing backend timeout with Restassured
问题描述
我使用 RestAssured 2.4.1 来测试服务堆栈,其第一个服务通过 REST 公开.
I'm using RestAssured 2.4.1 to test a service stack whose first service is exposed via REST.
现在,我想测试后端没有响应时的行为,REST 服务应该检测和处理这种情况.不幸的是,RestAssured 在 REST 服务检测到后端超时之前终止了 POST 请求.
Now, I want to test the behaviour when the backend is not responding, a situation that the REST service is supposed to detect and handle. Unfortunately, RestAssured terminates the POST request before the REST service detects the backend timeout.
如何增加 RestAssured 的相应超时时间?我正在尝试以下但没有成功
How can I increase the corresponding timeout of RestAssured? I'm trying the following without success
RestAssuredConfig config = RestAssured.config();
config.getHttpClientConfig()
.setParam(ClientPNames.CONN_MANAGER_TIMEOUT, 0) // HttpConnectionManager connection return time
.setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 0) // Remote host connection time
.setParam(CoreConnectionPNames.SO_TIMEOUT, 0) ; // Remote host response time
given()
.config(config)
. ...
推荐答案
我使用的是 RestAssured 3.0.1,但这就是我修复此问题的方式 - 但在全局范围内(我实际上想降低默认超时).我也使用 SystemDefaultHttpClient,因为我想重用连接(HTTP keepalive).因此它不适合 100%,但可能会给出指示.
I'm using RestAssured 3.0.1, but this is how I fixed this - yet globally (I actually wanted to lower the default timeout). I also use the SystemDefaultHttpClient, as I want to reuse connections (HTTP keepalive). Thus it does not fit 100%, but might give pointers.
HttpClientConfig clientConfig = RestAssured.config().getHttpClientConfig();
clientConfig = clientConfig.httpClientFactory(new HttpClientConfig.HttpClientFactory() {
@Override
public HttpClient createHttpClient() {
HttpClient rv = new SystemDefaultHttpClient();
HttpParams httpParams = rv.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5 * 1000); //Wait 5s for a connection
HttpConnectionParams.setSoTimeout(httpParams, 60 * 1000); // Default session is 60s
return rv;
}
});
//This is necessary to ensure, that the client is reused.
clientConfig = clientConfig.reuseHttpClientInstance();
RestAssured.config = RestAssured.config().httpClient(clientConfig);
这篇关于使用 Restassured 测试后端超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!