package com.ning.http.client.async;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;

/**
 * Tests to reproduce issues with handling of error responses
 *
 * @author Tatu Saloranta
 */
public class ErrorResponseTest
    extends AbstractBasicTest
{
    final static String BAD_REQUEST_STR = "Very Bad Request! No cookies.";
    
    private static class ErrorHandler extends AbstractHandler
    {
        public void handle(String s, Request r,
                HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            try { Thread.sleep(210L); } catch (InterruptedException e) { }
            response.setContentType("text/plain");
            response.setStatus(400);
            OutputStream out = response.getOutputStream();
            out.write(BAD_REQUEST_STR.getBytes("UTF-8"));
            out.flush();
        }
    }

    @Override
    public AbstractHandler configureHandler() throws Exception {
        return new ErrorHandler();
    }

    @Test(groups = "standalone")
    public void testQueryParameters() throws Exception
    {
        AsyncHttpClient client = new AsyncHttpClient();
        Future<Response> f = client
                .prepareGet("http://127.0.0.1:" + port1+"/foo")
                .addHeader("Accepts", "*/*")
                .execute();
        Response resp = f.get(3, TimeUnit.SECONDS);
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), 400);
        String respStr = resp.getResponseBody();
        assertEquals(BAD_REQUEST_STR, respStr);
    }
}

@@ -1,169 +1,876 @@
-package com.ning.http.client.async;%0A%0Aimport java.io.IOException;%0Aimport java.io.OutputStream;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUni
+/*%0A * Copyright 2010 Ning, Inc.%0A *%0A * Ning licenses this file to you under the Apache License, version 2.0%0A * (the %22License%22); you may not use this file except in compliance with the%0A * License.  You may obtain a copy of the License at:%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS, WITHOUT%0A * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the%0A * License for the specific language governing permissions and limitations%0A * under the License.%0A *%0A */%0Apackage com.ning.http.client.async;%0A%0Aimport com.ning.http.client.AsyncHttpClient;%0Aimport com.ning.http.client.Response;%0Aimport org.eclipse.jetty.server.Request;%0Aimport org.eclipse.jetty.server.handler.AbstractHandler;%0Aimport org.testng.annotations.Tes
 t;%0A%0A
@@ -1005,140 +1005,136 @@
 se;%0A
-%0A
 import 
-org.eclipse.jetty.server.Request;%0Aimport org.eclipse.jetty.server.handler.AbstractHandler;%0Aimport org.testng.annotations.Tes
+java.io.IOException;%0Aimport java.io.OutputStream;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUni
 t;%0A%0A
@@ -1231,92 +1231,8 @@
 l;%0A%0A
-import com.ning.http.client.AsyncHttpClient;%0Aimport com.ning.http.client.Response;%0A%0A
 /**%0A
@@ -1356,20 +1356,16 @@
 onseTest
-%0A   
  extends
@@ -1382,17 +1382,17 @@
 asicTest
-%0A
+ 
 %7B%0A    fi
@@ -1460,20 +1460,16 @@
 kies.%22;%0A
-    
 %0A    pri
@@ -1522,20 +1522,16 @@
 tHandler
-%0A   
  %7B%0A     
@@ -1589,16 +1589,27 @@
         
+       
+    
 HttpServ
@@ -1694,25 +1694,31 @@
 xception
-%0A
+ %7B%0A    
         
 %7B%0A      
@@ -1705,24 +1705,28 @@
             
+try 
 %7B%0A          
@@ -1727,21 +1727,19 @@
         
+ 
-try %7B
+  
  Thread.
@@ -1750,16 +1750,28 @@
 p(210L);
+%0A           
  %7D catch
@@ -1797,16 +1797,28 @@
 ion e) %7B
+%0A           
  %7D%0A     
@@ -2267,20 +2267,16 @@
 xception
-%0A   
  %7B%0A     
@@ -2417,17 +2417,19 @@
  + port1
-+
+ + 
 %22/foo%22)%0A

Add license, reformat