Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In REST Assured, how do I set a timeout?

Tags:

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

like image 509
TomH Avatar asked Oct 23 '17 12:10

TomH


People also ask

What is default timeout for Rest assured?

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

How do I set timeout in HTTP client?

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.

How do I add a delay in Rest assured?

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.

How do I set timeout in spring boot?

@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.


2 Answers

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"); 
like image 133
Luciano van der Veekens Avatar answered Sep 19 '22 16:09

Luciano van der Veekens


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); 
like image 36
Tillerino Avatar answered Sep 16 '22 16:09

Tillerino