[英]netty test: how to send a post to handler
我想在Netty中测试ChannelInboundHandler,它的输入是HTTPFullRequest
。
这是我的帖子:
POST HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
paramA=dataA\
¶mB=dataB
我使用EmbbedChannel测试处理程序,这是代码。
EmbeddedChannel channel = new EmbeddedChannel(
new ChannelInitializer<EmbeddedChannel>() {
@Override
protected void initChannel(EmbeddedChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("httpCodec", new HttpServerCodec());
p.addLast("aggregator", new HttpObjectAggregator(100000));
p.addLast("readPost", new ReadPost());
}
}
);
// input contains the post above.
assertTrue((channel.writeInbound(input.retain())));
但是我的ReadPost
总是读到一个抱怨failure(java.lang.IllegalArgumentException: text is empty (possibly HTTP/0.9))
帖子failure(java.lang.IllegalArgumentException: text is empty (possibly HTTP/0.9))
这是我的ReadPost处理程序快照:
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
if (req.method().equals(HttpMethod.POST)) {
.....
}
有人知道为什么吗? 提前致谢。
埃姆。
这仅仅是http语法的错误。
POST HTTP/1.1
需要主机名,正确的语法类似于POST / HTTP/1.1
。
http标头也缺少Content-Length
。
是的,仅此而已。
我真傻
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.