28

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.

I tried:

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);

}

or

ConnectionConfig connectionConfig = new ConnectionConfig()
            .closeIdleConnectionsAfterEachResponseAfter(10L, TimeUnit.MILLISECONDS);
RestAssuredConfig restAssuredConfig = new RestAssuredConfig().connectionConfig(connectionConfig);

I also tried to add

.queryParam("SO_TIMEOUT", 10)

or

.queryParam("CONNECTION_MANAGER_TIMEOUT", 10)

nothing seem to work. It doesn't abort my query

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
TomH
  • 283
  • 1
  • 4
  • 6

3 Answers3

35

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");
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • 10
    The CoreConnectionPNames seems to be deprecated. Is there a new way to configure the timeouts ? – Walter Kelt May 11 '18 at 12:55
  • 2
    See https://github.com/rest-assured/rest-assured/issues/935 and https://github.com/rest-assured/rest-assured/issues/497 – Christoph May 08 '20 at 12:51
11

Since CoreConnectionPNames is deprecated here's a newer way. This works for Apache HTTP client 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);
Tillerino
  • 588
  • 4
  • 9
  • 14
    You should get a `ClassCastException` this way as discussed [here](https://github.com/rest-assured/rest-assured/issues/497). Restassured doesn't support uses of external httpClient. You currently have to produce an `AbstractHttpClient` – Pdv Dec 11 '18 at 08:32
  • any fix on that :? with the ClassCastException ? – RosS Apr 10 '19 at 09:01
  • Documented here https://github.com/rest-assured/rest-assured/wiki/Usage#http-client-config – arulraj.net Sep 09 '19 at 05:12
  • @arulraj.net As far as I can see there is no fix for that. The only option is to continue to use the deprecated classes. See https://github.com/rest-assured/rest-assured/issues/497 Or do you see any other alternative? – Christoph May 07 '20 at 12:52
8

Following configuration worked for me.

RestAssured.config=RestAssuredConfig.config()
                        .httpClient(HttpClientConfig.httpClientConfig()
                                .setParam("http.socket.timeout",1000)
                                .setParam("http.connection.timeout", 1000));
kiranjith
  • 141
  • 1
  • 5