dreamhead / moco

Easy Setup Stub Server
MIT License
4.37k stars 1.08k forks source link

gradle引入依赖,Postman发送formdata请求会发生报错。 #299

Closed tian-pengfei closed 3 years ago

tian-pengfei commented 3 years ago
    @Before
    public void setup() {
        HttpServer server = httpServer(12306);
        server.request(and(by(uri("/target")),
                            eq(form("name"),"Kevin"),
                            eq(form("age"),"18")))
                .response("foo");
        runner = runner(server);
        runner.start();
    }

//    @After
//    public void tearDown() {
//        runner.stop();
//    }

    @Test
    public void should_response_as_expected() throws IOException, InterruptedException {

        Content content = Request.Post("http://localhost:12306/target")
                .bodyForm(new BasicNameValuePair("name","Kevin")
                         ,new BasicNameValuePair("age","18"))
                .execute().returnContent();
        System.out.println(content);
        assertThat(content.asString(), is("foo"));
        while(true){
            Thread.sleep(10000L);
        }
    }

引入上面代码 然后利用postman发送下面请求 image

就会发生如下错误

io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.util.internal.ReferenceCountUpdater.toLiveRealRefCnt(ReferenceCountUpdater.java:74)
    at io.netty.util.internal.ReferenceCountUpdater.release(ReferenceCountUpdater.java:138)
    at io.netty.util.AbstractReferenceCounted.release(AbstractReferenceCounted.java:76)
    at io.netty.handler.codec.http.multipart.MixedAttribute.release(MixedAttribute.java:320)
    at io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder.destroy(HttpPostMultipartRequestDecoder.java:947)
    at io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.destroy(HttpPostRequestDecoder.java:247)
    at com.github.dreamhead.moco.extractor.FormsRequestExtractor.doExtract(FormsRequestExtractor.java:42)
    at com.github.dreamhead.moco.HttpRequestExtractor.extract(HttpRequestExtractor.java:12)
    at com.github.dreamhead.moco.extractor.FormRequestExtractor.doExtract(FormRequestExtractor.java:19)
    at com.github.dreamhead.moco.HttpRequestExtractor.extract(HttpRequestExtractor.java:12)
    at com.github.dreamhead.moco.matcher.AbstractOperatorMatcher.match(AbstractOperatorMatcher.java:32)
    at com.github.dreamhead.moco.matcher.AndRequestMatcher.doMatch(AndRequestMatcher.java:14)
    at com.github.dreamhead.moco.matcher.CompositeRequestMatcher.match(CompositeRequestMatcher.java:44)
    at com.github.dreamhead.moco.setting.BaseSetting.match(BaseSetting.java:30)
    at com.github.dreamhead.moco.internal.Responser.getResponse(Responser.java:22)
    at com.github.dreamhead.moco.internal.BaseActualServer.getResponse(BaseActualServer.java:143)
    at com.github.dreamhead.moco.internal.HttpHandler.doGetResponse(HttpHandler.java:41)
    at com.github.dreamhead.moco.internal.HttpHandler.getHttpResponse(HttpHandler.java:36)
    at com.github.dreamhead.moco.internal.HttpHandler.handleRequest(HttpHandler.java:27)
    at com.github.dreamhead.moco.internal.MocoHandler.handleHttpRequest(MocoHandler.java:55)
    at com.github.dreamhead.moco.internal.MocoHandler.channelRead0(MocoHandler.java:39)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1408)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:682)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:617)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:534)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:906)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

报错原因如下图 image 为什么增加这句话 参考这个 https://github.com/netty/netty/issues/7814 高版本又把这句话给删了, image

利用gradle引入项目中 解决方法(升级netty):

    implementation ('com.github.dreamhead:moco-core:1.2.0')
    {
        exclude  group:"io.netty"
    }
    implementation "io.netty:netty-codec-http:4.1.65.Final"
    implementation "io.netty:netty-handler:4.1.65.Final"

netty应用广泛注意其他依赖影响netty版本。 其实本来打算隐藏这个错误的,又担心内存泄漏所以就升级了netty,不知道会不会出现问题。 希望作者大大早日在maven上发布新版本,很期待

Hi Open Source, I'm coming