This commit is contained in:
Rossen Stoyanchev 2014-07-11 09:17:02 -04:00
parent 2c77de10dd
commit 2ba8fd7e59
2 changed files with 4 additions and 18 deletions

View File

@ -128,7 +128,7 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder(); ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
builder.scheme(scheme); builder.scheme(scheme);
builder.host(host); builder.host(host);
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) { if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
builder.port(port); builder.port(port);
} }
builder.pathFromRequest(request); builder.pathFromRequest(request);

View File

@ -50,7 +50,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result);
} }
@ -58,7 +57,6 @@ public class ServletUriComponentsBuilderTests {
public void fromRequestEncodedPath() { public void fromRequestEncodedPath() {
request.setRequestURI("/mvc-showcase/data/foo%20bar"); request.setRequestURI("/mvc-showcase/data/foo%20bar");
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result); assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result);
} }
@ -66,7 +64,6 @@ public class ServletUriComponentsBuilderTests {
public void fromRequestAtypicalHttpPort() { public void fromRequestAtypicalHttpPort() {
request.setServerPort(8080); request.setServerPort(8080);
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://localhost:8080", result); assertEquals("http://localhost:8080", result);
} }
@ -75,7 +72,6 @@ public class ServletUriComponentsBuilderTests {
request.setScheme("https"); request.setScheme("https");
request.setServerPort(9043); request.setServerPort(9043);
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("https://localhost:9043", result); assertEquals("https://localhost:9043", result);
} }
@ -84,7 +80,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromRequestUri(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequestUri(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/param", result); assertEquals("http://localhost/mvc-showcase/data/param", result);
} }
@ -94,7 +89,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
assertEquals("http://anotherHost/mvc-showcase/data/param?foo=123", result); assertEquals("http://anotherHost/mvc-showcase/data/param?foo=123", result);
} }
@ -116,7 +110,6 @@ public class ServletUriComponentsBuilderTests {
@Test @Test
public void fromRequestWithForwardedHostMultiValuedHeader() { public void fromRequestWithForwardedHostMultiValuedHeader() {
this.request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org"); this.request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org");
assertEquals("a.example.org", ServletUriComponentsBuilder.fromRequest(this.request).build().getHost()); assertEquals("a.example.org", ServletUriComponentsBuilder.fromRequest(this.request).build().getHost());
} }
@ -142,8 +135,7 @@ public class ServletUriComponentsBuilderTests {
UriComponents result = ServletUriComponentsBuilder.fromRequest(request).build(); UriComponents result = ServletUriComponentsBuilder.fromRequest(request).build();
assertEquals("example.org", result.getHost()); assertEquals("example.org", result.getHost());
assertEquals("should have used the default port of the forwarded request", assertEquals("should have used the default port of the forwarded request", -1, result.getPort());
-1, result.getPort());
} }
@Test @Test
@ -154,10 +146,8 @@ public class ServletUriComponentsBuilderTests {
UriComponents result = ServletUriComponentsBuilder.fromRequest(request).build(); UriComponents result = ServletUriComponentsBuilder.fromRequest(request).build();
assertEquals("example.org", result.getHost()); assertEquals("example.org", result.getHost());
assertEquals("should have derived scheme from header", assertEquals("should have derived scheme from header", "https", result.getScheme());
"https", result.getScheme()); assertEquals("should have used the default port of the forwarded request", -1, result.getPort());
assertEquals("should have used the default port of the forwarded request",
-1, result.getPort());
} }
@Test @Test
@ -165,7 +155,6 @@ public class ServletUriComponentsBuilderTests {
request.setRequestURI("/mvc-showcase/data/param"); request.setRequestURI("/mvc-showcase/data/param");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromContextPath(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromContextPath(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase", result); assertEquals("http://localhost/mvc-showcase", result);
} }
@ -175,7 +164,6 @@ public class ServletUriComponentsBuilderTests {
request.setServletPath("/app"); request.setServletPath("/app");
request.setQueryString("foo=123"); request.setQueryString("foo=123");
String result = ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString(); String result = ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString();
assertEquals("http://localhost/mvc-showcase/app", result); assertEquals("http://localhost/mvc-showcase/app", result);
} }
@ -186,7 +174,6 @@ public class ServletUriComponentsBuilderTests {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request));
try { try {
String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString(); String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString();
assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result);
} }
finally { finally {
@ -202,7 +189,6 @@ public class ServletUriComponentsBuilderTests {
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);
String extension = builder.removePathExtension(); String extension = builder.removePathExtension();
String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString(); String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString();
assertEquals("http://localhost/rest/books/6/pages/1.json", result); assertEquals("http://localhost/rest/books/6/pages/1.json", result);
} }