From 53d6612cb29ad38514fc8240bade2c7a5c7ce4e7 Mon Sep 17 00:00:00 2001 From: jwilson Date: Sat, 10 Dec 2016 14:31:00 -0500 Subject: [PATCH] Improve toString() of Route, Address, and StreamAllocation. --- .../src/test/java/okhttp3/AddressTest.java | 14 ++++++++++++++ okhttp-tests/src/test/java/okhttp3/CallTest.java | 5 ++++- .../src/test/java/okhttp3/RecordedResponse.java | 6 ++++++ .../internal/connection/RouteSelectorTest.java | 6 ++++++ .../internal/http/RecordingProxySelector.java | 4 ++++ okhttp/src/main/java/okhttp3/Address.java | 15 +++++++++++++++ okhttp/src/main/java/okhttp3/Route.java | 4 ++++ .../internal/connection/StreamAllocation.java | 5 +++-- 8 files changed, 56 insertions(+), 3 deletions(-) diff --git a/okhttp-tests/src/test/java/okhttp3/AddressTest.java b/okhttp-tests/src/test/java/okhttp3/AddressTest.java index 30e376be5..ad07b7824 100644 --- a/okhttp-tests/src/test/java/okhttp3/AddressTest.java +++ b/okhttp-tests/src/test/java/okhttp3/AddressTest.java @@ -15,6 +15,7 @@ */ package okhttp3; +import java.net.Proxy; import java.util.List; import javax.net.SocketFactory; import okhttp3.internal.Util; @@ -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()); + } } diff --git a/okhttp-tests/src/test/java/okhttp3/CallTest.java b/okhttp-tests/src/test/java/okhttp3/CallTest.java index fcea93e34..bd754f799 100644 --- a/okhttp-tests/src/test/java/okhttp3/CallTest.java +++ b/okhttp-tests/src/test/java/okhttp3/CallTest.java @@ -1013,7 +1013,10 @@ public final class CallTest { 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 { diff --git a/okhttp-tests/src/test/java/okhttp3/RecordedResponse.java b/okhttp-tests/src/test/java/okhttp3/RecordedResponse.java index 3410775b6..460ab6684 100644 --- a/okhttp-tests/src/test/java/okhttp3/RecordedResponse.java +++ b/okhttp-tests/src/test/java/okhttp3/RecordedResponse.java @@ -154,6 +154,12 @@ public final class RecordedResponse { 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; diff --git a/okhttp-tests/src/test/java/okhttp3/internal/connection/RouteSelectorTest.java b/okhttp-tests/src/test/java/okhttp3/internal/connection/RouteSelectorTest.java index 272186bff..cecd9daa8 100644 --- a/okhttp-tests/src/test/java/okhttp3/internal/connection/RouteSelectorTest.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/connection/RouteSelectorTest.java @@ -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()); diff --git a/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java b/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java index 33a51500e..bd4038d4a 100644 --- a/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/http/RecordingProxySelector.java @@ -49,4 +49,8 @@ public final class RecordingProxySelector extends ProxySelector { Util.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(), ioe.getMessage())); } + + @Override public String toString() { + return "RecordingProxySelector"; + } } diff --git a/okhttp/src/main/java/okhttp3/Address.java b/okhttp/src/main/java/okhttp3/Address.java index 4fd97764a..8aef1932a 100644 --- a/okhttp/src/main/java/okhttp3/Address.java +++ b/okhttp/src/main/java/okhttp3/Address.java @@ -180,4 +180,19 @@ public final class Address { 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(); + } } diff --git a/okhttp/src/main/java/okhttp3/Route.java b/okhttp/src/main/java/okhttp3/Route.java index 8aaa0f5b8..7c7e9635e 100644 --- a/okhttp/src/main/java/okhttp3/Route.java +++ b/okhttp/src/main/java/okhttp3/Route.java @@ -96,4 +96,8 @@ public final class Route { result = 31 * result + inetSocketAddress.hashCode(); return result; } + + @Override public String toString() { + return "Route{" + inetSocketAddress + "}"; + } } diff --git a/okhttp/src/main/java/okhttp3/internal/connection/StreamAllocation.java b/okhttp/src/main/java/okhttp3/internal/connection/StreamAllocation.java index b78e3c54b..a2213dfc9 100644 --- a/okhttp/src/main/java/okhttp3/internal/connection/StreamAllocation.java +++ b/okhttp/src/main/java/okhttp3/internal/connection/StreamAllocation.java @@ -26,9 +26,9 @@ import okhttp3.internal.Internal; 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; @@ -342,7 +342,8 @@ public final class StreamAllocation { } @Override public String toString() { - return address.toString(); + RealConnection connection = connection(); + return connection != null ? connection.toString() : address.toString(); } public static final class StreamAllocationReference extends WeakReference {