From c1205c3243074d6bb3722f414cd64ed6f01ba95e Mon Sep 17 00:00:00 2001 From: tinexw Date: Sat, 14 Oct 2017 13:29:45 +0200 Subject: [PATCH 1/2] Provide access to root URI from TestRestTemplate Closes gh-10641 --- .../test/web/client/TestRestTemplate.java | 10 +++++++ .../web/client/TestRestTemplateTests.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index 71b0476e75..8b0ac4d801 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -78,6 +78,7 @@ import org.springframework.web.util.UriTemplateHandler; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Kristine Jetzke * @since 1.4.0 */ public class TestRestTemplate { @@ -165,6 +166,15 @@ public class TestRestTemplate { this.restTemplate.setUriTemplateHandler(handler); } + public String getRootUri() { + UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler(); + if (RootUriTemplateHandler.class + .isAssignableFrom(uriTemplateHandler.getClass())) { + return ((RootUriTemplateHandler) uriTemplateHandler).getRootUri(); + } + return ""; + } + /** * Retrieve a representation by doing a GET on the specified URL. The response (if * any) is converted and returned. diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java index 76f0306a82..ec053b0054 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java @@ -63,6 +63,7 @@ import static org.mockito.Mockito.verify; * @author Phillip Webb * @author Stephane Nicoll * @author Andy Wilkinson + * @author Kristine Jetzke */ public class TestRestTemplateTests { @@ -81,6 +82,31 @@ public class TestRestTemplateTests { .isInstanceOf(HttpComponentsClientHttpRequestFactory.class); } + @Test + public void getRootUriRootUriSetViaRestTemplateBuilder() { + String rootUri = "http://example.com"; + RestTemplate delegate = new RestTemplateBuilder().rootUri(rootUri).build(); + + assertThat(new TestRestTemplate(delegate).getRootUri()).isEqualTo(rootUri); + } + + @Test + public void getRootUriRootUriSetViaLocalHostUriTemplateHandler() { + String rootUri = "http://example.com"; + TestRestTemplate template = new TestRestTemplate(); + LocalHostUriTemplateHandler templateHandler = mock( + LocalHostUriTemplateHandler.class); + given(templateHandler.getRootUri()).willReturn(rootUri); + template.setUriTemplateHandler(templateHandler); + + assertThat(template.getRootUri()).isEqualTo(rootUri); + } + + @Test + public void getRootUriRootUriNotSet() { + assertThat(new TestRestTemplate().getRootUri()).isEqualTo(""); + } + @Test public void authenticated() { assertThat(new TestRestTemplate("user", "password").getRestTemplate() From 71632ac767ab0137c1ae3b776cfb33109549f6a8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sun, 12 Nov 2017 12:11:57 +0000 Subject: [PATCH 2/2] Polish "Provide access to root URI from TestRestTemplate" See gh-10641 --- .../boot/test/web/client/TestRestTemplate.java | 8 ++++++-- .../boot/test/web/client/TestRestTemplateTests.java | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index 8b0ac4d801..4e47fffa32 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -166,10 +166,14 @@ public class TestRestTemplate { this.restTemplate.setUriTemplateHandler(handler); } + /** + * Returns the root URI applied by a {@link RootUriTemplateHandler} or {@code ""} if + * the root URI is not available. + * @return the root URI + */ public String getRootUri() { UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler(); - if (RootUriTemplateHandler.class - .isAssignableFrom(uriTemplateHandler.getClass())) { + if (uriTemplateHandler instanceof RootUriTemplateHandler) { return ((RootUriTemplateHandler) uriTemplateHandler).getRootUri(); } return ""; diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java index ec053b0054..3740c0e4e2 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java @@ -86,7 +86,6 @@ public class TestRestTemplateTests { public void getRootUriRootUriSetViaRestTemplateBuilder() { String rootUri = "http://example.com"; RestTemplate delegate = new RestTemplateBuilder().rootUri(rootUri).build(); - assertThat(new TestRestTemplate(delegate).getRootUri()).isEqualTo(rootUri); } @@ -98,7 +97,6 @@ public class TestRestTemplateTests { LocalHostUriTemplateHandler.class); given(templateHandler.getRootUri()).willReturn(rootUri); template.setUriTemplateHandler(templateHandler); - assertThat(template.getRootUri()).isEqualTo(rootUri); }