Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Netty上网络代理的单元测试_Java_Junit_Network Programming_Netty - Fatal编程技术网

Java Netty上网络代理的单元测试

Java Netty上网络代理的单元测试,java,junit,network-programming,netty,Java,Junit,Network Programming,Netty,我试图为netty的HexDumpProxyFrontendHandler创建一个单元测试。但在使用EmbeddedChannel时遇到困难。这是我的密码: public class HexDumpProxyFrontendHandlerTest { @Test public void test1() throws Exception { HttpRequest request = new DefaultFullHttpRequest(HttpVersion.H

我试图为netty的HexDumpProxyFrontendHandler创建一个单元测试。但在使用EmbeddedChannel时遇到困难。这是我的密码:

public class HexDumpProxyFrontendHandlerTest {

    @Test
    public void test1() throws Exception {
        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
        HttpUtil.set100ContinueExpected(request, true);
        EmbeddedChannel channel = new EmbeddedChannel(new HexDumpProxyFrontendHandler("127.0.0.1", 8080));

        channel.writeInbound(request);
    }

}
我得到了
java.nio.channels.ClosedChannelException
,因为我知道代码试图连接到另一台服务器-
127.0.0.1:8080

我想知道有没有办法用EmbeddedChannel测试这段代码?或者我应该创建某种模拟服务器

我开始使用它来创建目标http服务器。它是创建存根服务器的特殊框架。我在另一个线程中运行代理,并通过网络流读取数据。现在测试看起来像:

private static final URI TARGET_LINK = URI.create("http://127.0.0.1:12306/target");
private Runner runner;

@Before
public void setUp() throws Exception {
    HttpServer server = httpServer(TARGET_LINK.getPort());
    server.request(and(by(uri("/target")), by(version(VERSION_1_1))))
            .response(with(text(TARGET_RESPONSE_BASE64)), header("Content-Type", "base64"));
    server.response(DEFAULT_RESPONSE);
    server.request(and(by(uri("/binary")), by(version(VERSION_1_1))))
            .response(with(text(BINARY_RESPONSE_BASE64)), header("Content-Type", "base64"));
    runner = runner(server);
    runner.start();
}


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

@Test
public void testTarget() throws Exception {
    BinaryProxy proxy = startProxy(TARGET_LINK.toASCIIString());

    byte[] response = getServerResponse();

    proxy.stop();

    assertArrayEquals(wrapLine(TARGET_REPLY), response);
}

private BinaryProxy startProxy(String link) throws InterruptedException {
    final BinaryProxy binaryProxy = new BinaryProxy(LISTEN_PORT, link);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                binaryProxy.start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    TimeUnit.MILLISECONDS.sleep(400);

    return binaryProxy;
}

private byte[] getServerResponse() throws IOException {
    Socket server = new Socket(LISTEN_ADDRESS, LISTEN_PORT);
    OutputStream out = server.getOutputStream();
    out.write(TEST_MESSAGE);
    out.flush();

    InputStream is = server.getInputStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[512];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();

    return buffer.toByteArray();
}
我知道这不是单元测试,但我没有找到比这更好的方法


对于FrontendHandler,我在
EmbeddedChannel
类中的所有实验都失败了。我只能将这个类与BackendHandler一起使用。

使用
EmbeddedChannel
进行测试的全部目的不是连接到外部套接字。启动模拟服务器是一种有效的集成测试,但它并不是对您自己的语句“我试图为HexDumpProxyFrontendHandler创建一个单元测试”的回答。@AbhijitSarkar我需要测试我的代码,我不能用
EmbeddedChannel
这样做,我该怎么办?如果你知道一个更好的方法,请分享,我不知道,但你让它看起来像你做的。