From d1e1796f768ea0e2c2ad761efeee785f1194192d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 8 Jun 2021 16:42:35 +0100 Subject: [PATCH] Consider overridden getRootUri() when getting root URI Fixes gh-26812 --- .../MetricsRestTemplateCustomizerTests.java | 19 ++++++++++++++++++- .../web/client/RootUriTemplateHandler.java | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java index f1cc5d5496..693dfa2a4f 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -25,16 +25,21 @@ import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.metrics.AutoTimer; +import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.boot.web.client.RootUriTemplateHandler; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; +import org.springframework.mock.env.MockEnvironment; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.match.MockRestRequestMatchers; import org.springframework.test.web.client.response.MockRestResponseCreators; @@ -136,6 +141,18 @@ class MetricsRestTemplateCustomizerTests { nestedMockServer.verify(); } + @Test + void whenCustomizerAndLocalHostUriTemplateHandlerAreUsedTogetherThenRestTemplateBuilderCanBuild() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("local.server.port", "8443"); + LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(environment, "https"); + RestTemplate restTemplate = new RestTemplateBuilder(this.customizer).uriTemplateHandler(uriTemplateHandler) + .build(); + assertThat(restTemplate.getUriTemplateHandler()) + .asInstanceOf(InstanceOfAssertFactories.type(RootUriTemplateHandler.class)) + .extracting(RootUriTemplateHandler::getRootUri).isEqualTo("https://localhost:8443"); + } + private static final class TestInterceptor implements ClientHttpRequestInterceptor { private final RestTemplate restTemplate; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java index c6091c36ab..dbbcefd1cd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java @@ -93,7 +93,7 @@ public class RootUriTemplateHandler implements UriTemplateHandler { * @since 2.3.10 */ public RootUriTemplateHandler withHandlerWrapper(Function wrapper) { - return new RootUriTemplateHandler(this.rootUri, wrapper.apply(this.handler)); + return new RootUriTemplateHandler(getRootUri(), wrapper.apply(this.handler)); } /**