public void testCreateHostConfigurationProxy() throws Exception {
   StubProgressMonitor monitor = new StubProgressMonitor();
   HttpClient client = new HttpClient();
   WebUtil.createHostConfiguration(
       client,
       new WebLocation(
           TestUrl.DEFAULT.getHttpOk().toString(),
           null,
           null,
           new IProxyProvider() {
             public Proxy getProxyForHost(String host, String proxyType) {
               assertEquals(IProxyData.HTTP_PROXY_TYPE, proxyType);
               return null;
             }
           }),
       monitor);
   WebUtil.createHostConfiguration(
       client,
       new WebLocation(
           TestUrl.DEFAULT.getHttpsOk().toString(),
           null,
           null,
           new IProxyProvider() {
             public Proxy getProxyForHost(String host, String proxyType) {
               assertEquals(IProxyData.HTTPS_PROXY_TYPE, proxyType);
               return null;
             }
           }),
       monitor);
 }
  public void testExecuteCancelStalledConnect() throws Exception {
    final StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getConnectionTimeout().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      Runnable runner =
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(500);
              } catch (InterruptedException e) {
              }
              monitor.canceled = true;
            }
          };
      new Thread(runner).start();
      WebUtil.execute(client, hostConfiguration, method, monitor);
      client.executeMethod(method);
      fail("Expected OperationCanceledException");
    } catch (OperationCanceledException expected) {
      assertTrue(monitor.isCanceled());
    } catch (ConnectException ignored) {
      System.err.println("Skipping testExecuteCancelStalledConnect() due to blocking firewall");
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }
  public void testConfigureClient() throws Exception {
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());

    WebUtil.createHostConfiguration(client, location, null /*monitor*/);

    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    assertEquals(
        CoreUtil.TEST_MODE ? 2 : MAX_HTTP_HOST_CONNECTIONS_DEFAULT,
        params.getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION));
    assertEquals(
        CoreUtil.TEST_MODE ? 20 : MAX_HTTP_TOTAL_CONNECTIONS_DEFAULT,
        params.getMaxTotalConnections());
  }
 public void testGetTitleFromUrl() throws Exception {
   assertEquals(
       "Eclipse Mylyn Open Source Project",
       WebUtil.getTitleFromUrl(new WebLocation(TestUrl.DEFAULT.getHttpOk().toString()), null));
   // disabled: fails in environments where the DNS resolver redirects for unknown hosts
   //		try {
   //			String title = WebUtil.getTitleFromUrl(new WebLocation("http://invalidurl"), null);
   //			fail("Expected UnknownHostException, got: " + title);
   //		} catch (UnknownHostException e) {
   //		}
   String url = "http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
   testProxy.addResponse(TestProxy.OK);
   assertNull(WebUtil.getTitleFromUrl(new WebLocation(url), null));
 }
  public void testExecute() throws Exception {
    StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      int result = WebUtil.execute(client, hostConfiguration, method, monitor);
      assertEquals(HttpStatus.SC_OK, result);
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }
  public void testExecuteAlreadyCancelled() throws Exception {
    StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      monitor.canceled = true;
      WebUtil.execute(client, hostConfiguration, method, monitor);
      fail("Expected InterruptedIOException");
    } catch (OperationCanceledException expected) {
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }