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

Changing the scheme will update the default port

This commit is contained in:
Michael Evans
2015-08-05 01:42:28 -04:00
parent e19b7913eb
commit 5bdcda6697
2 changed files with 23 additions and 1 deletions

View File

@@ -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")

View File

@@ -570,7 +570,12 @@ public final class HttpUrl {
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());