コード例 #1
1
  @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());
  }
コード例 #2
0
 @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());
 }
コード例 #3
0
 @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(""));
 }
コード例 #4
0
  @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"));
  }
コード例 #5
0
 @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());
 }
コード例 #6
0
  @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());
  }
コード例 #7
0
  @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());
  }
コード例 #8
0
  @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());
  }
コード例 #9
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"));
 }
コード例 #10
0
 @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());
 }
コード例 #11
0
  @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());
  }
コード例 #12
0
 /**
  * Configure a base URL to prepend URI templates with. The base URL must have a scheme and host
  * but may optionally contain a port and a path. The base URL must be fully expanded and encoded
  * which can be done via {@link UriComponentsBuilder}.
  *
  * @param baseUrl the base URL.
  */
 public void setBaseUrl(String baseUrl) {
   if (baseUrl != null) {
     UriComponents uriComponents = UriComponentsBuilder.fromUriString(baseUrl).build();
     Assert.hasText(uriComponents.getScheme(), "'baseUrl' must have a scheme");
     Assert.hasText(uriComponents.getHost(), "'baseUrl' must have a host");
     Assert.isNull(uriComponents.getQuery(), "'baseUrl' cannot have a query");
     Assert.isNull(uriComponents.getFragment(), "'baseUrl' cannot have a fragment");
   }
   this.baseUrl = baseUrl;
 }
コード例 #13
0
  @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());
  }
コード例 #14
0
 @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());
 }
コード例 #15
0
  @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());
  }
コード例 #16
0
  @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());
  }
コード例 #17
0
  @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());
  }
コード例 #18
0
  @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());
  }
コード例 #19
0
  @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());
  }
コード例 #20
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());
  }
コード例 #21
0
 @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()));
 }
コード例 #22
0
  @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());
  }
コード例 #23
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());
  }
コード例 #24
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());
  }
コード例 #25
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());
  }
コード例 #26
0
  @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());
  }
コード例 #27
0
  @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());
  }
コード例 #28
0
  @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());
  }
コード例 #29
0
  @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());
  }
コード例 #30
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());
  }