Unit tests for network proxy on Netty

2019-08-27 11:16发布

I tried to create a unit test for HexDumpProxyFrontendHandler from standard examples of netty. But have trouble doing so using EmbeddedChannel. Here is my code:

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);
    }

}

I got java.nio.channels.ClosedChannelException as I understood the code tried to connect to another server - 127.0.0.1:8080.

I would like to know is there some way to test this piece of code with EmbeddedChannel? Or I should create some sort of mock server

1条回答
做个烂人
2楼-- · 2019-08-27 11:47

I start using Moco to create target http server. It's special framework for creating stub servers. I run proxy in another thread and read data via network stream. Now test looks like:

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();
}

I know that it isn't a unit test but I didn't find any better way than that.

All my experiments with EmbeddedChannel class were failed for FrontendHandler. I was able to use this class only with BackendHandler.

查看更多
登录 后发表回答