@@ -0,0 +1,3812 @@
+package com.ning.http.client.async;%0A%0Aimport com.ning.http.client.AsyncHandler;%0Aimport com.ning.http.client.AsyncHttpClient;%0Aimport com.ning.http.client.HttpResponseBodyPart;%0Aimport com.ning.http.client.HttpResponseHeaders;%0Aimport com.ning.http.client.HttpResponseStatus;%0Aimport org.eclipse.jetty.server.Request;%0Aimport org.eclipse.jetty.server.handler.AbstractHandler;%0Aimport org.testng.annotations.Test;%0A%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.io.IOException;%0Aimport java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.LinkedBlockingQueue;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0Aimport static org.testng.Assert.assertEquals;%0Aimport static org.testng.Assert.assertFalse;%0Aimport static org.testng.Assert.assertTrue;%0Aimport static org.testng.Assert.fail;%0A%0A/**%0A * Tests case where response doesn't have body.%0A *%0A * @author Hubert Iwaniuk%0A */%0Apublic class EmptyBodyTest extends AbstractBasicTest %7B%0A    private class NoBodyResponseHandler extends AbstractHandler %7B%0A        public void handle(%0A                String s,%0A                Request request,%0A                HttpServletRequest req,%0A                HttpServletResponse resp)%0A                throws IOException, ServletException %7B%0A            resp.setStatus(HttpServletResponse.SC_OK);%0A            request.setHandled(true);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public AbstractHandler configureHandler() throws Exception %7B%0A        return new NoBodyResponseHandler();%0A    %7D%0A%0A    @Test(groups = %22standalone%22)%0A    public void testEmptyBody() throws IOException %7B%0A        AsyncHttpClient ahc = new AsyncHttpClient();%0A        final AtomicBoolean err = new AtomicBoolean(false);%0A        final LinkedBlockingQueue%3CString%3E queue = new LinkedBlockingQueue%3CString%3E();%0A        final AtomicBoolean status = new AtomicBoolean(false);%0A        final AtomicInteger headers = new AtomicInteger(0);%0A        final CountDownLatch latch = new CountDownLatch(1);%0A        ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler%3CObject%3E() %7B%0A            public void onThrowable(Throwable t) %7B%0A                fail(%22Got throwable.%22, t);%0A                err.set(true);%0A            %7D%0A%0A            public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception %7B%0A                String s = new String(e.getBodyPartBytes());%0A                log.info(%22got part: %22 + s);%0A                if (s.isEmpty()) %7B%0A                    //noinspection ThrowableInstanceNeverThrown%0A                    log.warn(%22Sampling stacktrace.%22,%0A                            new Throwable(%22trace that, we should not get called for empty body.%22));%0A                %7D%0A                queue.put(s);%0A                return STATE.CONTINUE;%0A            %7D%0A%0A            public STATE onStatusReceived(HttpResponseStatus e) throws Exception %7B%0A                status.set(true);%0A                return STATE.CONTINUE;%0A            %7D%0A%0A            public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception %7B%0A                if (headers.incrementAndGet() == 2) %7B%0A                    throw new Exception(%22Analyze this.%22);%0A                %7D%0A                return STATE.CONTINUE;%0A            %7D%0A%0A            public Object onCompleted() throws Exception %7B%0A                latch.countDown();%0A                return null;%0A            %7D%0A        %7D);%0A        try %7B%0A            assertTrue(latch.await(1, TimeUnit.SECONDS), %22Latch failed.%22);%0A        %7D catch (InterruptedException e) %7B%0A            fail(%22Interrupted.%22, e);%0A        %7D%0A        assertFalse(err.get());%0A        assertEquals(queue.size(), 0);%0A        assertTrue(status.get());%0A        assertEquals(headers.get(), 1);%0A    %7D%0A%7D%0A

Test exposing calls to onBodyReceived when there is no body.