@Test public void emptySegments() throws Exception { assertThat( UriComponentsBuilder.fromUriString("http://example.com/abc/") .path("/x/y/z") .build() .toString(), equalTo("http://example.com/abc/x/y/z")); assertThat( UriComponentsBuilder.fromUriString("http://example.com/abc/") .pathSegment("x", "y", "z") .build() .toString(), equalTo("http://example.com/abc/x/y/z")); assertThat( UriComponentsBuilder.fromUriString("http://example.com/abc/") .path("/x/") .path("/y/z") .build() .toString(), equalTo("http://example.com/abc/x/y/z")); assertThat( UriComponentsBuilder.fromUriString("http://example.com/abc/") .pathSegment("x") .path("y") .build() .toString(), equalTo("http://example.com/abc/x/y")); }
@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 public void equalsOpaqueUriComponents() throws Exception { UriComponents uriComponents1 = UriComponentsBuilder.fromUriString("http:example.com/foo/bar").build(); UriComponents uriComponents2 = UriComponentsBuilder.fromUriString("http:example.com/foo/bar").build(); UriComponents uriComponents3 = UriComponentsBuilder.fromUriString("http:example.com/foo/bin").build(); assertThat(uriComponents1, instanceOf(OpaqueUriComponents.class)); assertThat(uriComponents1, equalTo(uriComponents1)); assertThat(uriComponents1, equalTo(uriComponents2)); assertThat(uriComponents1, not(equalTo(uriComponents3))); }
@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 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 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-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 // 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()); }
@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")); }
/** * 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; }
@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 equalsHierarchicalUriComponents() throws Exception { UriComponents uriComponents1 = UriComponentsBuilder.fromUriString("http://example.com") .path("/{foo}") .query("bar={baz}") .build(); UriComponents uriComponents2 = UriComponentsBuilder.fromUriString("http://example.com") .path("/{foo}") .query("bar={baz}") .build(); UriComponents uriComponents3 = UriComponentsBuilder.fromUriString("http://example.com") .path("/{foo}") .query("bin={baz}") .build(); assertThat(uriComponents1, instanceOf(HierarchicalUriComponents.class)); assertThat(uriComponents1, equalTo(uriComponents1)); assertThat(uriComponents1, equalTo(uriComponents2)); assertThat(uriComponents1, not(equalTo(uriComponents3))); }
@Test // SPR-9317 public void fromUriEncodedQuery() throws URISyntaxException { URI uri = new URI("http://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D"); String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0); String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()) .build() .getQueryParams() .get("param") .get(0); assertEquals(fromUri, fromUriString); }
@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 relativeUrls() throws Exception { assertThat( UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build().toString(), equalTo("http://example.com/foo/../bar")); assertThat( UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build().toUriString(), equalTo("http://example.com/foo/../bar")); assertThat( UriComponentsBuilder.fromUriString("http://example.com/foo/../bar") .build() .toUri() .getPath(), equalTo("/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString("../../").build().toString(), equalTo("../../")); assertThat( UriComponentsBuilder.fromUriString("../../").build().toUriString(), equalTo("../../")); assertThat( UriComponentsBuilder.fromUriString("../../").build().toUri().getPath(), equalTo("../../")); assertThat( UriComponentsBuilder.fromUriString("http://example.com") .path("foo/../bar") .build() .toString(), equalTo("http://example.com/foo/../bar")); assertThat( UriComponentsBuilder.fromUriString("http://example.com") .path("foo/../bar") .build() .toUriString(), equalTo("http://example.com/foo/../bar")); assertThat( UriComponentsBuilder.fromUriString("http://example.com") .path("foo/../bar") .build() .toUri() .getPath(), equalTo("/foo/../bar")); }
@Test // SPR-13257 public void parsesEmptyUri() { UriComponents components = UriComponentsBuilder.fromUriString("").build(); assertThat(components.toString(), equalTo("")); }
@Test public void parsesEmptyFragment() { UriComponents components = UriComponentsBuilder.fromUriString("/example#").build(); assertThat(components.getFragment(), is(nullValue())); assertThat(components.toString(), equalTo("/example")); }
public static String encodeUri(String uri) { if (!StringUtils.hasLength(uri)) { return uri; } return UriComponentsBuilder.fromUriString(uri).build().encode().toUriString(); }
@Test public void normalize() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build(); assertEquals("http://example.com/bar", uriComponents.normalize().toString()); }