使用Selenium和BrowserMob时会发生TooLongFrameException

时间:2016-04-01 13:01:36

标签: selenium browsermob browsermob-proxy

我使用Selenium和BrowserMob来执行测试,我使用requestfilter和responsefilter修改请求标头和响应内容,如下所示:

public void proxySettings(){

    proxy.addResponseFilter((response, contents, messageInfo) -> {
        if((messageInfo.getOriginalRequest().toString().contains("/draft/footer"))&&(response.getStatus().code()==200)){
            contents.setTextContents(FileUtil.read("JsonPool//draft_footer.json"));
        }
    });
    proxy.addRequestFilter(new RequestFilter() {
        @Override
        public io.netty.handler.codec.http.HttpResponse filterRequest(io.netty.handler.codec.http.HttpRequest request,HttpMessageContents contents, HttpMessageInfo messageInfo) {
            if(messageInfo.getOriginalRequest().toString().contains("v1/doc"))
            {
                request.headers().set("Authorization", "Basic cHJpbmNpcGVDHHEHDNvbTpwd2Q0dGVzdCE=");
            }
            return null;
        }
    });
}

启动代理后,Selenium运行测试。不知何故,异常显示并且运行停止。

[LittleProxy-0-ProxyToServerWorker-4] ERROR org.littleshoot.proxy.impl.ProxyToServerConnection - (AWAITING_INITIAL) [id: 0x23fdf062, /192.168.1.103:49953 => www.html5rocks.com/216.239.38.21:80]: Caught an exception on ProxyToServerConnection io.netty.handler.codec.TooLongFrameException: HTTP content length exceeded 2097152 bytes.
at io.netty.handler.codec.http.HttpObjectAggregator.decode(HttpObjectAggregator.java:218) ~[browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.handler.codec.http.HttpObjectAggregator.decode(HttpObjectAggregator.java:57) ~[browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) [browsermob-dist-2.1.0-beta-4.jar:?]
at org.littleshoot.proxy.impl.ProxyConnection$ResponseReadMonitor.channelRead(ProxyConnection.java:738) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:276) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:263) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) [browsermob-dist-2.1.0-beta-4.jar:?]
at org.littleshoot.proxy.impl.ProxyConnection$BytesReadMonitor.channelRead(ProxyConnection.java:692) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [browsermob-dist-2.1.0-beta-4.jar:?]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) [browsermob-dist-2.1.0-beta-4.jar:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_40]

[ProxyToServerConnection io.netty.handler.codec.TooLongFrameException:HTTP内容长度超过2097152字节。]

我做了一些研究,发现http内容太大了,但我不知道在哪里设置这个内容长度。我只想要解决这个异常。

1 个答案:

答案 0 :(得分:2)

您必须自己设置最大请求/响应大小才能处理大于2097152字节的消息。

简而言之,像这样添加您的请求/响应过滤器:

proxyServer.addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter, 16777216)); // or whatever buffer size you want

此处有关于此问题的更多信息:https://github.com/lightbody/browsermob-proxy/issues/372