Search in sources :

Example 1 with Configurable

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

the class HttpComponentsHttpInvokerRequestExecutorTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
    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);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
    executor.setConnectionRequestTimeout(4567);
    HttpPost httpPost2 = executor.createHttpPost(config);
    assertNotNull(httpPost2.getConfig());
    assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
    // Default connection timeout merged
    assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 2 with Configurable

use of org.apache.http.client.methods.Configurable in project spring-cloud-netflix by spring-cloud.

the class GZIPCompression method timeoutPropertiesAreApplied.

@Test
public void timeoutPropertiesAreApplied() {
    addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000", "zuul.host.connect-timeout-millis=2100");
    setupContext();
    CloseableHttpClient httpClient = getFilter().newClient();
    Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
    RequestConfig config = ((Configurable) httpClient).getConfig();
    assertEquals(11000, config.getSocketTimeout());
    assertEquals(2100, config.getConnectTimeout());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) Configurable(org.apache.http.client.methods.Configurable) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with Configurable

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

the class HttpComponentsAsyncClientHttpRequestFactory method createAsyncRequest.

@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpAsyncClient asyncClient = getHttpAsyncClient();
    startAsyncClient();
    HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
    postProcessHttpRequest(httpRequest);
    HttpContext context = createHttpContext(httpMethod, uri);
    if (context == null) {
        context = HttpClientContext.create();
    }
    // Request configuration not set in the context
    if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
        // Use request configuration given by the user, when available
        RequestConfig config = null;
        if (httpRequest instanceof Configurable) {
            config = ((Configurable) httpRequest).getConfig();
        }
        if (config == null) {
            config = createRequestConfig(asyncClient);
        }
        if (config != null) {
            context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
        }
    }
    return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) Configurable(org.apache.http.client.methods.Configurable)

Example 4 with Configurable

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

the class HttpComponentsHttpInvokerRequestExecutorTests 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);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
    executor.setConnectTimeout(5000);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    RequestConfig requestConfig = httpPost.getConfig();
    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) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 5 with Configurable

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

the class HttpComponentsHttpInvokerRequestExecutorTests 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);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor() {

        @Override
        public HttpClient getHttpClient() {
            return client;
        }
    };
    executor.setReadTimeout(5000);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    RequestConfig requestConfig = httpPost.getConfig();
    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);
    executor.setReadTimeout(7000);
    HttpPost httpPost2 = executor.createHttpPost(config);
    RequestConfig requestConfig2 = httpPost2.getConfig();
    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) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)10 Configurable (org.apache.http.client.methods.Configurable)10 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 Test (org.junit.Test)4 HttpPost (org.apache.http.client.methods.HttpPost)3 Test (org.junit.jupiter.api.Test)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 HttpContext (org.apache.http.protocol.HttpContext)2 URI (java.net.URI)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 HttpClient (org.apache.http.client.HttpClient)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 ContentType (org.apache.http.entity.ContentType)1 StringEntity (org.apache.http.entity.StringEntity)1