Skip to content

Commit 6fc8776

Browse files
committed
Merge pull request #6919 from eddumelendez/gh-6904
* pr/6919: Add contextPath LocalHostUriTemplateHandler URIs
2 parents 7299976 + bc55b6e commit 6fc8776

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomFilterContextPathTests {
6464

6565
@Test
6666
public void contextLoads() {
67-
ResponseEntity<String> entity = this.restTemplate.getForEntity("/app/rest/hello",
67+
ResponseEntity<String> entity = this.restTemplate.getForEntity("/rest/hello",
6868
String.class);
6969
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7070
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomServletContextPathTests {
6464

6565
@Test
6666
public void contextLoads() {
67-
ResponseEntity<String> entity = this.restTemplate.getForEntity("/app/rest/hello",
67+
ResponseEntity<String> entity = this.restTemplate.getForEntity("/rest/hello",
6868
String.class);
6969
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7070
}

spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.test.web.client;
1818

19+
import org.springframework.boot.bind.RelaxedPropertyResolver;
1920
import org.springframework.boot.web.client.RootUriTemplateHandler;
2021
import org.springframework.core.env.Environment;
2122
import org.springframework.util.Assert;
@@ -28,6 +29,7 @@
2829
*
2930
* @author Phillip Webb
3031
* @author Andy Wilkinson
32+
* @author Eddú Meléndez
3133
* @since 1.4.0
3234
*/
3335
public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
@@ -36,9 +38,11 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
3638

3739
private final String scheme;
3840

41+
private RelaxedPropertyResolver serverPropertyResolver;
42+
3943
/**
4044
* Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http}
41-
* URIs using the given {@code environment} to determine the port.
45+
* URIs using the given {@code environment} to determine the context path and port.
4246
* @param environment the environment used to determine the port
4347
*/
4448
public LocalHostUriTemplateHandler(Environment environment) {
@@ -47,7 +51,8 @@ public LocalHostUriTemplateHandler(Environment environment) {
4751

4852
/**
4953
* Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the
50-
* given {@code scheme} and use the given {@code environment} to determine the port.
54+
* given {@code scheme} and use the given {@code environment} to determine the
55+
* context-path and port.
5156
* @param environment the environment used to determine the port
5257
* @param scheme the scheme of the root uri
5358
* @since 1.4.1
@@ -58,12 +63,14 @@ public LocalHostUriTemplateHandler(Environment environment, String scheme) {
5863
Assert.notNull(scheme, "Scheme must not be null");
5964
this.environment = environment;
6065
this.scheme = scheme;
66+
this.serverPropertyResolver = new RelaxedPropertyResolver(environment, "server.");
6167
}
6268

6369
@Override
6470
public String getRootUri() {
6571
String port = this.environment.getProperty("local.server.port", "8080");
66-
return this.scheme + "://localhost:" + port;
72+
String contextPath = this.serverPropertyResolver.getProperty("context-path", "");
73+
return this.scheme + "://localhost:" + port + contextPath;
6774
}
6875

6976
}

spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @author Phillip Webb
3131
* @author Andy Wilkinson
32+
* @author Eddú Meléndez
3233
*/
3334
public class LocalHostUriTemplateHandlerTests {
3435

@@ -74,4 +75,13 @@ public void getRootUriUsesCustomScheme() {
7475
assertThat(handler.getRootUri()).isEqualTo("https://localhost:8080");
7576
}
7677

78+
@Test
79+
public void getRootUriShouldUseContextPath() throws Exception {
80+
MockEnvironment environment = new MockEnvironment();
81+
environment.setProperty("server.contextPath", "/foo");
82+
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
83+
environment);
84+
assertThat(handler.getRootUri()).isEqualTo("http://localhost:8080/foo");
85+
}
86+
7787
}

0 commit comments

Comments
 (0)