From bc55b6e0d444decf205240ec3d826ab018d52744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sat, 17 Sep 2016 16:53:50 -0500 Subject: [PATCH] Add contextPath LocalHostUriTemplateHandler URIs Update `LocalHostUriTemplateHandler` so that the `server.context-path` property is also considered when building the URL. Fixes gh-6904 Closes gh-6919 --- ...toConfigurationCustomFilterContextPathTests.java | 2 +- ...oConfigurationCustomServletContextPathTests.java | 2 +- .../web/client/LocalHostUriTemplateHandler.java | 13 ++++++++++--- .../client/LocalHostUriTemplateHandlerTests.java | 10 ++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java index ac8f5bd1e70..77837a5dcc9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java @@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomFilterContextPathTests { @Test public void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/app/rest/hello", + ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java index 22fe14a2571..0bc1a88e89b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java @@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomServletContextPathTests { @Test public void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/app/rest/hello", + ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java index 56db672458a..54919a0dd30 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java @@ -16,6 +16,7 @@ package org.springframework.boot.test.web.client; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.web.client.RootUriTemplateHandler; import org.springframework.core.env.Environment; import org.springframework.util.Assert; @@ -28,6 +29,7 @@ import org.springframework.web.util.UriTemplateHandler; * * @author Phillip Webb * @author Andy Wilkinson + * @author Eddú Meléndez * @since 1.4.0 */ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { @@ -36,9 +38,11 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { private final String scheme; + private RelaxedPropertyResolver serverPropertyResolver; + /** * Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http} - * URIs using the given {@code environment} to determine the port. + * URIs using the given {@code environment} to determine the context path and port. * @param environment the environment used to determine the port */ public LocalHostUriTemplateHandler(Environment environment) { @@ -47,7 +51,8 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { /** * Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the - * given {@code scheme} and use the given {@code environment} to determine the port. + * given {@code scheme} and use the given {@code environment} to determine the + * context-path and port. * @param environment the environment used to determine the port * @param scheme the scheme of the root uri * @since 1.4.1 @@ -58,12 +63,14 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { Assert.notNull(scheme, "Scheme must not be null"); this.environment = environment; this.scheme = scheme; + this.serverPropertyResolver = new RelaxedPropertyResolver(environment, "server."); } @Override public String getRootUri() { String port = this.environment.getProperty("local.server.port", "8080"); - return this.scheme + "://localhost:" + port; + String contextPath = this.serverPropertyResolver.getProperty("context-path", ""); + return this.scheme + "://localhost:" + port + contextPath; } } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java index 60425672fa1..511f90cd341 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java @@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Andy Wilkinson + * @author Eddú Meléndez */ public class LocalHostUriTemplateHandlerTests { @@ -74,4 +75,13 @@ public class LocalHostUriTemplateHandlerTests { assertThat(handler.getRootUri()).isEqualTo("https://localhost:8080"); } + @Test + public void getRootUriShouldUseContextPath() throws Exception { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("server.contextPath", "/foo"); + LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( + environment); + assertThat(handler.getRootUri()).isEqualTo("http://localhost:8080/foo"); + } + }