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.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.Locale;
22  
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.FileSystemManager;
26  import org.apache.commons.vfs2.VFS;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests https://issues.apache.org/jira/browse/VFS-426 and https://issues.apache.org/jira/browse/VFS-810.
31   */
32  public class Http4FilesCacheTest {
33  
34      @Test
35      public void testQueryStringUrl0() throws FileSystemException {
36          @SuppressWarnings("resource")
37          final FileSystemManager fileSystemManager = VFS.getManager();
38  
39          final String noQueryStringUrl = "http4://commons.apache.org/";
40          try (FileObject noQueryFile = fileSystemManager.resolveFile(noQueryStringUrl)) {
41              assertEquals(noQueryStringUrl, noQueryFile.getURL().toExternalForm());
42          }
43      }
44  
45      @Test
46      public void testQueryStringUrl1() throws FileSystemException {
47          @SuppressWarnings("resource")
48          final FileSystemManager fileSystemManager = VFS.getManager();
49  
50          final String noQueryStringUrl = "http4://commons.apache.org/vfs";
51          try (FileObject noQueryFile = fileSystemManager.resolveFile(noQueryStringUrl)) {
52              assertEquals(noQueryStringUrl, noQueryFile.getURL().toExternalForm());
53          }
54      }
55  
56      @Test
57      public void testQueryStringUrl2() throws FileSystemException {
58          @SuppressWarnings("resource")
59          final FileSystemManager fileSystemManager = VFS.getManager();
60  
61          final String queryStringUrl = "http4://commons.apache.org/vfs?query=string";
62          try (FileObject queryFile = fileSystemManager.resolveFile(queryStringUrl)) {
63              assertEquals(queryStringUrl, queryFile.getURL().toExternalForm()); // failed for VFS-426
64          }
65      }
66  
67      @Test
68      public void testQueryStringUrl3() throws FileSystemException {
69          @SuppressWarnings("resource")
70          final FileSystemManager fileSystemManager = VFS.getManager();
71  
72          final String queryStringUrl2 = "http4://commons.apache.org/vfs?query=string&more=stuff";
73          try (FileObject queryFile2 = fileSystemManager.resolveFile(queryStringUrl2)) {
74              assertEquals(queryStringUrl2, queryFile2.getURL().toExternalForm()); // failed for VFS-426
75          }
76      }
77  
78      @Test
79      public void testQueryStringUrl4() throws FileSystemException {
80          @SuppressWarnings("resource")
81          final FileSystemManager fileSystemManager = VFS.getManager();
82  
83          // TODO All lowercase input except the percent encoded '\' (%5C);
84          // We end up converting back to lowercase, but OK per RFC.
85          final String queryStringUrl3 = "http4://alice%5C1234:secret@localhost:8080/";
86          try (FileObject queryFile3 = fileSystemManager.resolveFile(queryStringUrl3)) {
87              assertEquals(queryStringUrl3.toLowerCase(Locale.ROOT), queryFile3.getURL().toExternalForm());
88          }
89      }
90  
91      @Test
92      public void testQueryStringUrl5() throws FileSystemException {
93          @SuppressWarnings("resource")
94          final FileSystemManager fileSystemManager = VFS.getManager();
95  
96          // Like testQueryStringUrl4() but with all LC input.
97          final String queryStringUrl4 = "http4://alice%5c1234:secret@localhost:8080/";
98          try (FileObject queryFile4 = fileSystemManager.resolveFile(queryStringUrl4)) {
99              assertEquals(queryStringUrl4, queryFile4.getURL().toExternalForm());
100         }
101     }
102 
103     @Test
104     public void testQueryStringUrl6() throws FileSystemException {
105         @SuppressWarnings("resource")
106         final FileSystemManager fileSystemManager = VFS.getManager();
107 
108         // Like testQueryStringUrl4() but with all LC input and NO percent encoding.
109         final String queryStringUrl4 = "http4://alice:secret@localhost:8080/";
110         try (FileObject queryFile4 = fileSystemManager.resolveFile(queryStringUrl4)) {
111             assertEquals(queryStringUrl4, queryFile4.getURL().toExternalForm());
112         }
113     }
114 
115     @Test
116     public void testQueryStringUrl7() throws FileSystemException {
117         @SuppressWarnings("resource")
118         final FileSystemManager fileSystemManager = VFS.getManager();
119 
120         // Like testQueryStringUrl4() but with all LC input and NO percent encoding.
121         final String queryStringUrl4 = "http4://localhost:8080/";
122         try (FileObject queryFile4 = fileSystemManager.resolveFile(queryStringUrl4)) {
123             assertEquals(queryStringUrl4, queryFile4.getURL().toExternalForm());
124         }
125     }
126 
127 }