コード例 #1
0
  @Test
  public void testClone() throws URISyntaxException {
    UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
    builder1
        .scheme("http")
        .host("e1.com")
        .path("/p1")
        .pathSegment("ps1")
        .queryParam("q1")
        .fragment("f1");

    UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();
    builder2
        .scheme("https")
        .host("e2.com")
        .path("p2")
        .pathSegment("ps2")
        .queryParam("q2")
        .fragment("f2");

    UriComponents result1 = builder1.build();
    assertEquals("http", result1.getScheme());
    assertEquals("e1.com", result1.getHost());
    assertEquals("/p1/ps1", result1.getPath());
    assertEquals("q1", result1.getQuery());
    assertEquals("f1", result1.getFragment());

    UriComponents result2 = builder2.build();
    assertEquals("https", result2.getScheme());
    assertEquals("e2.com", result2.getHost());
    assertEquals("/p1/ps1/p2/ps2", result2.getPath());
    assertEquals("q1&q2", result2.getQuery());
    assertEquals("f2", result2.getFragment());
  }
コード例 #2
0
  @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());
  }
コード例 #3
0
 @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"));
 }
コード例 #4
0
  @Test // SPR-10539
  public void fromUriStringIPv6Host() throws URISyntaxException {
    UriComponents result =
        UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc]:8080/resource")
            .build()
            .encode();
    assertEquals("[1abc:2abc:3abc::5ABC:6abc]", result.getHost());

    UriComponents resultWithScopeId =
        UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc%eth0]:8080/resource")
            .build()
            .encode();
    assertEquals("[1abc:2abc:3abc::5ABC:6abc%25eth0]", resultWithScopeId.getHost());

    UriComponents resultIPv4compatible =
        UriComponentsBuilder.fromUriString("http://[::192.168.1.1]:8080/resource").build().encode();
    assertEquals("[::192.168.1.1]", resultIPv4compatible.getHost());
  }
コード例 #5
0
  @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());
  }
コード例 #6
0
  @Test
  public void multipleFromSameBuilder() throws URISyntaxException {
    UriComponentsBuilder builder =
        UriComponentsBuilder.newInstance().scheme("http").host("example.com").pathSegment("foo");
    UriComponents result1 = builder.build();
    builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz");
    UriComponents result2 = builder.build();

    assertEquals("http", result1.getScheme());
    assertEquals("example.com", result1.getHost());
    assertEquals("/foo", result1.getPath());
    URI expected = new URI("http://example.com/foo");
    assertEquals("Invalid result URI", expected, result1.toUri());

    assertEquals("http", result2.getScheme());
    assertEquals("example.com", result2.getHost());
    assertEquals("/foo/foo2", result2.getPath());
    assertEquals("bar", result2.getQuery());
    assertEquals("baz", result2.getFragment());
    expected = new URI("http://example.com/foo/foo2?bar#baz");
    assertEquals("Invalid result URI", expected, result2.toUri());
  }
コード例 #7
0
  @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());
  }
コード例 #8
0
  @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());
  }
コード例 #9
0
  @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());
  }
コード例 #10
0
  @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());
  }
コード例 #11
0
  @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());
  }
コード例 #12
0
  @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());
  }
コード例 #13
0
  @Test
  public void plain() throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
    UriComponents result =
        builder
            .scheme("http")
            .host("example.com")
            .path("foo")
            .queryParam("bar")
            .fragment("baz")
            .build();
    assertEquals("http", result.getScheme());
    assertEquals("example.com", result.getHost());
    assertEquals("foo", result.getPath());
    assertEquals("bar", result.getQuery());
    assertEquals("baz", result.getFragment());

    URI expected = new URI("http://example.com/foo?bar#baz");
    assertEquals("Invalid result URI", expected, result.toUri());
  }