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 java.io.File;
20  import java.time.Duration;
21  
22  import org.apache.commons.vfs2.AbstractProviderTestConfig;
23  import org.apache.commons.vfs2.FileNotFolderException;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.FileSystemManager;
27  import org.apache.commons.vfs2.FileSystemOptions;
28  import org.apache.commons.vfs2.ProviderTestSuite;
29  import org.apache.commons.vfs2.VFS;
30  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
31  import org.apache.commons.vfs2.util.NHttpFileServer;
32  import org.junit.Assert;
33  
34  import junit.framework.Test;
35  
36  /**
37   * Test cases for the HTTP provider.
38   *
39   */
40  public class HttpProviderTestCase extends AbstractProviderTestConfig {
41      private static final Duration ONE_MINUTE = Duration.ofMinutes(1);
42  
43      private static NHttpFileServer Server;
44  
45      private static int SocketPort;
46  
47      private static final String TEST_URI = "test.http.uri";
48  
49      /**
50       * Use %40 for @ in URLs
51       */
52      private static String ConnectionUri;
53  
54      private static String getSystemTestUriOverride() {
55          return System.getProperty(TEST_URI);
56      }
57  
58      /**
59       * Creates and starts an embedded Apache HTTP Server (HttpComponents).
60       *
61       * @throws Exception
62       */
63      private static void setUpClass() throws Exception {
64          Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
65          SocketPort = Server.getPort();
66          ConnectionUri = "http://localhost:" + SocketPort;
67      }
68  
69      /**
70       * Creates a new test suite.
71       *
72       * @return a new test suite.
73       * @throws Exception Thrown when the suite cannot be constructed.
74       */
75      public static Test suite() throws Exception {
76          return new ProviderTestSuite(new HttpProviderTestCase()) {
77              /**
78               * Adds base tests - excludes the nested test cases.
79               */
80              @Override
81              protected void addBaseTests() throws Exception {
82                  super.addBaseTests();
83                  addTests(HttpProviderTestCase.class);
84              }
85  
86              @Override
87              protected void setUp() throws Exception {
88                  if (getSystemTestUriOverride() == null) {
89                      setUpClass();
90                  }
91                  super.setUp();
92              }
93  
94              @Override
95              protected void tearDown() throws Exception {
96                  tearDownClass();
97                  super.tearDown();
98              }
99          };
100     }
101 
102     /**
103      * Stops the embedded Apache HTTP Server.
104      */
105     private static void tearDownClass() {
106         if (Server != null) {
107             Server.close();
108         }
109     }
110 
111     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
112         Assert.assertNotNull(file.getChildren());
113         Assert.assertTrue(file.getChildren().length > 0);
114     }
115 
116     /**
117      * Returns the base folder for tests.
118      */
119     @Override
120     public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
121         String uri = getSystemTestUriOverride();
122         if (uri == null) {
123             uri = ConnectionUri;
124         }
125         return manager.resolveFile(uri);
126     }
127 
128     // Test no longer passing 2016/04/28
129     public void ignoreTestHttp405() throws FileSystemException {
130         try (final FileObject fileObject = VFS.getManager()
131                 .resolveFile("http://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL")) {
132             assert fileObject.getContent().getSize() > 0;
133             assert !fileObject.getContent().isEmpty();
134 
135         }
136     }
137 
138     /**
139      * Prepares the file system manager.
140      */
141     @Override
142     public void prepare(final DefaultFileSystemManager manager) throws Exception {
143         manager.addProvider("http", new HttpFileProvider());
144     }
145 
146     /** Ensure VFS-453 options are present. */
147     public void testHttpTimeoutConfig() {
148         final FileSystemOptions options = new FileSystemOptions();
149         final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
150 
151         // ensure defaults are 0
152         assertEquals(0, builder.getConnectionTimeout(options));
153         assertEquals(0, builder.getConnectionTimeoutDuration(options).toMillis());
154         assertEquals(0, builder.getSoTimeout(options));
155         assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(options));
156 
157         // Set with deprecated milliseconds APIs.
158         builder.setConnectionTimeout(options, 60000);
159         builder.setSoTimeout(options, 60000);
160         builder.setUserAgent(options, "foo/bar");
161 
162         // ensure changes are visible
163         assertEquals(60000, builder.getConnectionTimeout(options));
164         assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
165         assertEquals(60000, builder.getSoTimeout(options));
166         assertEquals("foo/bar", builder.getUserAgent(options));
167 
168         // Set with Duration APIs.
169         builder.setConnectionTimeout(options, ONE_MINUTE);
170         builder.setSoTimeout(options, ONE_MINUTE);
171 
172         // ensure changes are visible
173         assertEquals(60000, builder.getConnectionTimeout(options));
174         assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
175         assertEquals(60000, builder.getSoTimeout(options));
176         assertEquals(ONE_MINUTE, builder.getSoTimeoutDuration(options));
177         assertEquals("foo/bar", builder.getUserAgent(options));
178 
179         // TODO: should also check the created HTTPClient
180     }
181 
182     private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
183         VFS.getManager().getFilesCache().close();
184         final FileSystemOptions opts = new FileSystemOptions();
185         HttpFileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
186         try (final FileObject file = VFS.getManager().resolveFile(uri, opts)) {
187             checkReadTestsFolder(file);
188         } catch (final FileNotFolderException e) {
189             // Expected: VFS HTTP does not support listing children yet.
190         }
191     }
192 
193     public void testResloveFolderSlashNoRedirectOff() throws FileSystemException {
194         testResloveFolderSlash(ConnectionUri + "/read-tests", false);
195     }
196 
197     public void testResloveFolderSlashNoRedirectOn() throws FileSystemException {
198         testResloveFolderSlash(ConnectionUri + "/read-tests", true);
199     }
200 
201     public void testResloveFolderSlashYesRedirectOff() throws FileSystemException {
202         testResloveFolderSlash(ConnectionUri + "/read-tests/", false);
203     }
204 
205     public void testResloveFolderSlashYesRedirectOn() throws FileSystemException {
206         testResloveFolderSlash(ConnectionUri + "/read-tests/", true);
207     }
208 }