TomH
TomH

Reputation: 313

In REST Assured, how do I set a timeout?

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

Upvotes: 31

Views: 36469

Answers (3)

kiranjith
kiranjith

Reputation: 211

Following configuration worked for me.

RestAssured.config=RestAssuredConfig.config()
                        .httpClient(HttpClientConfig.httpClientConfig()
                                .setParam("http.socket.timeout",1000)
                                .setParam("http.connection.timeout", 1000));

Upvotes: 11

Tillerino
Tillerino

Reputation: 748

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

Upvotes: 12

Luciano van der Veekens
Luciano van der Veekens

Reputation: 6577

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

Upvotes: 37

Related Questions