Search in sources :

Example 21 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class ZeroCopyFileTest method zeroCopyFileTest.

@Test
public void zeroCopyFileTest() throws IOException, ExecutionException, InterruptedException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpHeaders headers) {
                    return State.CONTINUE;
                }

                public Response onCompleted() {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) OutputStream(java.io.OutputStream) File(java.io.File) Test(org.testng.annotations.Test)

Example 22 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.

@Test
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, InterruptedException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    if (bodyPart.getBodyPartBytes().length == 0) {
                        return State.ABORT;
                    }
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpHeaders headers) {
                    return State.CONTINUE;
                }

                public Response onCompleted() {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) OutputStream(java.io.OutputStream) File(java.io.File) Test(org.testng.annotations.Test)

Example 23 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class InputStreamTest method testInvalidInputStream.

@Test
public void testInvalidInputStream() throws IOException, ExecutionException, InterruptedException {
    try (AsyncHttpClient c = asyncHttpClient()) {
        HttpHeaders h = new DefaultHttpHeaders().add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
        InputStream is = new InputStream() {

            int readAllowed;

            @Override
            public int available() {
                // Fake
                return 1;
            }

            @Override
            public int read() {
                int fakeCount = readAllowed++;
                if (fakeCount == 0) {
                    return (int) 'a';
                } else if (fakeCount == 1) {
                    return (int) 'b';
                } else if (fakeCount == 2) {
                    return (int) 'c';
                } else {
                    return -1;
                }
            }
        };
        Response resp = c.preparePost(getTargetUrl()).setHeaders(h).setBody(is).execute().get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader("X-Param"), "abc");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) InputStream(java.io.InputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 24 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class MultipartPartTest method transferToShouldWriteStringPart.

@Test
public void transferToShouldWriteStringPart() throws IOException, URISyntaxException {
    String text = FileUtils.readFileToString(TestUtils.resourceAsFile("test_sample_message.eml"), UTF_8);
    List<Part> parts = new ArrayList<>();
    parts.add(new StringPart("test_sample_message.eml", text));
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set("Cookie", "open-xchange-public-session-d41d8cd98f00b204e9800998ecf8427e=bfb98150b24f42bd844fc9ef2a9eaad5; open-xchange-secret-TSlq4Cm4nCBnDpBL1Px2A=9a49b76083e34c5ba2ef5c47362313fd; JSESSIONID=6883138728830405130.OX2");
    headers.set("Content-Length", "9241");
    headers.set("Content-Type", "multipart/form-data; boundary=5gigAKQyqDCVdlZ1fCkeLlEDDauTNoOOEhRnFg");
    headers.set("Host", "appsuite.qa.open-xchange.com");
    headers.set("Accept", "*/*");
    String boundary = "uwyqQolZaSmme019O2kFKvAeHoC14Npp";
    List<MultipartPart<? extends Part>> multipartParts = MultipartUtils.generateMultipartParts(parts, boundary.getBytes());
    try (MultipartBody multipartBody = new MultipartBody(multipartParts, "multipart/form-data; boundary=" + boundary, boundary.getBytes())) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(8 * 1024);
        multipartBody.transferTo(byteBuf);
        try {
            byteBuf.toString(StandardCharsets.UTF_8);
        } finally {
            byteBuf.release();
        }
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 25 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class NettyAsyncResponseTest method testCookieParseExpires.

@Test
public void testCookieParseExpires() {
    // e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = new Date(System.currentTimeMillis() + 60000);
    final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
    HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
    NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
    List<Cookie> cookies = response.getCookies();
    assertEquals(cookies.size(), 1);
    Cookie cookie = cookies.get(0);
    assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60);
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)286 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)149 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)83 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)73 Test (org.junit.Test)68 Test (org.junit.jupiter.api.Test)57 Test (org.testng.annotations.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)40 AsciiString (io.netty.util.AsciiString)29 ByteBuf (io.netty.buffer.ByteBuf)27 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)27 BStruct (org.ballerinalang.model.values.BStruct)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)22 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)20 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)19 Cookie (io.netty.handler.codec.http.cookie.Cookie)19 BValue (org.ballerinalang.model.values.BValue)19 ChannelPromise (io.netty.channel.ChannelPromise)18