@Test
  public void asyncStream302RedirectWithBody() throws Throwable {

    withClient(config().setFollowRedirect(true))
        .run(
            client -> {
              withServer(server)
                  .run(
                      server -> {
                        String originalUrl = server.getHttpUrl() + "/original";
                        String redirectUrl = server.getHttpUrl() + "/redirect";

                        server.enqueueResponse(
                            response -> {
                              response.setStatus(302);
                              response.setHeader(LOCATION.toString(), redirectUrl);
                              response
                                  .getOutputStream()
                                  .println("You are being asked to redirect to " + redirectUrl);
                            });
                        server.enqueueOk();

                        Response response =
                            client.prepareGet(originalUrl).execute().get(20, TimeUnit.SECONDS);

                        assertEquals(response.getStatusCode(), 200);
                        assertTrue(response.getResponseBody().isEmpty());
                      });
            });
  }
 @Test(groups = "online")
 public void testGoogleComWithTimeout() throws Exception {
   try (AsyncHttpClient c = asyncHttpClient(config().setRequestTimeout(10000))) {
     Response response = c.prepareGet("http://google.com/").execute().get(10, TimeUnit.SECONDS);
     assertNotNull(response);
     assertTrue(response.getStatusCode() == 301 || response.getStatusCode() == 302);
   }
 }
 @Test(groups = "online", enabled = false)
 // FIXME
 public void testMicrosoftCom() throws Exception {
   try (AsyncHttpClient c = asyncHttpClient(config().setRequestTimeout(10000))) {
     Response response = c.prepareGet("http://microsoft.com/").execute().get(10, TimeUnit.SECONDS);
     assertNotNull(response);
     assertEquals(response.getStatusCode(), 301);
   }
 }
 @Test(groups = "online")
 public void testMailGoogleCom() throws Exception {
   try (AsyncHttpClient c = asyncHttpClient(config().setRequestTimeout(10000))) {
     Response response =
         c.prepareGet("http://mail.google.com/").execute().get(10, TimeUnit.SECONDS);
     assertNotNull(response);
     assertEquals(response.getStatusCode(), 200);
   }
 }
 // FIXME Get a 302 in France...
 @Test(groups = "online", enabled = false)
 public void testUrlRequestParametersEncoding() throws Exception {
   try (AsyncHttpClient client = asyncHttpClient()) {
     String requestUrl2 = URL + URLEncoder.encode(REQUEST_PARAM, UTF_8.name());
     logger.info(String.format("Executing request [%s] ...", requestUrl2));
     Response response = client.prepareGet(requestUrl2).execute().get();
     assertEquals(response.getStatusCode(), 302);
   }
 }
  @Test(groups = "online")
  public void stripQueryStringTest() throws Exception {

    try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
      Response response = c.prepareGet("http://www.freakonomics.com/?p=55846").execute().get();

      assertNotNull(response);
      assertEquals(response.getStatusCode(), 200);
    }
  }
  @Test(groups = "online")
  public void asyncFullBodyProperlyRead() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient()) {
      Response r = client.prepareGet("http://www.cyberpresse.ca/").execute().get();

      InputStream stream = r.getResponseBodyAsStream();
      int contentLength = Integer.valueOf(r.getHeader("Content-Length"));

      assertEquals(contentLength, IOUtils.toByteArray(stream).length);
    }
  }
  @Test
  public void closeConnectionTest() throws Throwable {

    withClient()
        .run(
            client -> {
              withServer(server)
                  .run(
                      server -> {
                        server.enqueueEcho();

                        Response r =
                            client
                                .prepareGet(getTargetUrl())
                                .execute(
                                    new AsyncHandler<Response>() {

                                      private Response.ResponseBuilder builder =
                                          new Response.ResponseBuilder();

                                      public State onHeadersReceived(HttpResponseHeaders content)
                                          throws Exception {
                                        builder.accumulate(content);
                                        return State.CONTINUE;
                                      }

                                      public void onThrowable(Throwable t) {}

                                      public State onBodyPartReceived(HttpResponseBodyPart content)
                                          throws Exception {
                                        builder.accumulate(content);
                                        return content.isLast() ? State.ABORT : State.CONTINUE;
                                      }

                                      public State onStatusReceived(
                                          HttpResponseStatus responseStatus) throws Exception {
                                        builder.accumulate(responseStatus);

                                        return State.CONTINUE;
                                      }

                                      public Response onCompleted() throws Exception {
                                        return builder.build();
                                      }
                                    })
                                .get();

                        assertNotNull(r);
                        assertEquals(r.getStatusCode(), 200);
                      });
            });
  }
 // @Test(groups = "online")
 public void notRedirected302Test() throws Exception {
   isSet.getAndSet(false);
   try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
     Response response =
         c.prepareGet(getTargetUrl())
             .setFollowRedirect(false)
             .setHeader("X-redirect", "http://www.microsoft.com/")
             .execute()
             .get();
     assertNotNull(response);
     assertEquals(response.getStatusCode(), 302);
   }
 }
 @Test(groups = "standalone")
 public void testQueryParameters() throws Exception {
   try (AsyncHttpClient client = 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);
     assertEquals(resp.getResponseBody(), BAD_REQUEST_STR);
   }
 }
  // @Test(groups = "standalone")
  public void relativeLocationUrl() throws Exception {
    isSet.getAndSet(false);

    try (AsyncHttpClient c = asyncHttpClient()) {
      Response response =
          c.preparePost(getTargetUrl())
              .setFollowRedirect(true)
              .setHeader("X-redirect", "/foo/test")
              .execute()
              .get();
      assertNotNull(response);
      assertEquals(response.getStatusCode(), 200);
      assertEquals(response.getUri().toString(), getTargetUrl());
    }
  }
  @Test(groups = "standalone")
  public void closeConnectionTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
      Response r =
          c.prepareGet(getTargetUrl())
              .execute(
                  new AsyncHandler<Response>() {

                    private Response.ResponseBuilder builder = new Response.ResponseBuilder();

                    public State onHeadersReceived(HttpResponseHeaders content) throws Exception {
                      builder.accumulate(content);
                      return State.CONTINUE;
                    }

                    public void onThrowable(Throwable t) {}

                    public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
                      builder.accumulate(content);

                      if (content.isLast()) {
                        content.markUnderlyingConnectionAsToBeClosed();
                      }
                      return State.CONTINUE;
                    }

                    public State onStatusReceived(HttpResponseStatus responseStatus)
                        throws Exception {
                      builder.accumulate(responseStatus);

                      return State.CONTINUE;
                    }

                    public Response onCompleted() throws Exception {
                      return builder.build();
                    }
                  })
              .get();

      assertNotNull(r);
      assertEquals(r.getStatusCode(), 200);
    }
  }
  @Test(groups = "online", enabled = false)
  public void testAHC62Com() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
      Response response =
          c.prepareGet("http://api.crunchbase.com/v/1/financial-organization/kinsey-hills-group.js")
              .execute(
                  new AsyncHandler<Response>() {

                    private Response.ResponseBuilder builder = new Response.ResponseBuilder();

                    public void onThrowable(Throwable t) {
                      t.printStackTrace();
                    }

                    public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
                        throws Exception {
                      System.out.println(bodyPart.getBodyPartBytes().length);
                      builder.accumulate(bodyPart);

                      return State.CONTINUE;
                    }

                    public State onStatusReceived(HttpResponseStatus responseStatus)
                        throws Exception {
                      builder.accumulate(responseStatus);
                      return State.CONTINUE;
                    }

                    public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                      builder.accumulate(headers);
                      return State.CONTINUE;
                    }

                    public Response onCompleted() throws Exception {
                      return builder.build();
                    }
                  })
              .get(10, TimeUnit.SECONDS);
      assertNotNull(response);
      assertTrue(response.getResponseBody().length() >= 3870);
    }
  }
  // @Test(groups = "online")
  public void redirected302Test() throws Exception {
    isSet.getAndSet(false);
    try (AsyncHttpClient c = asyncHttpClient()) {
      Response response =
          c.prepareGet(getTargetUrl())
              .setFollowRedirect(true)
              .setHeader("X-redirect", "http://www.microsoft.com/")
              .execute()
              .get();

      assertNotNull(response);
      assertEquals(response.getStatusCode(), 200);

      String anyMicrosoftPage = "http://www.microsoft.com[^:]*:80";
      String baseUrl = getBaseUrl(response.getUri());

      assertTrue(
          baseUrl.matches(anyMicrosoftPage),
          "response does not show redirection to " + anyMicrosoftPage);
    }
  }
  @Test(groups = "online")
  public void evilCoookieTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
      RequestBuilder builder =
          get("http://localhost") //
              .setFollowRedirect(true) //
              .setUrl("http://www.google.com/") //
              .addHeader("Content-Type", "text/plain") //
              .addCookie(
                  new Cookie(
                      "evilcookie",
                      "test",
                      false,
                      ".google.com",
                      "/",
                      Long.MIN_VALUE,
                      false,
                      false));

      Response response = c.executeRequest(builder.build()).get();
      assertNotNull(response);
      assertEquals(response.getStatusCode(), 200);
    }
  }