Search in sources :

Example 6 with Configurable

use of org.apache.http.client.methods.Configurable in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method localSettingsOverrideClientDefaultSettings.

@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
    RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
    CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
    hrf.setConnectTimeout(5000);
    RequestConfig requestConfig = retrieveRequestConfig(hrf);
    assertEquals(5000, requestConfig.getConnectTimeout());
    assertEquals(6789, requestConfig.getConnectionRequestTimeout());
    assertEquals(-1, requestConfig.getSocketTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 7 with Configurable

use of org.apache.http.client.methods.Configurable in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
    RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
    CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
    assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
    hrf.setConnectionRequestTimeout(4567);
    RequestConfig requestConfig = retrieveRequestConfig(hrf);
    assertNotNull(requestConfig);
    assertEquals(4567, requestConfig.getConnectionRequestTimeout());
    // Default connection timeout merged
    assertEquals(1234, requestConfig.getConnectTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 8 with Configurable

use of org.apache.http.client.methods.Configurable in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method mergeBasedOnCurrentHttpClient.

@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
    RequestConfig defaultConfig = RequestConfig.custom().setSocketTimeout(1234).build();
    final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {

        @Override
        public HttpClient getHttpClient() {
            return client;
        }
    };
    hrf.setReadTimeout(5000);
    RequestConfig requestConfig = retrieveRequestConfig(hrf);
    assertEquals(-1, requestConfig.getConnectTimeout());
    assertEquals(-1, requestConfig.getConnectionRequestTimeout());
    assertEquals(5000, requestConfig.getSocketTimeout());
    // Update the Http client so that it returns an updated  config
    RequestConfig updatedDefaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
    when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
    hrf.setReadTimeout(7000);
    RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
    assertEquals(1234, requestConfig2.getConnectTimeout());
    assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
    assertEquals(7000, requestConfig2.getSocketTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)8 Configurable (org.apache.http.client.methods.Configurable)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 Test (org.junit.Test)6 HttpPost (org.apache.http.client.methods.HttpPost)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 HttpContext (org.apache.http.protocol.HttpContext)2 HttpClient (org.apache.http.client.HttpClient)1 CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)1 HttpAsyncClient (org.apache.http.nio.client.HttpAsyncClient)1