Search in sources :

Example 66 with DefaultLastHttpContent

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());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.testng.annotations.Test)

Example 67 with DefaultLastHttpContent

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());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.testng.annotations.Test)

Example 68 with DefaultLastHttpContent

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());
    }
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Gzipper(com.netflix.zuul.util.Gzipper) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 69 with DefaultLastHttpContent

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());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 70 with DefaultLastHttpContent

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());
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) SessionContext(com.netflix.zuul.context.SessionContext) Test(org.junit.Test)

Aggregations

DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)73 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)32 Test (org.junit.Test)27 ByteBuf (io.netty.buffer.ByteBuf)26 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)26 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)24 HttpContent (io.netty.handler.codec.http.HttpContent)17 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)11 HttpResponse (io.netty.handler.codec.http.HttpResponse)11 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)10 Test (org.junit.jupiter.api.Test)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)7 Channel (io.netty.channel.Channel)6 Test (org.testng.annotations.Test)6 SessionContext (com.netflix.zuul.context.SessionContext)5 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5