dreamhead / moco

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

Socket服务下,如果两条规则都能匹配上,那么最终返回后面的一个的响应。 #289

Open waitshang opened 3 years ago

waitshang commented 3 years ago

Mock服务端:

import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.Runner;
import com.github.dreamhead.moco.SocketServer;

/**
 * Created by shangwei2009@hotmail.com on 2021/1/18 9:08
 */
public class Eighth {
    public static void main(String[] args) {
        final SocketServer socketServer = Moco.socketServer(8080);
        socketServer.response("one");
        socketServer.response("two");

        final Runner runner = Runner.runner(socketServer);
        runner.start();
    }
}

客户端:

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

import java.nio.charset.StandardCharsets;

/**
 * Created by shangwei2009@hotmail.com on 2021/1/18 9:10
 */
public class EighthClient {
    public static void main(String[] args) throws InterruptedException {
        final NioEventLoopGroup group = new NioEventLoopGroup();
        final Bootstrap bootstrap = new Bootstrap();
        bootstrap.channel(NioSocketChannel.class)
                .group(group)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        final ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new LoggingHandler())
                                .addLast(new ChannelInboundHandlerAdapter() {
                                    private CompositeByteBuf bufs = ByteBufAllocator.DEFAULT.compositeBuffer();

                                    @Override
                                    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
                                        if (msg instanceof ByteBuf) {
                                            ByteBuf buf = (ByteBuf) msg;
                                            bufs.addComponent(buf);
                                            bufs.writerIndex(bufs.writerIndex() + buf.writerIndex());
                                        }
                                    }

                                    @Override
                                    public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
                                        if (bufs.numComponents() > 0) {
                                            ctx.fireChannelRead(bufs);
                                            bufs = ByteBufAllocator.DEFAULT.compositeBuffer();
                                        }

                                        ctx.fireChannelReadComplete();
                                    }
                                })
                                .addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                                        final byte[] bytes = new byte[msg.readableBytes()];
                                        msg.readBytes(bytes);
                                        System.out.println(new String(bytes));
                                        ctx.close();
                                    }
                                });
                    }
                });
        final ChannelFuture future = bootstrap.connect("127.0.0.1", 8080).sync();

        final Channel channel = future.channel();
        final ByteBuf byteBuf = Unpooled.copiedBuffer("client", StandardCharsets.UTF_8);
        channel.writeAndFlush(byteBuf).sync();
        channel.closeFuture().sync();
        group.shutdownGracefully();
    }
}

客户端收到响应two,在Http服务下,必定收到one。而且,Http服务下如果都不配置request会报错。