/** * Create a builder from a URI string. This will be parsed using the java standard library. * * @param value a URI string * @return a new builder */ public static Builder url(String value) { URI uri = URI.create(value); return new Builder() .scheme(uri.getScheme()) .authority(uri.getAuthority()) .path(uri.getRawPath()) .rawQuery(uri.getRawQuery()) .fragment(uri.getFragment()); }
/** * Create a builder from a URI string. This will be parsed using the java standard library. * * @param value a URI string * @return a new builder */ public static Builder url(String value) { URI uri = URI.create(value); return new Builder() .scheme(uri.getScheme()) .authority(uri.getAuthority()) .path(uri.getRawPath()) .rawQuery(uri.getRawQuery()) .fragment(uri.getFragment()); }
@Test public void setsSchemeAndAuthorityCorrectly() throws Exception { Url url = new Url.Builder() .scheme("http") .authority("example.com") .build(); assertThat(url.scheme(), is("http")); assertThat(url.authority(), is(authority("example.com"))); }
@Test public void shouldCreateWithHostAndPort() throws CharacterCodingException { assertThat("http://example.com:8080", is(new Url.Builder() .scheme("http") .authority(authority("example.com", 8080)) .build() .toString())); }
@Test public void shouldCreateFullyQualifiedUrl() throws Exception { Url fqUrl = url("/landing/de3445") .authority("example.com") .build(); assertThat(fqUrl, is(url("//example.com/landing/de3445").build())); }
@Test public void shouldCreateWithPath() throws CharacterCodingException { Url url = new Url.Builder() .scheme("http") .authority("example.com") .path("/landing/de3445") .build(); assertThat(url.toString(), is("http://example.com/landing/de3445")); }
@Test public void shouldCreateWithUserAndHostAndPort() throws CharacterCodingException { assertThat("http://someone@example.com:8080", is(new Url.Builder() .scheme("http") .authority(authority("someone", "example.com", 8080)) .build() .toString())); }
@Test public void canDropThePath() throws Exception { Url fqUrl = url("/landing/de3445") .authority("example.com") .build(); Url newUrl = fqUrl.newBuilder() .dropHost() .build(); assertThat(newUrl.authority(), isAbsent()); assertThat(newUrl.toString(), is("/landing/de3445")); }
@Test public void shouldCreateWithSchemeAndHost() throws CharacterCodingException { assertThat("http://example.com", is(new Url.Builder() .scheme("http") .authority("example.com") .path("") .build() .toString())); }