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.http;
18  
19  import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20  
21  import java.io.File;
22  import java.time.Duration;
23  
24  import org.apache.commons.vfs2.AbstractProviderTestConfig;
25  import org.apache.commons.vfs2.FileNotFolderException;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSystemException;
28  import org.apache.commons.vfs2.FileSystemManager;
29  import org.apache.commons.vfs2.FileSystemOptions;
30  import org.apache.commons.vfs2.ProviderTestSuite;
31  import org.apache.commons.vfs2.VFS;
32  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
33  import org.apache.commons.vfs2.util.NHttpFileServer;
34  import org.junit.Test;
35  import org.junit.jupiter.api.Assertions;
36  
37  /**
38   * Test cases for the HTTP provider.
39   */
40  public class HttpProviderTestCase extends AbstractProviderTestConfig {
41  
42      private static final Duration ONE_MINUTE = Duration.ofMinutes(1);
43  
44      private static NHttpFileServer server;
45  
46      private static final String TEST_URI = "test.http.uri";
47  
48      /**
49       * Use %40 for @ in URLs
50       */
51      private static String connectionUri;
52  
53      private static String getSystemTestUriOverride() {
54          return System.getProperty(TEST_URI);
55      }
56  
57      /**
58       * Creates and starts an embedded Apache HTTP Server (HttpComponents).
59       *
60       * @throws Exception
61       */
62      private static void setUpClass() throws Exception {
63          server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
64          connectionUri = getLocalHostUriString("http", 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 HttpProviderTestCase()) {
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(HttpProviderTestCase.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      */
113     private static void tearDownClass() {
114         if (server != null) {
115             server.close();
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         try (FileObject fileObject = VFS.getManager()
139                 .resolveFile("http://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL")) {
140             Assertions.assertTrue(fileObject.getContent().getSize() > 0, "Content size should be > 0");
141             Assertions.assertFalse(fileObject.getContent().isEmpty(), "Content should not be empty");
142 
143         }
144     }
145 
146     /**
147      * Prepares the file system manager.
148      */
149     @Override
150     public void prepare(final DefaultFileSystemManager manager) throws Exception {
151         manager.addProvider("http", new HttpFileProvider());
152     }
153 
154     /** Ensure VFS-453 options are present. */
155     @Test
156     public void testHttpTimeoutConfig() {
157         final FileSystemOptions options = new FileSystemOptions();
158         final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
159 
160         // ensure defaults are 0
161         assertEquals(0, builder.getConnectionTimeout(options));
162         assertEquals(0, builder.getConnectionTimeoutDuration(options).toMillis());
163         assertEquals(0, builder.getSoTimeout(options));
164         assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(options));
165 
166         // Set with deprecated milliseconds APIs.
167         builder.setConnectionTimeout(options, 60000);
168         builder.setSoTimeout(options, 60000);
169         builder.setUserAgent(options, "foo/bar");
170 
171         // ensure changes are visible
172         assertEquals(60000, builder.getConnectionTimeout(options));
173         assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
174         assertEquals(60000, builder.getSoTimeout(options));
175         assertEquals("foo/bar", builder.getUserAgent(options));
176 
177         // Set with Duration APIs.
178         builder.setConnectionTimeout(options, ONE_MINUTE);
179         builder.setSoTimeout(options, ONE_MINUTE);
180 
181         // ensure changes are visible
182         assertEquals(60000, builder.getConnectionTimeout(options));
183         assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
184         assertEquals(60000, builder.getSoTimeout(options));
185         assertEquals(ONE_MINUTE, builder.getSoTimeoutDuration(options));
186         assertEquals("foo/bar", builder.getUserAgent(options));
187 
188         // TODO: should also check the created HTTPClient
189     }
190 
191     private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
192         VFS.getManager().getFilesCache().close();
193         final FileSystemOptions opts = new FileSystemOptions();
194         HttpFileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
195         try (FileObject file = VFS.getManager().resolveFile(uri, opts)) {
196             checkReadTestsFolder(file);
197         } catch (final FileNotFolderException e) {
198             // Expected: VFS HTTP does not support listing children yet.
199         }
200     }
201 
202     @Test
203     public void testResolveFolderSlashNoRedirectOff() throws FileSystemException {
204         testResolveFolderSlash(connectionUri + "/read-tests", false);
205     }
206 
207     @Test
208     public void testResolveFolderSlashNoRedirectOn() throws FileSystemException {
209         testResolveFolderSlash(connectionUri + "/read-tests", true);
210     }
211 
212     @Test
213     public void testResolveFolderSlashYesRedirectOff() throws FileSystemException {
214         testResolveFolderSlash(connectionUri + "/read-tests/", false);
215     }
216 
217     @Test
218     public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
219         testResolveFolderSlash(connectionUri + "/read-tests/", true);
220     }
221 
222     @Test
223     public void testResolveIPv6Url() throws FileSystemException {
224         final String ipv6Url = "http://[fe80::1c42:dae:8370:aea6%en1]/file.txt";
225 
226         @SuppressWarnings("rawtypes")
227         final FileObject fileObject = VFS.getManager().resolveFile(ipv6Url, new FileSystemOptions());
228 
229         assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getFileSystem().getRootURI());
230         assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/file.txt", fileObject.getName().getURI());
231     }
232 }