This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git

commit 5339baf932d64959ffed01ea0577109b4490825c
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Feb 5 16:33:23 2021 -0500

    [VFS-782] Pass correct proxy authentication credentials #117.
    
    Add tests from https://github.com/apache/commons-vfs/pull/117 by
    satish-csi.
---
 .../provider/http4/Http4GetContentInfoTest.java    | 83 +++++++++++++++++-----
 .../provider/http5/Http5GetContentInfoTest.java    | 81 ++++++++++++++++-----
 2 files changed, 131 insertions(+), 33 deletions(-)

diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4GetContentInfoTest.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4GetContentInfoTest.java
index c20f7dc..1fe6411 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4GetContentInfoTest.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4GetContentInfoTest.java
@@ -25,6 +25,7 @@ import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemManager;
 import org.apache.commons.vfs2.FileSystemOptions;
 import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -35,33 +36,46 @@ import junit.framework.TestCase;
  */
 public class Http4GetContentInfoTest extends TestCase {
 
-    /**
-     * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo().
-     *
-     * @throws FileSystemException thrown when the getContentInfo API fails.
-     */
-    @Test
-    public void testGetContentInfo() throws FileSystemException, 
MalformedURLException {
-        @SuppressWarnings("resource") // getManager() returns a global.
-        final FileSystemManager fsManager = VFS.getManager();
-        final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
-        try (final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxy());
-            final FileContent content = fo.getContent()) {
-            Assert.assertNotNull(content);
-            // Used to NPE before fix:
-            content.getContentInfo();
+    FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
+        // get proxy host and port from env var "https_proxy"
+        String proxyHost = null;
+        int proxyPort = -1;
+        final String proxyUrl = System.getenv("https_proxy");
+        if (proxyUrl != null) {
+            final URL url = new URL(proxyUrl);
+            proxyHost = url.getHost();
+            proxyPort = url.getPort();
         }
+
+        // return null if proxy host or port invalid
+        if (proxyHost == null || proxyPort == -1) {
+            return null;
+        }
+
+        // return options with proxy
+        final Http4FileSystemConfigBuilder builder = 
Http4FileSystemConfigBuilder.getInstance();
+        final FileSystemOptions opts = new FileSystemOptions();
+        builder.setProxyHost(opts, proxyHost);
+        builder.setProxyPort(opts, proxyPort);
+        builder.setProxyScheme(opts, "http");
+        return opts;
     }
 
-    FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
+    private FileSystemOptions getOptionsWithProxyAuthentication() throws 
MalformedURLException {
         // get proxy host and port from env var "https_proxy"
         String proxyHost = null;
         int proxyPort = -1;
+        String user[] = null;
         final String proxyUrl = System.getenv("https_proxy");
         if (proxyUrl != null) {
             final URL url = new URL(proxyUrl);
             proxyHost = url.getHost();
             proxyPort = url.getPort();
+            final String userInfo = url.getUserInfo();
+            if (userInfo != null) {
+                user = userInfo.split(":");
+
+            }
         }
 
         // return null if proxy host or port invalid
@@ -74,7 +88,42 @@ public class Http4GetContentInfoTest extends TestCase {
         final FileSystemOptions opts = new FileSystemOptions();
         builder.setProxyHost(opts, proxyHost);
         builder.setProxyPort(opts, proxyPort);
-        builder.setProxyScheme(opts, "http");
+        if (user != null) {
+            builder.setProxyAuthenticator(opts, new 
StaticUserAuthenticator(null, user[0], user[1]));
+        }
         return opts;
     }
+
+    /**
+     * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo().
+     *
+     * @throws FileSystemException thrown when the getContentInfo API fails.
+     */
+    @Test
+    public void testGetContentInfo() throws FileSystemException, 
MalformedURLException {
+        @SuppressWarnings("resource") // getManager() returns a global.
+        final FileSystemManager fsManager = VFS.getManager();
+        final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
+        try (final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxy());
+            final FileContent content = fo.getContent()) {
+            Assert.assertNotNull(content);
+            // Used to NPE before fix:
+            content.getContentInfo();
+        }
+    }
+
+    /**
+     * Tests VFS-782 pass correct proxy authentication credentials.
+     *
+     * @throws FileSystemException thrown when the authentication fails.
+     */
+    @Test
+    public void testGetContentWithProxyAuthInfo() throws FileSystemException, 
MalformedURLException {
+        final FileSystemManager fsManager = VFS.getManager();
+        final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
+        final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxyAuthentication());
+        final FileContent content = fo.getContent();
+        Assert.assertNotNull(content);
+        content.getContentInfo();
+    }
 }
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/Http5GetContentInfoTest.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/Http5GetContentInfoTest.java
index 7ea3a2e..ff560a7 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/Http5GetContentInfoTest.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/Http5GetContentInfoTest.java
@@ -25,6 +25,7 @@ import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemManager;
 import org.apache.commons.vfs2.FileSystemOptions;
 import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -35,22 +36,6 @@ import junit.framework.TestCase;
  */
 public class Http5GetContentInfoTest extends TestCase {
 
-    /**
-     * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
-     *
-     * @throws FileSystemException thrown when the getContentInfo API fails.
-     */
-    @Test
-    public void testGetContentInfo() throws FileSystemException, 
MalformedURLException {
-        final FileSystemManager fsManager = VFS.getManager();
-        final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt";
-        final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxy());
-        final FileContent content = fo.getContent();
-        Assert.assertNotNull(content);
-        // Used to NPE before fix:
-        content.getContentInfo();
-    }
-
     FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
         // get proxy host and port from env var "https_proxy"
         String proxyHost = null;
@@ -75,4 +60,68 @@ public class Http5GetContentInfoTest extends TestCase {
         builder.setProxyScheme(opts, "http");
         return opts;
     }
+
+    private FileSystemOptions getOptionsWithProxyAuthentication() throws 
MalformedURLException {
+        // get proxy host and port from env var "https_proxy"
+        String proxyHost = null;
+        int proxyPort = -1;
+        String user[] = null;
+        final String proxyUrl = System.getenv("https_proxy");
+        if (proxyUrl != null) {
+            final URL url = new URL(proxyUrl);
+            proxyHost = url.getHost();
+            proxyPort = url.getPort();
+            final String userInfo = url.getUserInfo();
+            if (userInfo != null) {
+                user = userInfo.split(":");
+
+            }
+        }
+
+        // return null if proxy host or port invalid
+        if (proxyHost == null || proxyPort == -1) {
+            return null;
+        }
+
+        // return options with proxy
+        final Http5FileSystemConfigBuilder builder = 
Http5FileSystemConfigBuilder.getInstance();
+        final FileSystemOptions opts = new FileSystemOptions();
+        builder.setProxyHost(opts, proxyHost);
+        builder.setProxyPort(opts, proxyPort);
+        if (user != null) {
+            builder.setProxyAuthenticator(opts, new 
StaticUserAuthenticator(null, user[0], user[1]));
+        }
+        return opts;
+    }
+
+    /**
+     * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
+     *
+     * @throws FileSystemException thrown when the getContentInfo API fails.
+     */
+    @Test
+    public void testGetContentInfo() throws FileSystemException, 
MalformedURLException {
+        final FileSystemManager fsManager = VFS.getManager();
+        final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt";
+        final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxy());
+        final FileContent content = fo.getContent();
+        Assert.assertNotNull(content);
+        // Used to NPE before fix:
+        content.getContentInfo();
+    }
+
+    /**
+     * Tests VFS-782 pass correct proxy authentication credentials.
+     *
+     * @throws FileSystemException thrown when the authentication fails.
+     */
+    @Test
+    public void testGetContentWithProxyAuthInfo() throws FileSystemException, 
MalformedURLException {
+        final FileSystemManager fsManager = VFS.getManager();
+        final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
+        final FileObject fo = fsManager.resolveFile(uri, 
getOptionsWithProxyAuthentication());
+        final FileContent content = fo.getContent();
+        Assert.assertNotNull(content);
+        content.getContentInfo();
+    }
 }

Reply via email to