Skip to content

Commit

Permalink
Handle null fragments.
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Oct 31, 2015
1 parent 403e127 commit e4dd6cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 20 additions & 0 deletions okhttp-tests/src/test/java/com/squareup/okhttp/HttpUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
10 changes: 6 additions & 4 deletions okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -851,14 +851,16 @@ private void removeAllCanonicalQueryParameters(String canonicalName) {
}

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;
}

Expand Down

0 comments on commit e4dd6cf

Please sign in to comment.