private static void AssertEmptyResponse(EmbeddedChannel channel, HttpResponseStatus status, AsciiString headerName, bool headerStripped) { var response = new DefaultHttpResponse(HttpVersion.Http11, status); if (HttpHeaderNames.ContentLength.ContentEquals(headerName)) { response.Headers.Set(headerName, "0"); } else if (HttpHeaderNames.TransferEncoding.ContentEquals(headerName)) { response.Headers.Set(headerName, HttpHeaderValues.Chunked); } Assert.True(channel.WriteOutbound(response)); Assert.True(channel.WriteOutbound(EmptyLastHttpContent.Default)); var buffer = channel.ReadOutbound <IByteBuffer>(); StringBuilder responseText = new StringBuilder(); responseText.Append(HttpVersion.Http11.ToString()).Append(' ').Append(status.ToString()).Append("\r\n"); if (!headerStripped && headerName != null) { responseText.Append(headerName).Append(": "); if (HttpHeaderNames.ContentLength.ContentEquals(headerName)) { responseText.Append('0'); } else { responseText.Append(HttpHeaderValues.Chunked.ToString()); } responseText.Append("\r\n"); } responseText.Append("\r\n"); Assert.Equal(responseText.ToString(), buffer.ToString(Encoding.ASCII)); buffer.Release(); buffer = channel.ReadOutbound <IByteBuffer>(); buffer.Release(); }
static void SendHttpResponse(IChannelHandlerContext ctx, IFullHttpRequest req, IFullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). HttpResponseStatus responseStatus = res.Status; if (responseStatus.Code != 200) { ByteBufferUtil.WriteUtf8(res.Content, responseStatus.ToString()); HttpUtil.SetContentLength(res, res.Content.ReadableBytes); } // Send the response and close the connection if necessary. var keepAlive = HttpUtil.IsKeepAlive(req) && responseStatus.Code == 200; HttpUtil.SetKeepAlive(res, keepAlive); var future = ctx.WriteAndFlushAsync(res); if (!keepAlive) { future.CloseOnComplete(ctx.Channel); } }