OkHttpProtocolNegotiator { public String negotiate( SSLSocket sslSocket, String hostname, @Nullable List<Protocol> protocols) throws IOException { if (protocols != null) { configureTlsExtensions(sslSocket, hostname, protocols); } try { sslSocket.startHandshake(); String negotiatedProtocol = getSelectedProtocol(sslSocket); if (negotiatedProtocol == null) { throw new RuntimeException("protocol negotiation failed"); } return negotiatedProtocol; } finally { platform.afterHandshake(sslSocket); } } @VisibleForTesting  OkHttpProtocolNegotiator(Platform platform); static OkHttpProtocolNegotiator get(); String negotiate(
      SSLSocket sslSocket, String hostname, @Nullable List<Protocol> protocols); String getSelectedProtocol(SSLSocket socket);  }
@Test public void negotiate_noSelectedProtocol() throws Exception { Platform platform = mock(Platform.class); OkHttpProtocolNegotiator negotiator = new OkHttpProtocolNegotiator(platform); thrown.expect(RuntimeException.class); thrown.expectMessage("protocol negotiation failed"); negotiator.negotiate(sock, "hostname", ImmutableList.of(Protocol.HTTP_2)); }
@Test public void negotiate_android_handshakeFails() throws Exception { AndroidNegotiator negotiator = new AndroidNegotiator(platform, TlsExtensionType.ALPN_AND_NPN); FakeAndroidSslSocket androidSock = new FakeAndroidSslSocket() { @Override public void startHandshake() throws IOException { throw new IOException("expected"); } }; thrown.expect(IOException.class); thrown.expectMessage("expected"); negotiator.negotiate(androidSock, "hostname", ImmutableList.of(Protocol.HTTP_2)); }