@Test
 public void port() {
   UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();
   UriComponents uri2 = fromUriString("http://example.com/bar").port(8080).build();
   UriComponents uri3 =
       fromUriString("http://example.com/bar").port("{port}").build().expand(8080);
   UriComponents uri4 =
       fromUriString("http://example.com/bar").port("808{digit}").build().expand(0);
   assertEquals(8080, uri1.getPort());
   assertEquals("http://example.com:8080/bar", uri1.toUriString());
   assertEquals(8080, uri2.getPort());
   assertEquals("http://example.com:8080/bar", uri2.toUriString());
   assertEquals(8080, uri3.getPort());
   assertEquals("http://example.com:8080/bar", uri3.toUriString());
   assertEquals(8080, uri4.getPort());
   assertEquals("http://example.com:8080/bar", uri4.toUriString());
 }
  @Test
  public void fromUriString() {
    UriComponents result =
        UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc3986.txt").build();
    assertEquals("http", result.getScheme());
    assertNull(result.getUserInfo());
    assertEquals("www.ietf.org", result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("/rfc/rfc3986.txt", result.getPath());
    assertEquals(Arrays.asList("rfc", "rfc3986.txt"), result.getPathSegments());
    assertNull(result.getQuery());
    assertNull(result.getFragment());

    result =
        UriComponentsBuilder.fromUriString(
                "http://*****:*****@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)")
            .build();
    assertEquals("http", result.getScheme());
    assertEquals("arjen:foobar", result.getUserInfo());
    assertEquals("java.sun.com", result.getHost());
    assertEquals(80, result.getPort());
    assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.getPath());
    assertEquals("foo=bar", result.getQuery());
    MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(1);
    expectedQueryParams.add("foo", "bar");
    assertEquals(expectedQueryParams, result.getQueryParams());
    assertEquals("and(java.util.BitSet)", result.getFragment());

    result = UriComponentsBuilder.fromUriString("mailto:[email protected]#baz").build();
    assertEquals("mailto", result.getScheme());
    assertNull(result.getUserInfo());
    assertNull(result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("*****@*****.**", result.getSchemeSpecificPart());
    assertNull(result.getPath());
    assertNull(result.getQuery());
    assertEquals("baz", result.getFragment());

    result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28").build();
    assertNull(result.getScheme());
    assertNull(result.getUserInfo());
    assertNull(result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("docs/guide/collections/designfaq.html", result.getPath());
    assertNull(result.getQuery());
    assertEquals("28", result.getFragment());
  }
  @Test // SPR-11872
  public void fromHttpRequestWithForwardedHostWithDefaultPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(10080);
    request.addHeader("X-Forwarded-Host", "example.org");

    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

    assertEquals("example.org", result.getHost());
    assertEquals(-1, result.getPort());
  }
  @Test // SPR-11855
  public void fromHttpRequestWithForwardedHostAndPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(8080);
    request.addHeader("X-Forwarded-Host", "foobarhost");
    request.addHeader("X-Forwarded-Port", "9090");

    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

    assertEquals("foobarhost", result.getHost());
    assertEquals(9090, result.getPort());
  }
  @Test // SPR-10701
  public void fromHttpRequestWithForwardedHostIncludingPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "webtest.foo.bar.com:443");

    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

    assertEquals("webtest.foo.bar.com", result.getHost());
    assertEquals(443, result.getPort());
  }
  @Test
  public void fromHttpRequest() throws URISyntaxException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/path");
    request.setQueryString("a=1");

    UriComponents result =
        UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
    assertEquals("http", result.getScheme());
    assertEquals("localhost", result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("/path", result.getPath());
    assertEquals("a=1", result.getQuery());
  }
  @Test // SPR-12771
  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");

    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

    assertEquals("https", result.getScheme());
    assertEquals("84.198.58.199", result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("/rest/mobile/users/1", result.getPath());
  }