Motivation: In the past we had the following class hierarchy: Http2ConnectionHandler --- Http2FrameCodec -- Http2MultiplexCodec This hierarchy makes it impossible to plug in any code that would like to act on Http2Frame and Http2StreamFrame which can be quite useful for various situations (like metrics, logging etc). Beside this it also made the implementtion very hacky. To allow easier maintainance and also allow more flexible costumizations we should split Http2MultiplexCodec and Http2FrameCode. Modifications: - Introduce Http2MultiplexHandler (which is a replacement for Http2MultiplexCodec when used together with Http2FrameCodec) - Mark Http2MultiplexCodecBuilder and Http2MultiplexCodec as deprecated. People should use Http2FrameCodecBuilder / Http2FrameCodec together with Http2MultiplexHandlder in the future - Adjust / Add tests - Adjust examples Result: More flexible usage possible and less hacky / coupled implementation for http2 multiplexing
80 lines
2.8 KiB
Java
80 lines
2.8 KiB
Java
/*
|
|
* Copyright 2018 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 io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
import io.netty.channel.embedded.EmbeddedChannel;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertFalse;
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
public abstract class Http2MultiplexClientUpgradeTest<C extends Http2FrameCodec> {
|
|
|
|
@ChannelHandler.Sharable
|
|
final class NoopHandler extends ChannelInboundHandlerAdapter {
|
|
@Override
|
|
public void channelActive(ChannelHandlerContext ctx) {
|
|
ctx.channel().close();
|
|
}
|
|
}
|
|
|
|
private final class UpgradeHandler extends ChannelInboundHandlerAdapter {
|
|
Http2Stream.State stateOnActive;
|
|
int streamId;
|
|
|
|
@Override
|
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
Http2StreamChannel ch = (Http2StreamChannel) ctx.channel();
|
|
stateOnActive = ch.stream().state();
|
|
streamId = ch.stream().id();
|
|
super.channelActive(ctx);
|
|
}
|
|
}
|
|
|
|
protected abstract C newCodec(ChannelHandler upgradeHandler);
|
|
|
|
protected abstract ChannelHandler newMultiplexer(ChannelHandler upgradeHandler);
|
|
|
|
@Test
|
|
public void upgradeHandlerGetsActivated() throws Exception {
|
|
UpgradeHandler upgradeHandler = new UpgradeHandler();
|
|
C codec = newCodec(upgradeHandler);
|
|
EmbeddedChannel ch = new EmbeddedChannel(codec, newMultiplexer(upgradeHandler));
|
|
|
|
codec.onHttpClientUpgrade();
|
|
|
|
assertFalse(upgradeHandler.stateOnActive.localSideOpen());
|
|
assertTrue(upgradeHandler.stateOnActive.remoteSideOpen());
|
|
assertEquals(1, upgradeHandler.streamId);
|
|
assertTrue(ch.finishAndReleaseAll());
|
|
}
|
|
|
|
@Test(expected = Http2Exception.class)
|
|
public void clientUpgradeWithoutUpgradeHandlerThrowsHttp2Exception() throws Http2Exception {
|
|
C codec = newCodec(null);
|
|
EmbeddedChannel ch = new EmbeddedChannel(codec, newMultiplexer(null));
|
|
try {
|
|
codec.onHttpClientUpgrade();
|
|
} finally {
|
|
assertTrue(ch.finishAndReleaseAll());
|
|
}
|
|
}
|
|
}
|