Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve toString() of Route, Address, and StreamAllocation. #3029

Merged
merged 1 commit into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package okhttp3;

import java.net.Proxy;
import java.util.List;
import javax.net.SocketFactory;
import okhttp3.internal.Util;
Expand Down Expand Up @@ -48,4 +49,17 @@ public final class AddressTest {
authenticator, null, protocols, connectionSpecs, new RecordingProxySelector());
assertFalse(a.equals(b));
}

@Test public void addressToString() throws Exception {
Address address = new Address("square.com", 80, dns, socketFactory, null, null, null,
authenticator, null, protocols, connectionSpecs, proxySelector);
assertEquals("Address{square.com:80, proxySelector=RecordingProxySelector}",
address.toString());
}

@Test public void addressWithProxyToString() throws Exception {
Address address = new Address("square.com", 80, dns, socketFactory, null, null, null,
authenticator, Proxy.NO_PROXY, protocols, connectionSpecs, proxySelector);
assertEquals("Address{square.com:80, proxy=" + Proxy.NO_PROXY + "}", address.toString());
}
}
5 changes: 4 additions & 1 deletion okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,10 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
executeSynchronously("/").assertBody("seed connection pool");

// If this succeeds, too many requests were made.
executeSynchronously("/").assertFailure(IOException.class);
executeSynchronously("/")
.assertFailure(IOException.class)
.assertFailureMatches("unexpected end of stream on Connection.*"
+ server.getHostName() + ":" + server.getPort() + ".*");
}

@Test public void recoverFromTlsHandshakeFailure() throws Exception {
Expand Down
6 changes: 6 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/RecordedResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public RecordedResponse assertFailure(String... messages) {
return this;
}

public RecordedResponse assertFailureMatches(String pattern) {
assertNotNull(failure);
assertTrue(failure.getMessage(), failure.getMessage().matches(pattern));
return this;
}

public RecordedResponse assertSentRequestAtMillis(long minimum, long maximum) {
assertDateInRange(minimum, response.sentRequestAtMillis(), maximum);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ public final class RouteSelectorTest {
assertEquals("127.0.0.1", RouteSelector.getHostString(socketAddress));
}

@Test public void routeToString() throws Exception {
Route route = new Route(httpAddress(), Proxy.NO_PROXY,
InetSocketAddress.createUnresolved("host", 1234));
assertEquals("Route{host:1234}", route.toString());
}

private void assertRoute(Route route, Address address, Proxy proxy, InetAddress socketAddress,
int socketPort) {
assertEquals(address, route.address());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public void assertRequests(URI... expectedUris) {
Util.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(),
ioe.getMessage()));
}

@Override public String toString() {
return "RecordingProxySelector";
}
}
15 changes: 15 additions & 0 deletions okhttp/src/main/java/okhttp3/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,19 @@ && equal(this.hostnameVerifier, that.hostnameVerifier)
result = 31 * result + (certificatePinner != null ? certificatePinner.hashCode() : 0);
return result;
}

@Override public String toString() {
StringBuilder result = new StringBuilder()
.append("Address{")
.append(url.host()).append(":").append(url.port());

if (proxy != null) {
result.append(", proxy=").append(proxy);
} else {
result.append(", proxySelector=").append(proxySelector);
}

result.append("}");
return result.toString();
}
}
4 changes: 4 additions & 0 deletions okhttp/src/main/java/okhttp3/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ public boolean requiresTunnel() {
result = 31 * result + inetSocketAddress.hashCode();
return result;
}

@Override public String toString() {
return "Route{" + inetSocketAddress + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import okhttp3.internal.Util;
import okhttp3.internal.http.HttpCodec;
import okhttp3.internal.http1.Http1Codec;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.ErrorCode;
import okhttp3.internal.http2.Http2Codec;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.StreamResetException;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand Down Expand Up @@ -342,7 +342,8 @@ public boolean hasMoreRoutes() {
}

@Override public String toString() {
return address.toString();
RealConnection connection = connection();
return connection != null ? connection.toString() : address.toString();
}

public static final class StreamAllocationReference extends WeakReference<StreamAllocation> {
Expand Down