1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http5;
18
19 import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20
21 import java.io.File;
22 import java.time.Duration;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.vfs2.AbstractProviderTestConfig;
26 import org.apache.commons.vfs2.FileNotFolderException;
27 import org.apache.commons.vfs2.FileObject;
28 import org.apache.commons.vfs2.FileSystemException;
29 import org.apache.commons.vfs2.FileSystemManager;
30 import org.apache.commons.vfs2.FileSystemOptions;
31 import org.apache.commons.vfs2.ProviderTestSuite;
32 import org.apache.commons.vfs2.VFS;
33 import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
34 import org.apache.commons.vfs2.util.NHttpFileServer;
35 import org.junit.Test;
36 import org.junit.jupiter.api.Assertions;
37
38
39
40
41 public class Http5ProviderTestCase extends AbstractProviderTestConfig {
42
43 private static NHttpFileServer server;
44
45 private static final String TEST_URI = "test.http.uri";
46
47
48
49
50 private static String connectionUri;
51
52 private static String getSystemTestUriOverride() {
53 return System.getProperty(TEST_URI);
54 }
55
56
57
58
59
60
61 private static void setUpClass() throws Exception {
62 server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
63 connectionUri = getLocalHostUriString("http5", server.getPort());
64 }
65
66
67
68
69
70
71
72 public static junit.framework.Test suite() throws Exception {
73 return new ProviderTestSuite(new Http5ProviderTestCase()) {
74
75
76
77 @Override
78 protected void addBaseTests() throws Exception {
79 super.addBaseTests();
80
81 addTests(Http5ProviderTestCase.class);
82
83
84
85
86
87
88
89
90
91 }
92
93 @Override
94 protected void setUp() throws Exception {
95 if (getSystemTestUriOverride() == null) {
96 setUpClass();
97 }
98 super.setUp();
99 }
100
101 @Override
102 protected void tearDown() throws Exception {
103 tearDownClass();
104 super.tearDown();
105 }
106 };
107 }
108
109
110
111
112
113 private static void tearDownClass() throws InterruptedException {
114 if (server != null) {
115 server.shutdown(5000, TimeUnit.SECONDS);
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 final FileObject fileObject = VFS.getManager()
139 .resolveFile("http5://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
140 Assertions.assertFalse(fileObject.getContent().isEmpty(), "Content should not be empty");
141 }
142
143
144
145
146 @Override
147 public void prepare(final DefaultFileSystemManager manager) throws Exception {
148 if (!manager.hasProvider("http5")) {
149 manager.addProvider("http5", new Http5FileProvider());
150 }
151 }
152
153
154 @SuppressWarnings("deprecation")
155 @Test
156 public void testHttpTimeoutConfig() {
157 final FileSystemOptions opts = new FileSystemOptions();
158 final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
159
160
161 assertEquals(0, builder.getConnectionTimeout(opts));
162 assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
163 assertEquals(0, builder.getSoTimeout(opts));
164 assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
165 assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
166
167
168 builder.setConnectionTimeout(opts, 60000);
169 builder.setSoTimeout(opts, 60000);
170 builder.setUserAgent(opts, "foo/bar");
171
172
173 assertEquals(60000, builder.getConnectionTimeout(opts));
174 assertEquals(60000, builder.getSoTimeout(opts));
175 assertEquals("foo/bar", builder.getUserAgent(opts));
176
177
178 builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
179 builder.setSoTimeout(opts, Duration.ofMinutes(1));
180 builder.setUserAgent(opts, "foo/bar");
181
182
183 assertEquals(60000, builder.getConnectionTimeoutDuration(opts).toMillis());
184 assertEquals(60000, builder.getSoTimeoutDuration(opts).toMillis());
185 assertEquals("foo/bar", builder.getUserAgent(opts));
186 }
187
188 private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
189 VFS.getManager().getFilesCache().close();
190 final FileSystemOptions opts = new FileSystemOptions();
191 Http5FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
192 final FileObject file = VFS.getManager().resolveFile(uri, opts);
193 try {
194 checkReadTestsFolder(file);
195 } catch (final FileNotFolderException e) {
196
197 }
198 }
199
200 @Test
201 public void testResolveFolderSlashNoRedirectOff() throws FileSystemException {
202 testResolveFolderSlash(connectionUri + "/read-tests", false);
203 }
204
205 @Test
206 public void testResolveFolderSlashNoRedirectOn() throws FileSystemException {
207 testResolveFolderSlash(connectionUri + "/read-tests", true);
208 }
209
210 @Test
211 public void testResolveFolderSlashYesRedirectOff() throws FileSystemException {
212 testResolveFolderSlash(connectionUri + "/read-tests/", false);
213 }
214
215 @Test
216 public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
217 testResolveFolderSlash(connectionUri + "/read-tests/", true);
218 }
219
220 @Test
221 public void testResolveIPv6Url() throws FileSystemException {
222 final String ipv6Url = "http5://[fe80::1c42:dae:8370:aea6%en1]";
223
224 @SuppressWarnings("rawtypes")
225 final Http5FileObject fileObject = (Http5FileObject)
226 VFS.getManager().resolveFile(ipv6Url, new FileSystemOptions());
227
228 assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getInternalURI().toString());
229 }
230 }