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
to REST assured. I tried with all the solutions provided in this thread, still timeout is 60 secs(default time). I tried following solutions. 1. RestAssured.config = RestAssured.config().httpClient(HttpClientConfig.httpClientConfig()
The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.
In the past I've used awaitility, it allows you to wait for a response from the service before kicking off another call. https://github.com/awaitility/awaitility. You can return an extracted response and wait for the status code/body to return a value. Save this answer.
@Transactional Timeouts One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.
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");
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With