diff --git a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java index 5c19a28f2dc..07877085887 100644 --- a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java @@ -394,13 +394,6 @@ private static ProtocolNegotiator createProtocolNegotiatorByType( case PLAINTEXT_UPGRADE: return ProtocolNegotiators.plaintextUpgrade(); case TLS: - if (sslContext == null) { - try { - sslContext = GrpcSslContexts.forClient().build(); - } catch (SSLException ex) { - throw new RuntimeException(ex); - } - } return ProtocolNegotiators.tls(sslContext, authority); default: throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType); @@ -472,6 +465,13 @@ private static final class NettyTransportFactory implements ClientTransportFacto this.channelType = channelType; this.negotiationType = negotiationType; this.channelOptions = new HashMap, Object>(channelOptions); + if (negotiationType == NegotiationType.TLS && sslContext == null) { + try { + sslContext = GrpcSslContexts.forClient().build(); + } catch (SSLException ex) { + throw new RuntimeException(ex); + } + } this.sslContext = sslContext; if (transportCreationParamsFilterFactory == null) { diff --git a/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java b/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java index 9f03113dbe5..38d324a3878 100644 --- a/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java @@ -158,16 +158,11 @@ public void createProtocolNegotiator_plaintextUpgrade() { @Test public void createProtocolNegotiator_tlsWithNoContext() { - ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( + thrown.expect(NullPointerException.class); + NettyChannelBuilder.createProtocolNegotiator( "authority:1234", NegotiationType.TLS, noSslContext); - - assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); - ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; - - assertEquals("authority", n.getHost()); - assertEquals(1234, n.getPort()); } @Test @@ -185,11 +180,11 @@ public void createProtocolNegotiator_tlsWithClientContext() throws SSLException } @Test - public void createProtocolNegotiator_tlsWithAuthorityFallback() { + public void createProtocolNegotiator_tlsWithAuthorityFallback() throws SSLException { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "bad_authority", NegotiationType.TLS, - noSslContext); + GrpcSslContexts.forClient().build()); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator;