1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Handle null fragments.

This commit is contained in:
jwilson
2015-10-30 20:32:20 -04:00
parent 403e127cf2
commit e4dd6cfd63
2 changed files with 26 additions and 4 deletions

View File

@@ -1168,4 +1168,24 @@ public final class HttpUrlTest {
assertEquals(urlString, url.newBuilder().build().toString());
assertEquals("http://%6d%6D:%6d%6D@host/%6d%6D?%6d%6D", url.resolve("").toString());
}
@Test public void clearFragment() throws Exception {
HttpUrl url = HttpUrl.parse("http://host/#fragment")
.newBuilder()
.fragment(null)
.build();
assertEquals("http://host/", url.toString());
assertEquals(null, url.fragment());
assertEquals(null, url.encodedFragment());
}
@Test public void clearEncodedFragment() throws Exception {
HttpUrl url = HttpUrl.parse("http://host/#fragment")
.newBuilder()
.encodedFragment(null)
.build();
assertEquals("http://host/", url.toString());
assertEquals(null, url.fragment());
assertEquals(null, url.encodedFragment());
}
}

View File

@@ -851,14 +851,16 @@ public final class HttpUrl {
}
public Builder fragment(String fragment) {
if (fragment == null) throw new IllegalArgumentException("fragment == null");
this.encodedFragment = canonicalize(fragment, FRAGMENT_ENCODE_SET, false, false);
this.encodedFragment = fragment != null
? canonicalize(fragment, FRAGMENT_ENCODE_SET, false, false)
: null;
return this;
}
public Builder encodedFragment(String encodedFragment) {
if (encodedFragment == null) throw new IllegalArgumentException("encodedFragment == null");
this.encodedFragment = canonicalize(encodedFragment, FRAGMENT_ENCODE_SET, true, false);
this.encodedFragment = encodedFragment != null
? canonicalize(encodedFragment, FRAGMENT_ENCODE_SET, true, false)
: null;
return this;
}