Search in sources :

Example 81 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBody.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBody() throws Exception {
    byte[] bodyBytes = "foo=bar&a=b&empty=&city=%3c%22new%22%20york%20city%3e&other_city=los+angeles".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req);
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertEquals(5, decoder.getBodyHttpDatas().size());
    Attribute attr = (Attribute) decoder.getBodyHttpData("foo");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("bar", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("a");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("b", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("empty");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("city");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("<\"new\" york city>", attr.getValue());
    attr = (Attribute) decoder.getBodyHttpData("other_city");
    assertTrue(attr.getByteBuf().isDirect());
    assertEquals("los angeles", attr.getValue());
    decoder.destroy();
    assertTrue(req.release());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 82 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithBrokenHexByte0.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithBrokenHexByte0() {
    byte[] bodyBytes = "foo=bar&a=b&empty=%&city=paris".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    try {
        new HttpPostRequestDecoder(req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertEquals("Invalid hex byte at index '0' in string: '%'", e.getMessage());
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 83 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleLo.

@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleLo() {
    byte[] bodyBytes = "foo=bar&a=b&empty=%2g&city=london".getBytes();
    ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
    content.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
    try {
        new HttpPostRequestDecoder(req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
        assertEquals("Invalid hex byte at index '0' in string: '%2g'", e.getMessage());
    } finally {
        assertTrue(req.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 84 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testMultipartRequest.

// https://github.com/netty/netty/issues/8575
@Test
public void testMultipartRequest() throws Exception {
    String BOUNDARY = "01f136d9282f";
    byte[] bodyBytes = ("--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg_id\"\n" + "\n" + "15200\n" + "--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg\"\n" + "\n" + "test message\n" + "--" + BOUNDARY + "--").getBytes();
    ByteBuf byteBuf = Unpooled.directBuffer(bodyBytes.length);
    byteBuf.writeBytes(bodyBytes);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "/up", byteBuf);
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + BOUNDARY);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE), req, CharsetUtil.UTF_8);
    assertTrue(decoder.isMultipart());
    assertFalse(decoder.getBodyHttpDatas().isEmpty());
    assertEquals(2, decoder.getBodyHttpDatas().size());
    Attribute attrMsg = (Attribute) decoder.getBodyHttpData("msg");
    assertTrue(attrMsg.getByteBuf().isDirect());
    assertEquals("test message", attrMsg.getValue());
    Attribute attrMsgId = (Attribute) decoder.getBodyHttpData("msg_id");
    assertTrue(attrMsgId.getByteBuf().isDirect());
    assertEquals("15200", attrMsgId.getValue());
    decoder.destroy();
    assertTrue(req.release());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 85 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testNotLeakWhenWrapIllegalArgumentException.

private static void testNotLeakWhenWrapIllegalArgumentException(ByteBuf buf) {
    buf.writeCharSequence("a=b&foo=%22bar%22&==", CharsetUtil.US_ASCII);
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", buf);
    try {
        new HttpPostStandardRequestDecoder(request).destroy();
    } finally {
        assertTrue(request.release());
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)287 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)180 Test (org.junit.jupiter.api.Test)74 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)69 Test (org.junit.Test)64 ByteBuf (io.netty.buffer.ByteBuf)54 HttpResponse (io.netty.handler.codec.http.HttpResponse)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 URI (java.net.URI)35 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)31 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)30 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)30 AsciiString (io.netty.util.AsciiString)25 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)23 Map (java.util.Map)22 ChannelPromise (io.netty.channel.ChannelPromise)21 HttpMethod (io.netty.handler.codec.http.HttpMethod)20 IOException (java.io.IOException)19 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)16