/**
   * Tests that if a socket fails to connect, the allocated connection is properly released back to
   * the connection manager.
   */
  @Test
  public void testSocketConnectFailureReleasesConnection() throws Exception {
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final ConnectionRequest connrequest = Mockito.mock(ConnectionRequest.class);
    Mockito.when(connrequest.get(Mockito.anyInt(), Mockito.any(TimeUnit.class))).thenReturn(conn);
    final HttpClientConnectionManager connmgr = Mockito.mock(HttpClientConnectionManager.class);
    Mockito.doThrow(new ConnectException())
        .when(connmgr)
        .connect(
            Mockito.any(HttpClientConnection.class),
            Mockito.any(HttpRoute.class),
            Mockito.anyInt(),
            Mockito.any(HttpContext.class));

    Mockito.when(connmgr.requestConnection(Mockito.any(HttpRoute.class), Mockito.any()))
        .thenReturn(connrequest);

    final HttpClient client = HttpClients.custom().setConnectionManager(connmgr).build();
    final HttpContext context = new BasicHttpContext();
    final HttpGet httpget = new HttpGet("http://www.example.com/a");

    try {
      client.execute(httpget, context);
      Assert.fail("expected IOException");
    } catch (final IOException expected) {
    }

    Mockito.verify(connmgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS);
  }
Example #2
0
  public void request2(String uri, int reqNum, int reqTerm) throws Exception {
    HttpClientContext context = HttpClientContext.create();
    HttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
    HttpRoute route = new HttpRoute(new HttpHost(uri, 80));
    // Request new connection. This can be a long process
    if (reqNum == 0) {
      System.out.println(
          String.format("request to %s infinite times with term '%d' ms", uri, reqTerm));
    } else {
      System.out.println(
          String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm));
    }
    int i = 0, tick = 0;

    while (true) {
      usleep(reqTerm);
      tick = (int) (Math.random() * 10) % 2;
      if (tick == 0) {
        continue;
      }
      System.out.println("111111111");
      ConnectionRequest connRequest = connManager.requestConnection(route, null);
      // Wait for connection up to 10 sec
      HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
      System.out.println("222222222");
      try {
        if (!conn.isOpen()) // If not open
        {
          System.out.println("333333333");
          // establish connection based on its route info
          connManager.connect(conn, route, 1000, context);
          // and mark it as route complete
          connManager.routeComplete(conn, route, context);
        }
        // Do useful things with the connection.
        System.out.println("request " + uri);
        System.out.println(
            "--> response status  = " + conn.receiveResponseHeader().getStatusLine());
      } finally {
        connManager.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
      }

      System.out.println("----------------------------------------");
      if (reqNum != 0 && reqNum < ++i) {
        break;
      }
    }
  }
  @Test(expected = RequestAbortedException.class)
  public void testExecConnectionRequestFailed() throws Exception {
    final HttpRoute route = new HttpRoute(target);
    final HttpClientContext context = new HttpClientContext();
    final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));

    Mockito.when(connRequest.get(Mockito.anyInt(), Mockito.<TimeUnit>any()))
        .thenThrow(new ExecutionException("Opppsie", null));
    mainClientExec.execute(route, request, context, execAware);
  }
  @Before
  public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    mainClientExec =
        new MainClientExec(
            requestExecutor,
            connManager,
            reuseStrategy,
            keepAliveStrategy,
            proxyHttpProcessor,
            targetAuthStrategy,
            proxyAuthStrategy,
            userTokenHandler);
    target = new HttpHost("foo", 80);
    proxy = new HttpHost("bar", 8888);

    Mockito.when(connManager.requestConnection(Mockito.<HttpRoute>any(), Mockito.any()))
        .thenReturn(connRequest);
    Mockito.when(connRequest.get(Mockito.anyLong(), Mockito.<TimeUnit>any()))
        .thenReturn(managedConn);
    final Map<String, Header> challenges = new HashMap<String, Header>();
    challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=test"));
    final AuthOption authOption =
        new AuthOption(new BasicScheme(), new UsernamePasswordCredentials("user:pass"));
    Mockito.when(
            targetAuthStrategy.getChallenges(
                Mockito.eq(target), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any()))
        .thenReturn(challenges);
    Mockito.when(
            targetAuthStrategy.getChallenges(
                Mockito.eq(target), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any()))
        .thenReturn(challenges);
    Mockito.when(
            targetAuthStrategy.select(
                Mockito.same(challenges),
                Mockito.eq(target),
                Mockito.<HttpResponse>any(),
                Mockito.<HttpClientContext>any()))
        .thenReturn(new LinkedList<AuthOption>(Arrays.asList(authOption)));
    Mockito.when(
            proxyAuthStrategy.getChallenges(
                Mockito.eq(proxy), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any()))
        .thenReturn(challenges);
    Mockito.when(
            proxyAuthStrategy.getChallenges(
                Mockito.eq(proxy), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any()))
        .thenReturn(challenges);
    Mockito.when(
            proxyAuthStrategy.select(
                Mockito.same(challenges),
                Mockito.eq(proxy),
                Mockito.<HttpResponse>any(),
                Mockito.<HttpClientContext>any()))
        .thenReturn(new LinkedList<AuthOption>(Arrays.asList(authOption)));
  }