From 30859b75a6111dd06dcac88945c31fd0ad75a826 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] Append contextPath to root URI in LocalHostUriTemplateHandler Previous to this commit, `server.context-path` property was not considerate when TestRestTemplate perform an action. Now, context-path is concatenated to the uri. See gh-6904 --- ...yAutoConfigurationCustomFilterContextPathTests.java | 2 +- ...AutoConfigurationCustomServletContextPathTests.java | 2 +- .../test/web/client/LocalHostUriTemplateHandler.java | 9 ++++++++- .../web/client/LocalHostUriTemplateHandlerTests.java | 10 ++++++++++ 4 files changed, 20 insertions(+), 3 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 ac8f5bd1e70c..77837a5dcc9e 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 22fe14a2571a..0bc1a88e89ba 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 56db672458a5..fdb31a05958b 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 @@ * * @author Phillip Webb * @author Andy Wilkinson + * @author Eddú Meléndez * @since 1.4.0 */ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { @@ -36,6 +38,8 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { private final String scheme; + private RelaxedPropertyResolver contextPathResolver; + /** * Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http} * URIs using the given {@code environment} to determine the port. @@ -58,12 +62,15 @@ public LocalHostUriTemplateHandler(Environment environment, String scheme) { Assert.notNull(scheme, "Scheme must not be null"); this.environment = environment; this.scheme = scheme; + this.contextPathResolver = 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.contextPathResolver.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 60425672fa1f..511f90cd3412 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 @@ * * @author Phillip Webb * @author Andy Wilkinson + * @author Eddú Meléndez */ public class LocalHostUriTemplateHandlerTests { @@ -74,4 +75,13 @@ public void getRootUriUsesCustomScheme() { 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"); + } + }