spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

RestTestClient not using configured redirect settings

Open OrangeDog opened this issue 2 weeks ago • 7 comments

I'm using Boot 4.2.0 and migrating from @AutoConfigureTestRestTemplate to @AutoConfigureRestTestClient.

The underlying implementation is org.apache.httpcomponents.client5:httpclient5:5.6.5.

In my application properties I have spring.http.clients.redirects: dont_follow.

The TestRestTemplate is correctly not following redirects, but the RestTestClient is, causing exceptions such as this when the controller under test returns e.g. Location: https://test/:

org.apache.hc.client5.http.HttpHostConnectException: Connect to https://test:443 failed: Connection timed out: getsockopt
	at java.base/sun.nio.ch.Net.pollConnect(Native Method)
	at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:639)
	at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:550)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:284)
	at java.base/java.net.Socket.connect(Socket.java:659)
	at org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:219)
	at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:547)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:164)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:174)
	at org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:144)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ContentCompressionExec.execute(ContentCompressionExec.java:138)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.HttpRequestRetryExec.execute(HttpRequestRetryExec.java:112)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.RedirectExec.execute(RedirectExec.java:110)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:186)
	at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:87)
	at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:55)
	at org.apache.hc.client5.http.classic.HttpClient.executeOpen(HttpClient.java:183)
	at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:99)
	at org.springframework.http.client.AbstractStreamingClientHttpRequest.executeInternal(AbstractStreamingClientHttpRequest.java:87)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:80)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeWithRequestAndBody(AbstractBufferingClientHttpRequest.java:89)
	at org.springframework.http.client.InterceptingClientHttpRequest$EndOfChainRequestExecution.execute(InterceptingClientHttpRequest.java:102)
	at org.springframework.test.web.servlet.client.DefaultRestTestClient$WiretapInterceptor.intercept(DefaultRestTestClient.java:536)
	at org.springframework.http.client.ClientHttpRequestInterceptor.lambda$apply$0(ClientHttpRequestInterceptor.java:89)
	at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:73)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:50)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:80)
	at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:575)

OrangeDog avatar Nov 20 '26 16:11 OrangeDog

Hi @OrangeDog , I'd like to work on this issue. I'll investigate the configuration path and prepare a fix once the issue has been triaged.

Triyaambak avatar Nov 21 '26 12:11 Triyaambak

Related: #42879, #43431

OrangeDog avatar Nov 21 '26 12:11 OrangeDog

Pardon ?

Triyaambak avatar Nov 21 '26 13:11 Triyaambak

@Triyaambak #42879 is where spring.http.clients.redirects was originally introduced.

Currently RestTestClientTestAutoConfiguration#getBuilder calls RestTestClient.bindToServer() without arguments, so it doesn't apply spring.http.clients.* / ClientHttpRequestFactorySettings and falls back to the client library's default redirect behavior.

manthansharma6767 avatar Nov 21 '26 13:11 manthansharma6767

Ahh , i gotcha , thanks @manthansharma6767

Could you confirm whether this issue is ready for external contributions, or should I wait for triage?

Triyaambak avatar Nov 21 '26 13:11 Triyaambak

@Triyaambak It's still labeled status: waiting-for-triage, so definitely wait for the Spring team to triage it before submitting a PR.

manthansharma6767 avatar Nov 21 '26 13:11 manthansharma6767

None of the RestTestClient methods allow customising this.

It appears the only way is to start with new DefaultRestTestClientBuilder(builder) passing e.g. the autoconfigured RestClient.Builder, but DefaultRestTestClientBuilder is package-private.

e.g. for a @SpringBootTest with no additional message parsing config:

@Bean
public RestTestClient restTestClient(RestClient.Builder clientBuilder, ApplicationContext applicationContext) throws Exception {
    Class<?> clazz = Class.forName("org.springframework.test.web.servlet.client.DefaultRestTestClientBuilder");
    Constructor<?> constructor = clazz.getDeclaredConstructor(RestClient.Builder.class);
    constructor.setAccessible(true);
    RestTestClient.Builder<?> builder = (RestTestClient.Builder<?>) constructor.newInstance(clientBuilder);

    LocalTestWebServer localTestWebServer = LocalTestWebServer.get(applicationContext);
    return builder.uriBuilderFactory(localTestWebServer.uriBuilderFactory()).build();
}

OrangeDog avatar Nov 23 '26 11:11 OrangeDog