use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class ZuulMessageImplTest method testBufferBody3GetBody.
@Test
public void testBufferBody3GetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes())));
msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("World!".getBytes())));
msg.bufferBodyContents(new DefaultLastHttpContent());
final String body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(0, msg.getHeaders().getAll("Content-Length").size());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class ZuulMessageImpl method setBodyAsText.
@Override
public void setBodyAsText(String bodyText) {
disposeBufferedBody();
if (!Strings.isNullOrEmpty(bodyText)) {
final ByteBuf content = Unpooled.copiedBuffer(bodyText.getBytes(Charsets.UTF_8));
bufferBodyContents(new DefaultLastHttpContent(content));
setContentLength(bodyText.getBytes(CS_UTF8).length);
} else {
bufferBodyContents(new DefaultLastHttpContent());
setContentLength(0);
}
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class ZuulMessageImpl method setBody.
@Override
public void setBody(byte[] body) {
disposeBufferedBody();
if (body != null && body.length > 0) {
final ByteBuf content = Unpooled.copiedBuffer(body);
bufferBodyContents(new DefaultLastHttpContent(content));
setContentLength(body.length);
} else {
bufferBodyContents(new DefaultLastHttpContent());
setContentLength(0);
}
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class Http2ContentLengthEnforcingHandlerTest method failsOnShortContent.
@Test
public void failsOnShortContent() {
EmbeddedChannel chan = new EmbeddedChannel();
chan.pipeline().addLast(new Http2ContentLengthEnforcingHandler());
DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
req.headers().add(HttpHeaderNames.CONTENT_LENGTH, 2);
chan.writeInbound(req);
Object out = chan.readOutbound();
assertThat(out).isNull();
DefaultHttpContent content = new DefaultHttpContent(ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "a"));
chan.writeInbound(content);
out = chan.readOutbound();
assertThat(out).isNull();
DefaultHttpContent content2 = new DefaultLastHttpContent();
chan.writeInbound(content2);
out = chan.readOutbound();
assertThat(out).isInstanceOf(Http2ResetFrame.class);
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ambry by linkedin.
the class NettyMessageProcessorTest method requestHandleWithBadInputTest.
/**
* Tests for error handling flow when bad input streams are provided to the {@link NettyMessageProcessor}.
*/
@Test
public void requestHandleWithBadInputTest() throws IOException {
String content = "@@randomContent@@@";
// content without request.
EmbeddedChannel channel = createChannel();
channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// content without request on a channel that was kept alive
channel = createChannel();
// send and receive response for a good request and keep the channel alive
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, MockRestRequestService.ECHO_REST_METHOD, null));
channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// drain the content
while (channel.readOutbound() != null) {
;
}
assertTrue("Channel is not active", channel.isActive());
// send content without request
channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// content when no content is expected.
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// wrong HTTPObject.
channel = createChannel();
channel.writeInbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// request while another request is in progress.
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
// decoding failure
channel = createChannel();
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, "/", null);
httpRequest.setDecoderResult(DecoderResult.failure(new IllegalStateException("Induced failure")));
channel.writeInbound(httpRequest);
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
// unsupported method
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.TRACE, "/", null));
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
}
Aggregations