Search in sources :

Example 6 with HttpResponseHeaders

use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.

the class EmptyBodyTest method testEmptyBody.

@Test(groups = "standalone")
public void testEmptyBody() throws IOException {
    try (AsyncHttpClient ahc = asyncHttpClient()) {
        final AtomicBoolean err = new AtomicBoolean(false);
        final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        final AtomicBoolean status = new AtomicBoolean(false);
        final AtomicInteger headers = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(1);
        ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {

            public void onThrowable(Throwable t) {
                fail("Got throwable.", t);
                err.set(true);
            }

            public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
                byte[] bytes = e.getBodyPartBytes();
                if (bytes.length != 0) {
                    String s = new String(bytes);
                    logger.info("got part: {}", s);
                    logger.warn("Sampling stacktrace.", new Throwable("trace that, we should not get called for empty body."));
                    queue.put(s);
                }
                return State.CONTINUE;
            }

            public State onStatusReceived(HttpResponseStatus e) throws Exception {
                status.set(true);
                return AsyncHandler.State.CONTINUE;
            }

            public State onHeadersReceived(HttpResponseHeaders e) throws Exception {
                if (headers.incrementAndGet() == 2) {
                    throw new Exception("Analyze this.");
                }
                return State.CONTINUE;
            }

            public Object onCompleted() throws Exception {
                latch.countDown();
                return null;
            }
        });
        try {
            assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
        } catch (InterruptedException e) {
            fail("Interrupted.", e);
        }
        assertFalse(err.get());
        assertEquals(queue.size(), 0);
        assertTrue(status.get());
        assertEquals(headers.get(), 1);
    }
}
Also used : HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 7 with HttpResponseHeaders

use of org.asynchttpclient.HttpResponseHeaders 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 8 with HttpResponseHeaders

use of org.asynchttpclient.HttpResponseHeaders 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 9 with HttpResponseHeaders

use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.

the class NettyAsyncResponseTest method testCookieParseWeirdExpiresValue.

@Test(groups = "standalone")
public void testCookieParseWeirdExpiresValue() {
    final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org";
    HttpResponseHeaders responseHeaders = new HttpResponseHeaders(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);
    assertEquals(cookie.maxAge(), Long.MIN_VALUE);
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.testng.annotations.Test)

Example 10 with HttpResponseHeaders

use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.

the class NettyAsyncResponseTest method testCookieParseMaxAge.

@Test(groups = "standalone")
public void testCookieParseMaxAge() {
    final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org";
    HttpResponseHeaders responseHeaders = new HttpResponseHeaders(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);
    assertEquals(cookie.maxAge(), 60);
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.testng.annotations.Test)

Aggregations

HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)12 Test (org.testng.annotations.Test)9 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)6 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)4 HttpResponseBodyPart (org.asynchttpclient.HttpResponseBodyPart)4 HttpResponseStatus (org.asynchttpclient.HttpResponseStatus)4 Cookie (io.netty.handler.codec.http.cookie.Cookie)3 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)3 State (org.asynchttpclient.AsyncHandler.State)3 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)3 Response (org.asynchttpclient.Response)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AsyncHandler (org.asynchttpclient.AsyncHandler)2 BasicHttpsTest (org.asynchttpclient.BasicHttpsTest)2 NettyResponseStatus (org.asynchttpclient.netty.NettyResponseStatus)2