UriComponentsBuilder.fromHttpRequest sets port correctly
Cherry-picked from commit d64c48 on master Issue: SPR-12771
This commit is contained in:
@@ -307,6 +307,7 @@ public class UriComponentsBuilder implements Cloneable {
|
|||||||
|
|
||||||
builder.scheme(scheme);
|
builder.scheme(scheme);
|
||||||
builder.host(host);
|
builder.host(host);
|
||||||
|
builder.port(null);
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -256,6 +256,26 @@ public class UriComponentsBuilderTests {
|
|||||||
assertEquals("a=1", result.getQuery());
|
assertEquals("a=1", result.getQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-12771
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromHttpRequestResetsPortBeforeSettingIt() throws Exception {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
request.addHeader("X-Forwarded-Proto", "https");
|
||||||
|
request.addHeader("X-Forwarded-Host", "84.198.58.199");
|
||||||
|
request.addHeader("X-Forwarded-Port", 443);
|
||||||
|
request.setScheme("http");
|
||||||
|
request.setServerName("example.com");
|
||||||
|
request.setServerPort(80);
|
||||||
|
request.setRequestURI("/rest/mobile/users/1");
|
||||||
|
|
||||||
|
UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
|
||||||
|
assertEquals("https", result.getScheme());
|
||||||
|
assertEquals("84.198.58.199", result.getHost());
|
||||||
|
assertEquals(-1, result.getPort());
|
||||||
|
assertEquals("/rest/mobile/users/1", result.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void path() throws URISyntaxException {
|
public void path() throws URISyntaxException {
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");
|
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -36,15 +36,18 @@ public class ServletUriComponentsBuilderTests {
|
|||||||
|
|
||||||
private MockHttpServletRequest request;
|
private MockHttpServletRequest request;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.request = new MockHttpServletRequest();
|
this.request = new MockHttpServletRequest();
|
||||||
this.request.setScheme("http");
|
this.request.setScheme("http");
|
||||||
this.request.setServerName("localhost");
|
this.request.setServerName("localhost");
|
||||||
this.request.setServerPort(-1);
|
this.request.setServerPort(-1);
|
||||||
|
this.request.setRequestURI("/mvc-showcase");
|
||||||
this.request.setContextPath("/mvc-showcase");
|
this.request.setContextPath("/mvc-showcase");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fromRequest() {
|
public void fromRequest() {
|
||||||
this.request.setRequestURI("/mvc-showcase/data/param");
|
this.request.setRequestURI("/mvc-showcase/data/param");
|
||||||
@@ -64,7 +67,7 @@ public class ServletUriComponentsBuilderTests {
|
|||||||
public void fromRequestAtypicalHttpPort() {
|
public void fromRequestAtypicalHttpPort() {
|
||||||
this.request.setServerPort(8080);
|
this.request.setServerPort(8080);
|
||||||
String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString();
|
String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString();
|
||||||
assertEquals("http://localhost:8080", result);
|
assertEquals("http://localhost:8080/mvc-showcase", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -72,7 +75,7 @@ public class ServletUriComponentsBuilderTests {
|
|||||||
this.request.setScheme("https");
|
this.request.setScheme("https");
|
||||||
this.request.setServerPort(9043);
|
this.request.setServerPort(9043);
|
||||||
String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString();
|
String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString();
|
||||||
assertEquals("https://localhost:9043", result);
|
assertEquals("https://localhost:9043/mvc-showcase", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -150,6 +153,19 @@ public class ServletUriComponentsBuilderTests {
|
|||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-12771
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromRequestWithForwardedProtoAndDefaultPort() {
|
||||||
|
this.request.addHeader("X-Forwarded-Proto", "https");
|
||||||
|
this.request.addHeader("X-Forwarded-Host", "84.198.58.199");
|
||||||
|
this.request.addHeader("X-Forwarded-Port", "443");
|
||||||
|
this.request.setServerPort(80);
|
||||||
|
UriComponents result = ServletUriComponentsBuilder.fromRequest(this.request).build();
|
||||||
|
|
||||||
|
assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fromRequestWithForwardedPrefix() {
|
public void fromRequestWithForwardedPrefix() {
|
||||||
this.request.setRequestURI("/bar");
|
this.request.setRequestURI("/bar");
|
||||||
|
|||||||
Reference in New Issue
Block a user