use of io.netty.handler.codec.http.DefaultLastHttpContent in project rest.li by linkedin.
the class TestChannelPoolStreamHandler method testConnectionClose.
@Test(dataProvider = "connectionClose")
public void testConnectionClose(String headerName, List<String> headerValue) {
EmbeddedChannel ch = getChannel();
FakePool pool = new FakePool();
ch.attr(ChannelPoolStreamHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED);
HttpContent lastChunk = new DefaultLastHttpContent();
response.headers().set(headerName, headerValue);
ch.writeInbound(response);
ch.writeInbound(lastChunk);
Assert.assertTrue(pool.isDisposeCalled());
Assert.assertFalse(pool.isPutCalled());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project rest.li by linkedin.
the class TestChannelPoolStreamHandler method testConnectionKeepAlive.
@Test(dataProvider = "connectionKeepAlive")
public void testConnectionKeepAlive(String headerName, List<String> headerValue) {
EmbeddedChannel ch = getChannel();
FakePool pool = new FakePool();
ch.attr(ChannelPoolStreamHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED);
HttpContent lastChunk = new DefaultLastHttpContent();
response.headers().set(headerName, headerValue);
ch.writeInbound(response);
ch.writeInbound(lastChunk);
Assert.assertFalse(pool.isDisposeCalled());
Assert.assertTrue(pool.isPutCalled());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class GZipResponseFilter method processContentChunk.
@Override
public HttpContent processContentChunk(ZuulMessage resp, HttpContent chunk) {
final Gzipper gzipper = (Gzipper) resp.getContext().get(CommonContextKeys.GZIPPER);
gzipper.write(chunk);
if (chunk instanceof LastHttpContent) {
gzipper.finish();
return new DefaultLastHttpContent(gzipper.getByteBuf());
} else {
return new DefaultHttpContent(gzipper.getByteBuf());
}
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class GZipResponseFilterTest method prepareResponseBody_NeedsGZipping.
@Test
public void prepareResponseBody_NeedsGZipping() throws Exception {
originalRequestHeaders.set("Accept-Encoding", "gzip");
byte[] originBody = "blah".getBytes();
response.getHeaders().set("Content-Length", Integer.toString(originBody.length));
// Force GZip for small response
Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true);
response.setHasBody(true);
assertTrue(filter.shouldFilter(response));
final HttpResponseMessage result = filter.apply(response);
final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain());
final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent());
final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()];
final int hc1Len = hc1.content().readableBytes();
final int hc2Len = hc2.content().readableBytes();
hc1.content().readBytes(body, 0, hc1Len);
hc2.content().readBytes(body, hc1Len, hc2Len);
String bodyStr;
// Check body is a gzipped version of the origin body.
try (ByteArrayInputStream bais = new ByteArrayInputStream(body);
GZIPInputStream gzis = new GZIPInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
int b;
while ((b = gzis.read()) != -1) {
baos.write(b);
}
bodyStr = baos.toString("UTF-8");
}
assertEquals("blah", bodyStr);
assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding"));
// Check Content-Length header has been removed
assertEquals(0, result.getHeaders().getAll("Content-Length").size());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project zuul by Netflix.
the class ZuulMessageImplTest method testBufferBody2GetBody.
@Test
public void testBufferBody2GetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes())));
msg.bufferBodyContents(new DefaultLastHttpContent(Unpooled.copiedBuffer("World!".getBytes())));
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());
}
Aggregations