Search in sources :

Example 1 with HttpProxyConnectException

use of io.netty.handler.proxy.HttpProxyHandler.HttpProxyConnectException in project netty by netty.

the class HttpProxyHandlerTest method testExceptionDuringConnect.

@Test
public void testExceptionDuringConnect() throws Exception {
    EventLoopGroup group = null;
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        group = new DefaultEventLoopGroup(1);
        final LocalAddress addr = new LocalAddress("a");
        final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
        ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addFirst(new HttpResponseEncoder());
                DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY);
                response.headers().add("name", "value");
                response.headers().add(HttpHeaderNames.CONTENT_LENGTH, "0");
                ch.writeAndFlush(response);
            }
        }).bind(addr);
        serverChannel = sf.sync().channel();
        ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).group(group).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addFirst(new HttpProxyHandler(addr));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        exception.set(cause);
                    }
                });
            }
        }).connect(new InetSocketAddress("localhost", 1234));
        clientChannel = cf.sync().channel();
        clientChannel.close().sync();
        assertTrue(exception.get() instanceof HttpProxyConnectException);
        HttpProxyConnectException actual = (HttpProxyConnectException) exception.get();
        assertNotNull(actual.headers());
        assertEquals("value", actual.headers().get("name"));
    } finally {
        if (clientChannel != null) {
            clientChannel.close();
        }
        if (serverChannel != null) {
            serverChannel.close();
        }
        if (group != null) {
            group.shutdownGracefully();
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpProxyConnectException(io.netty.handler.proxy.HttpProxyHandler.HttpProxyConnectException) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) InetSocketAddress(java.net.InetSocketAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 LocalAddress (io.netty.channel.local.LocalAddress)1 LocalChannel (io.netty.channel.local.LocalChannel)1 LocalServerChannel (io.netty.channel.local.LocalServerChannel)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)1 HttpProxyConnectException (io.netty.handler.proxy.HttpProxyHandler.HttpProxyConnectException)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.jupiter.api.Test)1