1

我使用 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内容太大,但我不知道在哪里设置这个内容长度。我只是希望这个异常可以解决。

4

1 回答 1

2

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

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

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

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

于 2016-04-02T23:45:34.800 回答