View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.http4;
18  
19  import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20  
21  import java.io.File;
22  import java.time.Duration;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.vfs2.AbstractProviderTestConfig;
26  import org.apache.commons.vfs2.FileNotFolderException;
27  import org.apache.commons.vfs2.FileObject;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileSystemManager;
30  import org.apache.commons.vfs2.FileSystemOptions;
31  import org.apache.commons.vfs2.ProviderTestSuite;
32  import org.apache.commons.vfs2.VFS;
33  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
34  import org.apache.commons.vfs2.util.NHttpFileServer;
35  import org.junit.Test;
36  import org.junit.jupiter.api.Assertions;
37  
38  /**
39   * Test cases for the HTTP4 provider.
40   */
41  public class Http4ProviderTestCase extends AbstractProviderTestConfig {
42  
43      private static NHttpFileServer server;
44  
45      private static final String TEST_URI = "test.http.uri";
46  
47      /**
48       * Use %40 for @ in URLs
49       */
50      private static String connectionUri;
51  
52      private static String getSystemTestUriOverride() {
53          return System.getProperty(TEST_URI);
54      }
55  
56      /**
57       * Creates and starts an embedded Apache HTTP Server (HttpComponents).
58       *
59       * @throws Exception
60       */
61      private static void setUpClass() throws Exception {
62          server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
63          final int socketPort = server.getPort();
64          connectionUri = getLocalHostUriString("http4", server.getPort());
65      }
66  
67      /**
68       * Creates a new test suite.
69       *
70       * @return a new test suite.
71       * @throws Exception Thrown when the suite cannot be constructed.
72       */
73      public static junit.framework.Test suite() throws Exception {
74          return new ProviderTestSuite(new Http4ProviderTestCase()) {
75              /**
76               * Adds base tests - excludes the nested test cases.
77               */
78              @Override
79              protected void addBaseTests() throws Exception {
80                  super.addBaseTests();
81  
82                  addTests(Http4ProviderTestCase.class);
83  
84                  // HttpAsyncServer returns 400 on link local requests from Httpclient
85                  // (e.g. Apache Web Server does the same https://bz.apache.org/bugzilla/show_bug.cgi?id=35122,
86                  // but not every HTTP server does).
87                  // Until this is addressed, local connection test won't work end-to-end
88  
89                  // if (getSystemTestUriOverride() == null) {
90                  //    addTests(IPv6LocalConnectionTests.class);
91                  // }
92              }
93  
94              @Override
95              protected void setUp() throws Exception {
96                  if (getSystemTestUriOverride() == null) {
97                      setUpClass();
98                  }
99                  super.setUp();
100             }
101 
102             @Override
103             protected void tearDown() throws Exception {
104                 tearDownClass();
105                 super.tearDown();
106             }
107         };
108     }
109 
110     /**
111      * Stops the embedded Apache HTTP Server.
112      * @throws InterruptedException
113      */
114     private static void tearDownClass() throws InterruptedException {
115         if (server != null) {
116             server.shutdown(5000, TimeUnit.SECONDS);
117         }
118     }
119 
120     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
121         Assertions.assertNotNull(file.getChildren());
122         Assertions.assertTrue(file.getChildren().length > 0);
123     }
124 
125     /**
126      * Returns the base folder for tests.
127      */
128     @Override
129     public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
130         String uri = getSystemTestUriOverride();
131         if (uri == null) {
132             uri = connectionUri;
133         }
134         return manager.resolveFile(uri);
135     }
136 
137     // Test no longer passing 2016/04/28
138     public void ignoreTestHttp405() throws FileSystemException {
139         @SuppressWarnings("resource") // getManager() returns a global.
140         final FileObject fileObject = VFS.getManager()
141                 .resolveFile("http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
142         Assertions.assertFalse(fileObject.getContent().isEmpty(), "Content should not be empty");
143     }
144 
145     /**
146      * Prepares the file system manager.
147      */
148     @Override
149     public void prepare(final DefaultFileSystemManager manager) throws Exception {
150         if (!manager.hasProvider("http4")) {
151             manager.addProvider("http4", new Http4FileProvider());
152         }
153     }
154 
155     /** Ensure VFS-453 options are present. */
156     @SuppressWarnings("deprecation")
157     @Test
158     public void testHttpTimeoutConfig() {
159         final FileSystemOptions opts = new FileSystemOptions();
160         final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
161 
162         // ensure defaults are 0
163         assertEquals(0, builder.getConnectionTimeout(opts));
164         assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
165         assertEquals(0, builder.getSoTimeout(opts));
166         assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
167         assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
168 
169         // Set int timeouts
170         builder.setConnectionTimeout(opts, 60000);
171         builder.setSoTimeout(opts, 60000);
172         builder.setUserAgent(opts, "foo/bar");
173 
174         // ensure changes are visible
175         assertEquals(60_000, builder.getConnectionTimeout(opts));
176         assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
177         assertEquals(60_000, builder.getSoTimeout(opts));
178         assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
179         assertEquals("foo/bar", builder.getUserAgent(opts));
180 
181         // Set Duration timeouts
182         builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
183         builder.setSoTimeout(opts, Duration.ofMinutes(1));
184         builder.setUserAgent(opts, "foo/bar");
185 
186         // ensure changes are visible
187         assertEquals(60_000, builder.getConnectionTimeout(opts));
188         assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
189         assertEquals(60_000, builder.getSoTimeout(opts));
190         assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
191         assertEquals("foo/bar", builder.getUserAgent(opts));
192     }
193 
194     private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
195         VFS.getManager().getFilesCache().close();
196         final FileSystemOptions opts = new FileSystemOptions();
197         Http4FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
198         @SuppressWarnings("resource") // getManager() returns a global.
199         final FileObject file = VFS.getManager().resolveFile(uri, opts);
200         try {
201             checkReadTestsFolder(file);
202         } catch (final FileNotFolderException e) {
203             // Expected: VFS HTTP does not support listing children yet.
204         }
205     }
206 
207     @Test
208     public void testResolveFolderSlashNoRedirectOff() throws FileSystemException {
209         testResolveFolderSlash(connectionUri + "/read-tests", false);
210     }
211 
212     @Test
213     public void testResolveFolderSlashNoRedirectOn() throws FileSystemException {
214         testResolveFolderSlash(connectionUri + "/read-tests", true);
215     }
216 
217     @Test
218     public void testResolveFolderSlashYesRedirectOff() throws FileSystemException {
219         testResolveFolderSlash(connectionUri + "/read-tests/", false);
220     }
221 
222     @Test
223     public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
224         testResolveFolderSlash(connectionUri + "/read-tests/", true);
225     }
226 
227     @Test
228     public void testResolveIPv6Url() throws FileSystemException {
229         final String ipv6Url = "http4://[fe80::1c42:dae:8370:aea6%en1]";
230 
231         @SuppressWarnings("rawtypes")
232         final Http4FileObject fileObject = (Http4FileObject)
233                 VFS.getManager().resolveFile(ipv6Url, new FileSystemOptions());
234 
235         assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getInternalURI().toString());
236     }
237 }