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"); + } + }