public static void setProxyIfNeeded(final Properties properties) { String proxyHost = properties.getProperty(AzureConstants.PROXY_HOST); String proxyPort = properties.getProperty(AzureConstants.PROXY_PORT); if (!Strings.isNullOrEmpty(proxyHost) && Strings.isNullOrEmpty(proxyPort)) { int port = Integer.parseInt(proxyPort); SocketAddress proxyAddr = new InetSocketAddress(proxyHost, port); Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddr); OperationContext.setDefaultProxy(proxy); } }
@After public void genericTestMethodTearDown() { OperationContext.setDefaultProxy(Proxy.NO_PROXY); }
@Before public void genericTestMethodSetUp() { OperationContext.setDefaultProxy(Proxy.NO_PROXY); }
@NotNull public static CloudBlobClient getCloudBlobClient(@NotNull final StorageInputs inputs) throws Exception { final String storageConnectionString = String.format(STORAGE_AUTH_ENDPOINT, inputs.getStorageAccount(), inputs.getKey()); OperationContext.setDefaultProxy(getProxy(inputs.getProxyHost(), inputs.getProxyPort(), inputs.getProxyUsername(), inputs.getProxyPassword())); final CloudBlobClient client = CloudStorageAccount.parse(storageConnectionString).createCloudBlobClient(); if (inputs.getTimeout() != 0) { client.getDefaultRequestOptions().setTimeoutIntervalInMs(inputs.getTimeout()); } return client; } }
@Test public void testDefaultProxy() throws URISyntaxException, StorageException { CloudBlobClient blobClient = TestHelper.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference("container1"); // Use a default proxy OperationContext.setDefaultProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.1", 8888))); // Turn off retries to make the failure happen faster BlobRequestOptions opt = new BlobRequestOptions(); opt.setRetryPolicyFactory(new RetryNoRetry()); // Unfortunately HttpURLConnection doesn't expose a getter and the usingProxy method it does have doesn't // work as one would expect and will always for us return false. So, we validate by making sure the request // fails when we set a bad proxy rather than check the proxy setting itself succeeding. try { container.exists(null, opt, null); fail("Bad proxy should throw an exception."); } catch (StorageException e) { if (e.getCause().getClass() != ConnectException.class && e.getCause().getClass() != SocketTimeoutException.class && e.getCause().getClass() != SocketException.class) { Assert.fail("Unepected exception for bad proxy"); } } }
@Test public void testProxyOverridesDefault() throws URISyntaxException, StorageException { CloudBlobClient blobClient = TestHelper.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference("container1"); // Set a default proxy OperationContext.setDefaultProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.1", 8888))); // Turn off retries to make the failure happen faster BlobRequestOptions opt = new BlobRequestOptions(); opt.setRetryPolicyFactory(new RetryNoRetry()); // Unfortunately HttpURLConnection doesn't expose a getter and the usingProxy method it does have doesn't // work as one would expect and will always for us return false. So, we validate by making sure the request // fails when we set a bad proxy rather than check the proxy setting itself succeeding. try { container.exists(null, opt, null); fail("Bad proxy should throw an exception."); } catch (StorageException e) { if (e.getCause().getClass() != ConnectException.class && e.getCause().getClass() != SocketTimeoutException.class) { Assert.fail("Unepected exception for bad proxy"); } } // Override it with no proxy OperationContext opContext = new OperationContext(); opContext.setProxy(Proxy.NO_PROXY); // Should succeed as request-level proxy should override the bad default proxy container.exists(null, null, opContext); }