From 462e88af7e62eb33bcdb62a3869a0c9dd616d99a Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 9 Jul 2019 14:39:32 +0200 Subject: [PATCH] DecoratingHttp2ConnectionEncoder.consumeRemoteSettings must not throw if delegate is instance of Http2SettingsReceivedConsumer (#9343) Motivation: b3dba317d797e21cc253bb6ad6776307297f612e introduced the concept of Http2SettingsReceivedConsumer but did not correctly inplement DecoratingHttp2ConnectionEncoder.consumeRemoteSettings(...). Modifications: - Add missing `else` around the throws - Add unit tests Result: Correctly implement DecoratingHttp2ConnectionEncoder.consumeRemoteSettings(...) --- .../DecoratingHttp2ConnectionEncoder.java | 5 ++- .../DecoratingHttp2ConnectionEncoderTest.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 codec-http2/src/test/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoderTest.java diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoder.java index 00262f40f6..c67b4addf3 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoder.java @@ -65,8 +65,9 @@ public class DecoratingHttp2ConnectionEncoder extends DecoratingHttp2FrameWriter public void consumeReceivedSettings(Http2Settings settings) { if (delegate instanceof Http2SettingsReceivedConsumer) { ((Http2SettingsReceivedConsumer) delegate).consumeReceivedSettings(settings); + } else { + throw new IllegalStateException("delegate " + delegate + " is not an instance of " + + Http2SettingsReceivedConsumer.class); } - throw new IllegalStateException("delegate " + delegate + " is not an instance of " + - Http2SettingsReceivedConsumer.class); } } diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoderTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoderTest.java new file mode 100644 index 0000000000..0eb9a73cc0 --- /dev/null +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DecoratingHttp2ConnectionEncoderTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2019 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.netty.handler.codec.http2; + +import org.junit.Test; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + +public class DecoratingHttp2ConnectionEncoderTest { + + @Test(expected = IllegalStateException.class) + public void testConsumeReceivedSettingsThrows() { + Http2ConnectionEncoder encoder = mock(Http2ConnectionEncoder.class); + DecoratingHttp2ConnectionEncoder decoratingHttp2ConnectionEncoder = + new DecoratingHttp2ConnectionEncoder(encoder); + decoratingHttp2ConnectionEncoder.consumeReceivedSettings(Http2Settings.defaultSettings()); + } + + @Test + public void testConsumeReceivedSettingsDelegate() { + TestHttp2ConnectionEncoder encoder = mock(TestHttp2ConnectionEncoder.class); + DecoratingHttp2ConnectionEncoder decoratingHttp2ConnectionEncoder = + new DecoratingHttp2ConnectionEncoder(encoder); + + Http2Settings settings = Http2Settings.defaultSettings(); + decoratingHttp2ConnectionEncoder.consumeReceivedSettings(Http2Settings.defaultSettings()); + verify(encoder, times(1)).consumeReceivedSettings(eq(settings)); + } + + private interface TestHttp2ConnectionEncoder extends Http2ConnectionEncoder, Http2SettingsReceivedConsumer { } +}