In REST Assured, how do I set a timeout?
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);
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");
Following configuration worked for me.
RestAssured.config=RestAssuredConfig.config()
.httpClient(HttpClientConfig.httpClientConfig()
.setParam("http.socket.timeout",1000)
.setParam("http.connection.timeout", 1000));