use of org.apache.http.client.config.RequestConfig in project spring-boot by spring-projects.
the class RestTemplateBuilderTests method connectTimeoutCanBeConfiguredOnHttpComponentsRequestFactory.
@Test
public void connectTimeoutCanBeConfiguredOnHttpComponentsRequestFactory() {
ClientHttpRequestFactory requestFactory = this.builder.requestFactory(HttpComponentsClientHttpRequestFactory.class).setConnectTimeout(1234).build().getRequestFactory();
assertThat(((RequestConfig) ReflectionTestUtils.getField(requestFactory, "requestConfig")).getConnectTimeout()).isEqualTo(1234);
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactoryTests method retrieveRequestConfig.
private RequestConfig retrieveRequestConfig(HttpComponentsClientHttpRequestFactory factory) throws Exception {
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) factory.createRequest(uri, HttpMethod.GET);
return (RequestConfig) request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactoryTests method assertCustomConfig.
@Test
public void assertCustomConfig() throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(httpClient);
hrf.setConnectTimeout(1234);
hrf.setConnectionRequestTimeout(4321);
hrf.setReadTimeout(4567);
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) hrf.createRequest(uri, HttpMethod.GET);
Object config = request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
assertNotNull("Request config should be set", config);
assertTrue("Wrong request config type" + config.getClass().getName(), RequestConfig.class.isInstance(config));
RequestConfig requestConfig = (RequestConfig) config;
assertEquals("Wrong custom connection timeout", 1234, requestConfig.getConnectTimeout());
assertEquals("Wrong custom connection request timeout", 4321, requestConfig.getConnectionRequestTimeout());
assertEquals("Wrong custom socket timeout", 4567, requestConfig.getSocketTimeout());
}
use of org.apache.http.client.config.RequestConfig 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());
}
use of org.apache.http.client.config.RequestConfig in project apn-proxy by apn-proxy.
the class TestProxyWithHttpClient method test.
private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(2000);
cm.setDefaultMaxPerRoute(40);
cm.setDefaultConnectionConfig(connectionConfig);
CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm).disableContentCompression().disableCookieManagement().build();
HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());
RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true).setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpGet request = new HttpGet(uri);
request.setConfig(config);
try {
CloseableHttpResponse httpResponse = httpClient.execute(request);
Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseHandler.handleResponse(httpResponse);
httpResponse.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
Aggregations