@@ -0,0 +1,3924 @@
+package com.proofpoint.http.client.testing;%0A%0Aimport com.google.common.base.Function;%0Aimport com.proofpoint.http.client.AsyncHttpClient.AsyncHttpResponseFuture;%0Aimport com.proofpoint.http.client.Request;%0Aimport com.proofpoint.http.client.Response;%0Aimport com.proofpoint.http.client.ResponseHandler;%0Aimport org.testng.annotations.Test;%0A%0Aimport java.net.URI;%0A%0Aimport static com.proofpoint.http.client.Request.Builder.prepareGet;%0Aimport static org.testng.Assert.assertEquals;%0Aimport static org.testng.Assert.assertSame;%0Aimport static org.testng.Assert.fail;%0A%0Apublic class TestTestingHttpClient%0A%7B%0A    @Test%0A    public void testExceptionFromProcessor()%0A            throws Exception%0A    %7B%0A        Request request = prepareGet()%0A                .setUri(URI.create(%22http://example.org%22))%0A                .build();%0A%0A        final RuntimeException expectedException = new RuntimeException(%22test exception%22);%0A%0A        AsyncHttpResponseFuture%3CString,CapturedException%3E future = new TestingHttpClient(%0A                new Function%3CRequest, Response%3E()%0A                %7B%0A                    @Override%0A                    public Response apply(Request input)%0A                    %7B%0A                        throw expectedException;%0A                    %7D%0A                %7D).executeAsync(request, new CaptureExceptionResponseHandler());%0A%0A        try %7B%0A            future.checkedGet();%0A            fail(%22expected exception%22);%0A        %7D%0A        catch (CapturedException e) %7B%0A            assertSame(e.getCause(), expectedException);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testExceptionFromProcessorWithDefaultingHandler()%0A            throws Exception%0A    %7B%0A        Request request = prepareGet()%0A                .setUri(URI.create(%22http://example.org%22))%0A                .build();%0A%0A        final RuntimeException testingException = new RuntimeException(%22test exception%22);%0A        final Object expectedResponse = new Object();%0A%0A        AsyncHttpResponseFuture%3CObject,RuntimeException%3E future = new TestingHttpClient(%0A                new Function%3CRequest, Response%3E()%0A                %7B%0A                    @Override%0A                    public Response apply(Request input)%0A                    %7B%0A                        throw testingException;%0A                    %7D%0A                %7D).executeAsync(request, new DefaultExceptionResponseHandler(testingException, expectedResponse));%0A%0A        assertSame(future.checkedGet(), expectedResponse);%0A    %7D%0A%0A    public static class CaptureExceptionResponseHandler implements ResponseHandler%3CString, CapturedException%3E%0A    %7B%0A        @Override%0A        public String handleException(Request request, Exception exception)%0A                throws CapturedException%0A        %7B%0A            throw new CapturedException(exception);%0A        %7D%0A%0A        @Override%0A        public String handle(Request request, Response response)%0A                throws CapturedException%0A        %7B%0A            throw new UnsupportedOperationException();%0A        %7D%0A%0A    %7D%0A%0A    protected static class CapturedException extends Exception%0A    %7B%0A        public CapturedException(Exception exception)%0A        %7B%0A            super(exception);%0A        %7D%0A    %7D%0A%0A    private class DefaultExceptionResponseHandler implements ResponseHandler%3CObject, RuntimeException%3E%0A    %7B%0A        private final RuntimeException expectedException;%0A        private final Object response;%0A%0A        public DefaultExceptionResponseHandler(RuntimeException expectedException, Object response)%0A        %7B%0A            this.expectedException = expectedException;%0A            this.response = response;%0A        %7D%0A%0A        @Override%0A        public Object handleException(Request request, Exception exception)%0A        %7B%0A            assertEquals(exception, expectedException);%0A            return response;%0A        %7D%0A%0A        @Override%0A        public Object handle(Request request, Response response)%0A        %7B%0A            throw new UnsupportedOperationException();%0A        %7D%0A    %7D%0A%7D%0A

Test exception handling behavior of TestingHttpClient