mirror of
https://github.com/square/okhttp.git
synced 2026-01-17 08:42:25 +03:00
Add newBuilder overload for resolving directly to a builder.
This commit is contained in:
@@ -108,6 +108,22 @@ public final class HttpUrlTest {
|
||||
assertEquals(null, HttpUrl.parse("#fragment"));
|
||||
}
|
||||
|
||||
@Test public void newBuilderResolve() throws Exception {
|
||||
// Non-exhaustive tests because implementation is the same as resolve.
|
||||
HttpUrl base = HttpUrl.parse("http://host/a/b");
|
||||
assertEquals(HttpUrl.parse("https://host2/"), base.newBuilder("https://host2").build());
|
||||
assertEquals(HttpUrl.parse("http://host2/"), base.newBuilder("//host2").build());
|
||||
assertEquals(HttpUrl.parse("http://host/path"), base.newBuilder("/path").build());
|
||||
assertEquals(HttpUrl.parse("http://host/a/path"), base.newBuilder("path").build());
|
||||
assertEquals(HttpUrl.parse("http://host/a/b?query"), base.newBuilder("?query").build());
|
||||
assertEquals(HttpUrl.parse("http://host/a/b#fragment"), base.newBuilder("#fragment").build());
|
||||
assertEquals(HttpUrl.parse("http://host/a/b"), base.newBuilder("").build());
|
||||
assertEquals(null, base.newBuilder("ftp://b"));
|
||||
assertEquals(null, base.newBuilder("ht+tp://b"));
|
||||
assertEquals(null, base.newBuilder("ht-tp://b"));
|
||||
assertEquals(null, base.newBuilder("ht.tp://b"));
|
||||
}
|
||||
|
||||
@Test public void resolveNoScheme() throws Exception {
|
||||
HttpUrl base = HttpUrl.parse("http://host/a/b");
|
||||
assertEquals(HttpUrl.parse("http://host2/"), base.resolve("//host2"));
|
||||
|
||||
@@ -597,11 +597,13 @@ public final class HttpUrl {
|
||||
return fragment;
|
||||
}
|
||||
|
||||
/** Returns the URL that would be retrieved by following {@code link} from this URL. */
|
||||
/**
|
||||
* Returns the URL that would be retrieved by following {@code link} from this URL, or null if
|
||||
* the resulting URL is not well-formed.
|
||||
*/
|
||||
public HttpUrl resolve(String link) {
|
||||
Builder builder = new Builder();
|
||||
Builder.ParseResult result = builder.parse(this, link);
|
||||
return result == Builder.ParseResult.SUCCESS ? builder.build() : null;
|
||||
Builder builder = newBuilder(link);
|
||||
return builder != null ? builder.build() : null;
|
||||
}
|
||||
|
||||
public Builder newBuilder() {
|
||||
@@ -619,6 +621,16 @@ public final class HttpUrl {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a builder for the URL that would be retrieved by following {@code link} from this URL,
|
||||
* or null if the resulting URL is not well-formed.
|
||||
*/
|
||||
public Builder newBuilder(String link) {
|
||||
Builder builder = new Builder();
|
||||
Builder.ParseResult result = builder.parse(this, link);
|
||||
return result == Builder.ParseResult.SUCCESS ? builder : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@code HttpUrl} representing {@code url} if it is a well-formed HTTP or HTTPS
|
||||
* URL, or null if it isn't.
|
||||
|
||||
Reference in New Issue
Block a user