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.http5;
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 HTTP5 provider.
40   */
41  public class Http5ProviderTestCase 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          connectionUri = getLocalHostUriString("http5", server.getPort());
64      }
65  
66      /**
67       * Creates a new test suite.
68       *
69       * @return a new test suite.
70       * @throws Exception Thrown when the suite cannot be constructed.
71       */
72      public static junit.framework.Test suite() throws Exception {
73          return new ProviderTestSuite(new Http5ProviderTestCase()) {
74              /**
75               * Adds base tests - excludes the nested test cases.
76               */
77              @Override
78              protected void addBaseTests() throws Exception {
79                  super.addBaseTests();
80  
81                  addTests(Http5ProviderTestCase.class);
82  
83                  // HttpAsyncServer returns 400 on link local requests from Httpclient
84                  // (e.g. Apache Web Server does the same https://bz.apache.org/bugzilla/show_bug.cgi?id=35122,
85                  // but not every HTTP server does).
86                  // Until this is addressed, local connection test won't work end-to-end
87  
88                  // if (getSystemTestUriOverride() == null) {
89                  //    addTests(IPv6LocalConnectionTests.class);
90                  // }
91              }
92  
93              @Override
94              protected void setUp() throws Exception {
95                  if (getSystemTestUriOverride() == null) {
96                      setUpClass();
97                  }
98                  super.setUp();
99              }
100 
101             @Override
102             protected void tearDown() throws Exception {
103                 tearDownClass();
104                 super.tearDown();
105             }
106         };
107     }
108 
109     /**
110      * Stops the embedded Apache HTTP Server.
111      * @throws InterruptedException
112      */
113     private static void tearDownClass() throws InterruptedException {
114         if (server != null) {
115             server.shutdown(5000, TimeUnit.SECONDS);
116         }
117     }
118 
119     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
120         Assertions.assertNotNull(file.getChildren());
121         Assertions.assertTrue(file.getChildren().length > 0);
122     }
123 
124     /**
125      * Returns the base folder for tests.
126      */
127     @Override
128     public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
129         String uri = getSystemTestUriOverride();
130         if (uri == null) {
131             uri = connectionUri;
132         }
133         return manager.resolveFile(uri);
134     }
135 
136     // Test no longer passing 2016/04/28
137     public void ignoreTestHttp405() throws FileSystemException {
138         final FileObject fileObject = VFS.getManager()
139                 .resolveFile("http5://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
140         Assertions.assertFalse(fileObject.getContent().isEmpty(), "Content should not be empty");
141     }
142 
143     /**
144      * Prepares the file system manager.
145      */
146     @Override
147     public void prepare(final DefaultFileSystemManager manager) throws Exception {
148         if (!manager.hasProvider("http5")) {
149             manager.addProvider("http5", new Http5FileProvider());
150         }
151     }
152 
153     /** Ensure VFS-453 options are present. */
154     @SuppressWarnings("deprecation")
155     @Test
156     public void testHttpTimeoutConfig() {
157         final FileSystemOptions opts = new FileSystemOptions();
158         final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
159 
160         // ensure defaults are 0
161         assertEquals(0, builder.getConnectionTimeout(opts));
162         assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
163         assertEquals(0, builder.getSoTimeout(opts));
164         assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
165         assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
166 
167         // timeout as int
168         builder.setConnectionTimeout(opts, 60000);
169         builder.setSoTimeout(opts, 60000);
170         builder.setUserAgent(opts, "foo/bar");
171 
172         // ensure changes are visible
173         assertEquals(60000, builder.getConnectionTimeout(opts));
174         assertEquals(60000, builder.getSoTimeout(opts));
175         assertEquals("foo/bar", builder.getUserAgent(opts));
176 
177         // timeout as Duration
178         builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
179         builder.setSoTimeout(opts, Duration.ofMinutes(1));
180         builder.setUserAgent(opts, "foo/bar");
181 
182         // ensure changes are visible
183         assertEquals(60000, builder.getConnectionTimeoutDuration(opts).toMillis());
184         assertEquals(60000, builder.getSoTimeoutDuration(opts).toMillis());
185         assertEquals("foo/bar", builder.getUserAgent(opts));
186     }
187 
188     private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
189         VFS.getManager().getFilesCache().close();
190         final FileSystemOptions opts = new FileSystemOptions();
191         Http5FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
192         final FileObject file = VFS.getManager().resolveFile(uri, opts);
193         try {
194             checkReadTestsFolder(file);
195         } catch (final FileNotFolderException e) {
196             // Expected: VFS HTTP does not support listing children yet.
197         }
198     }
199 
200     @Test
201     public void testResolveFolderSlashNoRedirectOff() throws FileSystemException {
202         testResolveFolderSlash(connectionUri + "/read-tests", false);
203     }
204 
205     @Test
206     public void testResolveFolderSlashNoRedirectOn() throws FileSystemException {
207         testResolveFolderSlash(connectionUri + "/read-tests", true);
208     }
209 
210     @Test
211     public void testResolveFolderSlashYesRedirectOff() throws FileSystemException {
212         testResolveFolderSlash(connectionUri + "/read-tests/", false);
213     }
214 
215     @Test
216     public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
217         testResolveFolderSlash(connectionUri + "/read-tests/", true);
218     }
219 
220     @Test
221     public void testResolveIPv6Url() throws FileSystemException {
222         final String ipv6Url = "http5://[fe80::1c42:dae:8370:aea6%en1]";
223 
224         @SuppressWarnings("rawtypes")
225         final Http5FileObject fileObject = (Http5FileObject)
226                 VFS.getManager().resolveFile(ipv6Url, new FileSystemOptions());
227 
228         assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getInternalURI().toString());
229     }
230 }