@Test
  public void pathSegments() throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
    UriComponents result = builder.pathSegment("foo").pathSegment("bar").build();

    assertEquals("/foo/bar", result.getPath());
    assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments());
  }
 @Test
 public void queryParamWithoutValueWithEquals() throws Exception {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build();
   assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar="));
   assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(""));
 }
 @Test
 public void toUriEncoded() throws URISyntaxException {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com/hotel list/Z\u00fcrich").build();
   assertEquals(
       new URI("http://example.com/hotel%20list/Z%C3%BCrich"), uriComponents.encode().toUri());
 }
  @Test
  public void pathSegmentsThenPath() {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").path("/");
    UriComponents result = builder.build();

    assertEquals("/foo/", result.getPath());
    assertEquals(Collections.singletonList("foo"), result.getPathSegments());
  }
  @Test
  public void pathThenPathSegments() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z");
    UriComponents result = builder.build().encode();

    assertEquals("/foo/bar/ba%2Fz", result.getPath());
    assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments());
  }
  @Test // SPR-9832
  public void fromUriStringQueryParamWithReservedCharInValue() throws URISyntaxException {
    String uri = "http://www.google.com/ig/calculator?q=1USD=?EUR";
    UriComponents result = UriComponentsBuilder.fromUriString(uri).build();

    assertEquals("q=1USD=?EUR", result.getQuery());
    assertEquals("1USD=?EUR", result.getQueryParams().getFirst("q"));
  }
 @Test
 public void expand() {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com").path("/{foo} {bar}").build();
   uriComponents = uriComponents.expand("1 2", "3 4");
   assertEquals("/1 2 3 4", uriComponents.getPath());
   assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString());
 }
 @Test
 public void copyToUriComponentsBuilder() {
   UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build();
   UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance();
   source.copyToUriComponentsBuilder(targetBuilder);
   UriComponents result = targetBuilder.build().encode();
   assertEquals("/foo/bar/ba%2Fz", result.getPath());
   assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments());
 }
  @Test
  public void pathSegmentsSomeEmpty() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.newInstance().pathSegment("", "foo", "", "bar");
    UriComponents result = builder.build();

    assertEquals("/foo/bar", result.getPath());
    assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments());
  }
 @Test // SPR-11970
 public void fromUriStringNoPathWithReservedCharInQuery() {
   UriComponents result =
       UriComponentsBuilder.fromUriString("http://example.com?foo=bar@baz").build();
   assertTrue(StringUtils.isEmpty(result.getUserInfo()));
   assertEquals("example.com", result.getHost());
   assertTrue(result.getQueryParams().containsKey("foo"));
   assertEquals("bar@baz", result.getQueryParams().getFirst("foo"));
 }
  @Test
  public void fromOpaqueUri() throws URISyntaxException {
    URI uri = new URI("mailto:[email protected]#baz");
    UriComponents result = UriComponentsBuilder.fromUri(uri).build();
    assertEquals("mailto", result.getScheme());
    assertEquals("*****@*****.**", result.getSchemeSpecificPart());
    assertEquals("baz", result.getFragment());

    assertEquals("Invalid result URI", uri, result.toUri());
  }
  @Test
  public void emptyQueryParam() throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
    UriComponents result = builder.queryParam("baz").build();

    assertEquals("baz", result.getQuery());
    MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(2);
    expectedQueryParams.add("baz", null);
    assertEquals(expectedQueryParams, result.getQueryParams());
  }
  @Test
  public void buildAndExpandHierarchical() {
    UriComponents result = UriComponentsBuilder.fromPath("/{foo}").buildAndExpand("fooValue");
    assertEquals("/fooValue", result.toUriString());

    Map<String, String> values = new HashMap<>();
    values.put("foo", "fooValue");
    values.put("bar", "barValue");
    result = UriComponentsBuilder.fromPath("/{foo}/{bar}").buildAndExpand(values);
    assertEquals("/fooValue/barValue", result.toUriString());
  }
 @Test
 public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString(
               "http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich")
           .build(true);
   UriComponents encoded = uriComponents.encode();
   assertEquals(
       new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"),
       encoded.toUri());
 }
  @Test
  public void buildAndExpandOpaque() {
    UriComponents result =
        UriComponentsBuilder.fromUriString("mailto:{user}@{domain}")
            .buildAndExpand("foo", "example.com");
    assertEquals("mailto:[email protected]", result.toUriString());

    Map<String, String> values = new HashMap<>();
    values.put("user", "foo");
    values.put("domain", "example.com");
    UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values);
    assertEquals("mailto:[email protected]", result.toUriString());
  }
  @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-14761
  public void fromHttpRequestWithForwardedIPv6HostAndPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "[1abc:2abc:3abc::5ABC:6abc]:8080");

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

    assertEquals("http://[1abc:2abc:3abc::5ABC:6abc]:8080/mvc-showcase", result.toString());
  }
  @Test // SPR-14761
  public void fromHttpRequestWithForwardedIPv4Host() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("Forwarded", "host=192.168.0.1");

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

    assertEquals("http://192.168.0.1/mvc-showcase", result.toString());
  }
  @Test
  public void fromHttpRequestWithForwardedHost() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "anotherHost");

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

    assertEquals("http://anotherHost/mvc-showcase", result.toString());
  }
 @Test
 public void serializable() throws Exception {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com")
           .path("/{foo}")
           .query("bar={baz}")
           .build();
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ObjectOutputStream oos = new ObjectOutputStream(bos);
   oos.writeObject(uriComponents);
   ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
   UriComponents readObject = (UriComponents) ois.readObject();
   assertThat(uriComponents.toString(), equalTo(readObject.toString()));
 }
  @Test
  public void replaceQueryParam() {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42);
    builder.replaceQueryParam("baz", "xuq", 24);
    UriComponents result = builder.build();

    assertEquals("baz=xuq&baz=24", result.getQuery());

    builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42);
    builder.replaceQueryParam("baz");
    result = builder.build();

    assertNull("Query param should have been deleted", result.getQuery());
  }
  @Test
  public void fromHttpRequestMultipleForwardedHeaderComma() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "host=84.198.58.199 ;proto=https, proto=ftp; host=1.2.3.4");
    request.setScheme("http");
    request.setServerName("example.com");
    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("/rest/mobile/users/1", result.getPath());
  }
  @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 // 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
  public void replaceQuery() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux");
    builder.replaceQuery("baz=42");
    UriComponents result = builder.build();

    assertEquals("http://example.com/foo?baz=42", result.toUriString());

    builder = UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux");
    builder.replaceQuery(null);
    result = builder.build();

    assertEquals("http://example.com/foo", result.toUriString());
  }
  @Test
  public void replacePath() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt");
    builder.replacePath("/rfc/rfc3986.txt");
    UriComponents result = builder.build();

    assertEquals("http://www.ietf.org/rfc/rfc3986.txt", result.toUriString());

    builder = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt");
    builder.replacePath(null);
    result = builder.build();

    assertEquals("http://www.ietf.org", result.toUriString());
  }
  @Test // SPR-12813
  public void fromHttpRequestWithForwardedPortMultiValueHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(9090);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "a.example.org");
    request.addHeader("X-Forwarded-Port", "80,52022");

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

    assertEquals("http://a.example.org/mvc-showcase", result.toString());
  }
  @Test // SPR-12771
  public void fromHttpRequestWithForwardedProtoAndDefaultPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Proto", "https");
    request.addHeader("X-Forwarded-Host", "84.198.58.199");
    request.addHeader("X-Forwarded-Port", "443");

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

    assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
  }
  @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());
  }
  @Test
  public void fromHierarchicalUri() throws URISyntaxException {
    URI uri = new URI("http://example.com/foo?bar#baz");
    UriComponents result = UriComponentsBuilder.fromUri(uri).build();
    assertEquals("http", result.getScheme());
    assertEquals("example.com", result.getHost());
    assertEquals("/foo", result.getPath());
    assertEquals("bar", result.getQuery());
    assertEquals("baz", result.getFragment());

    assertEquals("Invalid result URI", uri, result.toUri());
  }