Allow MvcUriComponentsBuilder instance with baseUrl

Before this change MvcUriComponentsBuilder exposed only static factory
methods for creating links where the links are relative to the current
request or a baseUrl explicitly provided as an argument.

This change allows creating an MvcUriComponents builder instance with
a built-in baseUrl. The instance can then be used with non-static
withXxx(...) method alternatives to the static fromXxx(...) methods.

Issue: SPR-12617
This commit is contained in:
Rossen Stoyanchev
2015-04-23 10:59:38 -04:00
parent 69b0791926
commit 1cd0f433e0
3 changed files with 184 additions and 11 deletions

View File

@@ -30,10 +30,12 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpEntity;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
@@ -41,6 +43,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -100,7 +106,7 @@ public class MvcUriComponentsBuilderTests {
}
@Test
public void testFromControllerWithCustomBaseUrl() {
public void testFromControllerWithCustomBaseUrlViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
UriComponents uriComponents = fromController(builder, PersonControllerImpl.class).build();
@@ -108,6 +114,16 @@ public class MvcUriComponentsBuilderTests {
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromControllerWithCustomBaseUrlViaInstance() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build();
assertEquals("http://example.org:9090/base/people", uriComponents.toString());
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromMethodNamePathVariable() throws Exception {
UriComponents uriComponents = fromMethodName(
@@ -162,7 +178,7 @@ public class MvcUriComponentsBuilderTests {
}
@Test
public void testFromMethodNameWithCustomBaseUrl() throws Exception {
public void testFromMethodNameWithCustomBaseUrlViaStaticCall() throws Exception {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class,
"methodWithPathVariable", new Object[] {"1"}).build();
@@ -171,6 +187,17 @@ public class MvcUriComponentsBuilderTests {
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromMethodNameWithCustomBaseUrlViaInstance() throws Exception {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class,
"methodWithPathVariable", new Object[] {"1"}).build();
assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString());
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromMethodCall() {
UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();
@@ -222,7 +249,7 @@ public class MvcUriComponentsBuilderTests {
}
@Test
public void testFromMethodCallWithCustomBaseUrl() {
public void testFromMethodCallWithCustomBaseUrlViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
UriComponents uriComponents = fromMethodCall(builder, on(ControllerWithMethods.class).myMethod(null)).build();
@@ -230,6 +257,48 @@ public class MvcUriComponentsBuilderTests {
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromMethodCallWithCustomBaseUrlViaInstance() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
UriComponents result = mvcBuilder.withMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();
assertEquals("http://example.org:9090/base/something/else", result.toString());
assertEquals("http://example.org:9090/base", builder.toUriString());
}
@Test
public void testFromMappingName() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(WebConfig.class);
context.refresh();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
this.request.setServerName("example.org");
this.request.setServerPort(9999);
this.request.setContextPath("/base");
String mappingName = "PAC#getAddressesForCountry";
String url = MvcUriComponentsBuilder.fromMappingName(mappingName).arg(0, "DE").buildAndExpand(123);
assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
@Test
public void testFromMappingNameWithCustomBaseUrl() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(WebConfig.class);
context.refresh();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(baseUrl);
String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
@Test
public void usesForwardedHostAsHostIfHeaderIsSet() {
this.request.addHeader("X-Forwarded-Host", "somethingDifferent");
@@ -329,6 +398,7 @@ public class MvcUriComponentsBuilderTests {
}
}
@SuppressWarnings("unused")
@RequestMapping("/user/{userId}/contacts")
static class UserContactController {
@@ -338,4 +408,14 @@ public class MvcUriComponentsBuilderTests {
}
}
@EnableWebMvc
static class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public PersonsAddressesController controller() {
return new PersonsAddressesController();
}
}
}