Allow default settings of a custom HttpAsyncClient to apply

This is a rework of 71783c5 for SPR-12540 for the async extension that
was not merging the internal RequestConfig as it should.

Issue: SPR-13125
This commit is contained in:
Stephane Nicoll
2015-06-15 18:12:57 +02:00
parent 10cb80a500
commit 5236eb6b63
4 changed files with 92 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +16,21 @@
package org.springframework.http.client;
import java.net.URI;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Stephane Nicoll
*/
public class HttpComponentsAsyncClientHttpRequestFactoryTests extends AbstractAsyncHttpRequestFactoryTestCase {
@@ -38,4 +47,43 @@ public class HttpComponentsAsyncClientHttpRequestFactoryTests extends AbstractAs
assertHttpMethod("patch", HttpMethod.PATCH);
}
@Test
public void customHttpAsyncClientUsesItsDefault() throws Exception {
HttpComponentsAsyncClientHttpRequestFactory factory =
new HttpComponentsAsyncClientHttpRequestFactory();
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest)
factory.createAsyncRequest(uri, HttpMethod.GET);
assertNull("No custom config should be set with a custom HttpAsyncClient",
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
}
@Test
public void defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization() throws Exception {
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build())
.build();
HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client);
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest)
factory.createAsyncRequest(uri, HttpMethod.GET);
assertNull("No custom config should be set with a custom HttpClient",
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
factory.setConnectionRequestTimeout(4567);
HttpComponentsAsyncClientHttpRequest request2 = (HttpComponentsAsyncClientHttpRequest)
factory.createAsyncRequest(uri, HttpMethod.GET);
Object requestConfigAttribute = request2.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
assertNotNull(requestConfigAttribute);
RequestConfig requestConfig = (RequestConfig) requestConfigAttribute;
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// No way to access the request config of the HTTP client so no way to "merge" our customizations
assertEquals(-1, requestConfig.getConnectTimeout());
}
}