I'm using RestAssured 2.4.1 to test a service stack whose first service is exposed via REST.
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.
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)
. ...
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);
What exactly is timing out?
If your test is using org.junit.Test then you can set the timeout explicitly. The following sets the timeout to 60 seconds (60000 milliseconds):
@Test(timeout = 60000)
public void canCallFullyQualifiedUrlsWithoutPortDefined() throws Exception {
// This test hangs forever unless it works
get("http://filehost-semc-rss-dev.s3.amazonaws.com/testfile1.txt");
}
You can get the actual response time from rest-assured:
@Test public void
response_time_can_be_extracted() {
long time =
given().
param("firstName", "John").
param("lastName", "Doe").
when().
get("/greet").
then().
extract().response().time();
assertThat(time, greaterThan(0L));
}