Browse Source

Merge pull request #6919 from eddumelendez/gh-6904

* pr/6919:
  Add contextPath LocalHostUriTemplateHandler URIs
pull/6909/merge
Phillip Webb 10 years ago
parent
commit
6fc87764d5
  1. 2
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java
  2. 2
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomServletContextPathTests.java
  3. 13
      spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java
  4. 10
      spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java

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

@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomFilterContextPathTests { @@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomFilterContextPathTests {
@Test
public void contextLoads() {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/app/rest/hello",
ResponseEntity<String> entity = this.restTemplate.getForEntity("/rest/hello",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}

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

@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomServletContextPathTests { @@ -64,7 +64,7 @@ public class JerseyAutoConfigurationCustomServletContextPathTests {
@Test
public void contextLoads() {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/app/rest/hello",
ResponseEntity<String> entity = this.restTemplate.getForEntity("/rest/hello",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}

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

@ -16,6 +16,7 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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;
}
}

10
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; @@ -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 { @@ -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");
}
}

Loading…
Cancel
Save