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 java.io.File;
20  import java.time.Duration;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.commons.vfs2.AbstractProviderTestConfig;
24  import org.apache.commons.vfs2.FileNotFolderException;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileSystemManager;
28  import org.apache.commons.vfs2.FileSystemOptions;
29  import org.apache.commons.vfs2.ProviderTestSuite;
30  import org.apache.commons.vfs2.VFS;
31  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
32  import org.apache.commons.vfs2.util.NHttpFileServer;
33  import org.junit.Assert;
34  
35  import junit.framework.Test;
36  
37  /**
38   * Test cases for the HTTP4 provider.
39   *
40   */
41  public class Http4ProviderTestCase extends AbstractProviderTestConfig {
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 = "http4://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 Http4ProviderTestCase()) {
77              /**
78               * Adds base tests - excludes the nested test cases.
79               */
80              @Override
81              protected void addBaseTests() throws Exception {
82                  super.addBaseTests();
83                  addTests(Http4ProviderTestCase.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      * @throws InterruptedException
105      */
106     private static void tearDownClass() throws InterruptedException {
107         if (Server != null) {
108             Server.shutdown(5000, TimeUnit.SECONDS);
109         }
110     }
111 
112     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
113         Assert.assertNotNull(file.getChildren());
114         Assert.assertTrue(file.getChildren().length > 0);
115     }
116 
117     /**
118      * Returns the base folder for tests.
119      */
120     @Override
121     public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
122         String uri = getSystemTestUriOverride();
123         if (uri == null) {
124             uri = ConnectionUri;
125         }
126         return manager.resolveFile(uri);
127     }
128 
129     // Test no longer passing 2016/04/28
130     public void ignoreTestHttp405() throws FileSystemException {
131         @SuppressWarnings("resource") // getManager() returns a global.
132         final FileObject fileObject = VFS.getManager()
133                 .resolveFile("http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
134         assert !fileObject.getContent().isEmpty();
135     }
136 
137     /**
138      * Prepares the file system manager.
139      */
140     @Override
141     public void prepare(final DefaultFileSystemManager manager) throws Exception {
142         if (!manager.hasProvider("http4")) {
143             manager.addProvider("http4", new Http4FileProvider());
144         }
145     }
146 
147     /** Ensure VFS-453 options are present. */
148     @SuppressWarnings("deprecation")
149     public void testHttpTimeoutConfig() {
150         final FileSystemOptions opts = new FileSystemOptions();
151         final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
152 
153         // ensure defaults are 0
154         assertEquals(0, builder.getConnectionTimeout(opts));
155         assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
156         assertEquals(0, builder.getSoTimeout(opts));
157         assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
158         assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
159 
160         // Set int timeouts
161         builder.setConnectionTimeout(opts, 60000);
162         builder.setSoTimeout(opts, 60000);
163         builder.setUserAgent(opts, "foo/bar");
164 
165         // ensure changes are visible
166         assertEquals(60_000, builder.getConnectionTimeout(opts));
167         assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
168         assertEquals(60_000, builder.getSoTimeout(opts));
169         assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
170         assertEquals("foo/bar", builder.getUserAgent(opts));
171 
172         // Set Duration timeouts
173         builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
174         builder.setSoTimeout(opts, Duration.ofMinutes(1));
175         builder.setUserAgent(opts, "foo/bar");
176 
177         // ensure changes are visible
178         assertEquals(60_000, builder.getConnectionTimeout(opts));
179         assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
180         assertEquals(60_000, builder.getSoTimeout(opts));
181         assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
182         assertEquals("foo/bar", builder.getUserAgent(opts));
183     }
184 
185     private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
186         VFS.getManager().getFilesCache().close();
187         final FileSystemOptions opts = new FileSystemOptions();
188         Http4FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
189         @SuppressWarnings("resource") // getManager() returns a global.
190         final FileObject file = VFS.getManager().resolveFile(uri, opts);
191         try {
192             checkReadTestsFolder(file);
193         } catch (final FileNotFolderException e) {
194             // Expected: VFS HTTP does not support listing children yet.
195         }
196     }
197 
198     public void testResloveFolderSlashNoRedirectOff() throws FileSystemException {
199         testResloveFolderSlash(ConnectionUri + "/read-tests", false);
200     }
201 
202     public void testResloveFolderSlashNoRedirectOn() throws FileSystemException {
203         testResloveFolderSlash(ConnectionUri + "/read-tests", true);
204     }
205 
206     public void testResloveFolderSlashYesRedirectOff() throws FileSystemException {
207         testResloveFolderSlash(ConnectionUri + "/read-tests/", false);
208     }
209 
210     public void testResloveFolderSlashYesRedirectOn() throws FileSystemException {
211         testResloveFolderSlash(ConnectionUri + "/read-tests/", true);
212     }
213 }