Skip to content

Commit

Permalink
Changing the scheme will update the default port
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEvans committed Aug 7, 2015
1 parent e19b791 commit 5bdcda6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 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 @@ -602,6 +602,23 @@ public final class HttpUrlTest {
assertEquals("fragment", url.fragment());
}

@Test public void changingSchemeChangesDefaultPort() throws Exception {
assertEquals(443, HttpUrl.parse("http://example.com")
.newBuilder()
.scheme("https")
.build().port());

assertEquals(80, HttpUrl.parse("https://example.com")
.newBuilder()
.scheme("http")
.build().port());

assertEquals(1234, HttpUrl.parse("https://example.com:1234")
.newBuilder()
.scheme("http")
.build().port());
}

@Test public void composeEncodesWhitespace() throws Exception {
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
Expand Down
7 changes: 6 additions & 1 deletion okhttp/src/main/java/com/squareup/okhttp/HttpUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,12 @@ public Builder newBuilder() {
result.encodedUsername = encodedUsername();
result.encodedPassword = encodedPassword();
result.host = host;
result.port = port;
// If we're set to a default port, unset it, in case of a scheme change.
if (port == defaultPort(scheme)) {
result.port = -1;
} else {
result.port = port;
}
result.encodedPathSegments.clear();
result.encodedPathSegments.addAll(encodedPathSegments());
result.encodedQuery(encodedQuery());
Expand Down

0 comments on commit 5bdcda6

Please sign in to comment.