Search in sources :

Example 71 with DefaultFullHttpRequest

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

the class HttpPostMultiPartRequestDecoderTest method testDecodeFullHttpRequestWithNoContentTypeHeader.

@Test
public void testDecodeFullHttpRequestWithNoContentTypeHeader() {
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    try {
        new HttpPostMultipartRequestDecoder(req);
        fail("Was expecting an ErrorDataDecoderException");
    } catch (HttpPostRequestDecoder.ErrorDataDecoderException expected) {
    // expected
    } 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) Test(org.junit.jupiter.api.Test)

Example 72 with DefaultFullHttpRequest

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

the class HttpPostRequestEncoderTest method testDataIsMultipleOfChunkSize2.

@Test
public void testDataIsMultipleOfChunkSize2() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    int length = 7943;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("foo", longText);
    assertNotNull(encoder.finalizeRequest());
    checkNextChunkSize(encoder, 8080);
    HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
    assertTrue(httpContent instanceof LastHttpContent, "Expected LastHttpContent is not received");
    httpContent.release();
    assertTrue(encoder.isEndOfInput(), "Expected end of input is not receive");
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Example 73 with DefaultFullHttpRequest

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

the class HttpPostRequestEncoderTest method testSingleFileUploadNoName.

@Test
public void testSingleFileUploadNoName() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    File file1 = new File(getClass().getResource("/file-01.txt").toURI());
    encoder.addBodyAttribute("foo", "bar");
    encoder.addBodyFileUpload("quux", "", file1, "text/plain", false);
    String multipartDataBoundary = encoder.multipartDataBoundary;
    String content = getRequestBody(encoder);
    String expected = "--" + multipartDataBoundary + "\r\n" + CONTENT_DISPOSITION + ": form-data; name=\"foo\"" + "\r\n" + CONTENT_LENGTH + ": 3" + "\r\n" + CONTENT_TYPE + ": text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + CONTENT_DISPOSITION + ": form-data; name=\"quux\"\r\n" + CONTENT_LENGTH + ": " + file1.length() + "\r\n" + CONTENT_TYPE + ": text/plain" + "\r\n" + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + "\r\n" + "File 01" + StringUtil.NEWLINE + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    assertEquals(expected, content);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 74 with DefaultFullHttpRequest

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

the class HttpPostRequestEncoderTest method testMultiFileUploadInMixedMode.

@Test
public void testMultiFileUploadInMixedMode() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    File file1 = new File(getClass().getResource("/file-01.txt").toURI());
    File file2 = new File(getClass().getResource("/file-02.txt").toURI());
    File file3 = new File(getClass().getResource("/file-03.txt").toURI());
    encoder.addBodyAttribute("foo", "bar");
    encoder.addBodyFileUpload("quux", file1, "text/plain", false);
    encoder.addBodyFileUpload("quux", file2, "text/plain", false);
    encoder.addBodyFileUpload("quux", file3, "text/plain", false);
    // We have to query the value of these two fields before finalizing
    // the request, which unsets one of them.
    String multipartDataBoundary = encoder.multipartDataBoundary;
    String multipartMixedBoundary = encoder.multipartMixedBoundary;
    String content = getRequestBody(encoder);
    String expected = "--" + multipartDataBoundary + "\r\n" + CONTENT_DISPOSITION + ": form-data; name=\"foo\"" + "\r\n" + CONTENT_LENGTH + ": 3" + "\r\n" + CONTENT_TYPE + ": text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + CONTENT_DISPOSITION + ": form-data; name=\"quux\"" + "\r\n" + CONTENT_TYPE + ": multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + CONTENT_DISPOSITION + ": attachment; filename=\"file-01.txt\"" + "\r\n" + CONTENT_LENGTH + ": " + file1.length() + "\r\n" + CONTENT_TYPE + ": text/plain" + "\r\n" + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + "\r\n" + "File 01" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + CONTENT_DISPOSITION + ": attachment; filename=\"file-02.txt\"" + "\r\n" + CONTENT_LENGTH + ": " + file2.length() + "\r\n" + CONTENT_TYPE + ": text/plain" + "\r\n" + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + "\r\n" + "File 02" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + CONTENT_DISPOSITION + ": attachment; filename=\"file-03.txt\"" + "\r\n" + CONTENT_LENGTH + ": " + file3.length() + "\r\n" + CONTENT_TYPE + ": text/plain" + "\r\n" + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + "\r\n" + "File 03" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    assertEquals(expected, content);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 75 with DefaultFullHttpRequest

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

the class HttpPostRequestEncoderTest method testHttpPostRequestEncoderSlicedBuffer.

@Test
public void testHttpPostRequestEncoderSlicedBuffer() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    // add Form attribute
    encoder.addBodyAttribute("getform", "POST");
    encoder.addBodyAttribute("info", "first value");
    encoder.addBodyAttribute("secondinfo", "secondvalue a&");
    encoder.addBodyAttribute("thirdinfo", "short text");
    int length = 100000;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("fourthinfo", longText.substring(0, 7470));
    File file1 = new File(getClass().getResource("/file-01.txt").toURI());
    encoder.addBodyFileUpload("myfile", file1, "application/x-zip-compressed", false);
    encoder.finalizeRequest();
    while (!encoder.isEndOfInput()) {
        HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
        ByteBuf content = httpContent.content();
        int refCnt = content.refCnt();
        assertTrue((content.unwrap() == content || content.unwrap() == null) && refCnt == 1 || content.unwrap() != content && refCnt == 2, "content: " + content + " content.unwrap(): " + content.unwrap() + " refCnt: " + refCnt);
        httpContent.release();
    }
    encoder.cleanFiles();
    encoder.close();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)215 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)117 Test (org.junit.jupiter.api.Test)72 Test (org.junit.Test)61 ByteBuf (io.netty.buffer.ByteBuf)56 HttpRequest (io.netty.handler.codec.http.HttpRequest)47 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)43 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)40 AsciiString (io.netty.util.AsciiString)30 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 Channel (io.netty.channel.Channel)20 ChannelPromise (io.netty.channel.ChannelPromise)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 IOException (java.io.IOException)17 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 URI (java.net.URI)15 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12