Search in sources :

Example 96 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class TransferListenerTest method basicPutFileTest.

@Test(groups = "standalone")
public void basicPutFileTest() throws Exception {
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
    final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
    final AtomicLong bbSentLenght = new AtomicLong(0L);
    final AtomicBoolean completed = new AtomicBoolean(false);
    File file = createTempFile(1024 * 100 * 10);
    int timeout = (int) (file.length() / 1000);
    try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(file).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 97 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class TransferListenerTest method basicPutFileBodyGeneratorTest.

@Test(groups = "standalone")
public void basicPutFileBodyGeneratorTest() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicReference<Throwable> throwable = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
        final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
        final AtomicLong bbSentLenght = new AtomicLong(0L);
        final AtomicBoolean completed = new AtomicBoolean(false);
        File file = createTempFile(1024 * 100 * 10);
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(file)).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 98 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class ResumableAsyncHandlerTest method testOnHeadersReceivedContentLengthMinus.

@Test
public void testOnHeadersReceivedContentLengthMinus() throws Exception {
    ResumableAsyncHandler handler = new ResumableAsyncHandler();
    HttpHeaders responseHeaders = new DefaultHttpHeaders();
    responseHeaders.add(CONTENT_LENGTH, -1);
    HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
    State status = handler.onHeadersReceived(headers);
    assertEquals(status, AsyncHandler.State.ABORT, "State should be ABORT for content length -1");
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) State(org.asynchttpclient.AsyncHandler.State) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 99 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class ResumableAsyncHandlerTest method testOnHeadersReceivedWithDecoratedAsyncHandler.

@Test
public void testOnHeadersReceivedWithDecoratedAsyncHandler() throws Exception {
    HttpHeaders responseHeaders = new DefaultHttpHeaders();
    HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
    @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class);
    State mockState = mock(State.class);
    when(decoratedAsyncHandler.onHeadersReceived(headers)).thenReturn(mockState);
    ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler);
    State status = handler.onHeadersReceived(headers);
    assertEquals(status, mockState, "State should be equal to the state returned from decoratedAsyncHandler");
}
Also used : Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) State(org.asynchttpclient.AsyncHandler.State) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 100 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.

the class RetryNonBlockingIssue method testRetryNonBlocking.

/**
     * Tests that a head request can be made
     * 
     * @throws IOException
     * @throws ExecutionException
     * @throws InterruptedException
     */
@Test(groups = "standalone")
public void testRetryNonBlocking() throws IOException, InterruptedException, ExecutionException {
    AsyncHttpClientConfig config = //
    config().setKeepAlive(//
    true).setMaxConnections(//
    100).setConnectTimeout(//
    60000).setRequestTimeout(//
    30000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> res = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
        }
        StringBuilder b = new StringBuilder();
        for (ListenableFuture<Response> r : res) {
            Response theres = r.get();
            assertEquals(200, theres.getStatusCode());
            b.append("==============\r\n");
            b.append("Response Headers\r\n");
            HttpHeaders heads = theres.getHeaders();
            b.append(heads + "\r\n");
            b.append("==============\r\n");
        }
        System.out.println(b.toString());
        System.out.flush();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)114 Test (org.junit.Test)54 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)47 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)37 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)34 AsciiString (io.netty.util.AsciiString)27 Test (org.testng.annotations.Test)25 ByteBuf (io.netty.buffer.ByteBuf)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Cookie (io.netty.handler.codec.http.cookie.Cookie)12 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)10 Charset (java.nio.charset.Charset)9 List (java.util.List)9