1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
50
51 private static String connectionUri;
52
53 private static String getSystemTestUriOverride() {
54 return System.getProperty(TEST_URI);
55 }
56
57
58
59
60
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
69
70
71
72
73 public static junit.framework.Test suite() throws Exception {
74 return new ProviderTestSuite(new HttpProviderTestCase()) {
75
76
77
78 @Override
79 protected void addBaseTests() throws Exception {
80 super.addBaseTests();
81
82 addTests(HttpProviderTestCase.class);
83
84
85
86
87
88
89
90
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
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
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
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
148
149 @Override
150 public void prepare(final DefaultFileSystemManager manager) throws Exception {
151 manager.addProvider("http", new HttpFileProvider());
152 }
153
154
155 @Test
156 public void testHttpTimeoutConfig() {
157 final FileSystemOptions options = new FileSystemOptions();
158 final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
159
160
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
167 builder.setConnectionTimeout(options, 60000);
168 builder.setSoTimeout(options, 60000);
169 builder.setUserAgent(options, "foo/bar");
170
171
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
178 builder.setConnectionTimeout(options, ONE_MINUTE);
179 builder.setSoTimeout(options, ONE_MINUTE);
180
181
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
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
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 }