Skip to content

Commit d1e1796

Browse files
committed
Consider overridden getRootUri() when getting root URI
Fixes gh-26812
1 parent 1f6983c commit d1e1796

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,16 +25,21 @@
2525
import io.micrometer.core.instrument.Tag;
2626
import io.micrometer.core.instrument.simple.SimpleConfig;
2727
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
28+
import org.assertj.core.api.InstanceOfAssertFactories;
2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
3031

3132
import org.springframework.boot.actuate.metrics.AutoTimer;
33+
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
34+
import org.springframework.boot.web.client.RestTemplateBuilder;
35+
import org.springframework.boot.web.client.RootUriTemplateHandler;
3236
import org.springframework.http.HttpMethod;
3337
import org.springframework.http.HttpRequest;
3438
import org.springframework.http.MediaType;
3539
import org.springframework.http.client.ClientHttpRequestExecution;
3640
import org.springframework.http.client.ClientHttpRequestInterceptor;
3741
import org.springframework.http.client.ClientHttpResponse;
42+
import org.springframework.mock.env.MockEnvironment;
3843
import org.springframework.test.web.client.MockRestServiceServer;
3944
import org.springframework.test.web.client.match.MockRestRequestMatchers;
4045
import org.springframework.test.web.client.response.MockRestResponseCreators;
@@ -136,6 +141,18 @@ void interceptNestedRequest() {
136141
nestedMockServer.verify();
137142
}
138143

144+
@Test
145+
void whenCustomizerAndLocalHostUriTemplateHandlerAreUsedTogetherThenRestTemplateBuilderCanBuild() {
146+
MockEnvironment environment = new MockEnvironment();
147+
environment.setProperty("local.server.port", "8443");
148+
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(environment, "https");
149+
RestTemplate restTemplate = new RestTemplateBuilder(this.customizer).uriTemplateHandler(uriTemplateHandler)
150+
.build();
151+
assertThat(restTemplate.getUriTemplateHandler())
152+
.asInstanceOf(InstanceOfAssertFactories.type(RootUriTemplateHandler.class))
153+
.extracting(RootUriTemplateHandler::getRootUri).isEqualTo("https://localhost:8443");
154+
}
155+
139156
private static final class TestInterceptor implements ClientHttpRequestInterceptor {
140157

141158
private final RestTemplate restTemplate;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public String getRootUri() {
9393
* @since 2.3.10
9494
*/
9595
public RootUriTemplateHandler withHandlerWrapper(Function<UriTemplateHandler, UriTemplateHandler> wrapper) {
96-
return new RootUriTemplateHandler(this.rootUri, wrapper.apply(this.handler));
96+
return new RootUriTemplateHandler(getRootUri(), wrapper.apply(this.handler));
9797
}
9898

9999
/**

0 commit comments

Comments
 (0)