1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http4;
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 Http4ProviderTestCase 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 final int socketPort = server.getPort();
64 connectionUri = getLocalHostUriString("http4", server.getPort());
65 }
66
67
68
69
70
71
72
73 public static junit.framework.Test suite() throws Exception {
74 return new ProviderTestSuite(new Http4ProviderTestCase()) {
75
76
77
78 @Override
79 protected void addBaseTests() throws Exception {
80 super.addBaseTests();
81
82 addTests(Http4ProviderTestCase.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
114 private static void tearDownClass() throws InterruptedException {
115 if (server != null) {
116 server.shutdown(5000, TimeUnit.SECONDS);
117 }
118 }
119
120 private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
121 Assertions.assertNotNull(file.getChildren());
122 Assertions.assertTrue(file.getChildren().length > 0);
123 }
124
125
126
127
128 @Override
129 public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
130 String uri = getSystemTestUriOverride();
131 if (uri == null) {
132 uri = connectionUri;
133 }
134 return manager.resolveFile(uri);
135 }
136
137
138 public void ignoreTestHttp405() throws FileSystemException {
139 @SuppressWarnings("resource")
140 final FileObject fileObject = VFS.getManager()
141 .resolveFile("http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL");
142 Assertions.assertFalse(fileObject.getContent().isEmpty(), "Content should not be empty");
143 }
144
145
146
147
148 @Override
149 public void prepare(final DefaultFileSystemManager manager) throws Exception {
150 if (!manager.hasProvider("http4")) {
151 manager.addProvider("http4", new Http4FileProvider());
152 }
153 }
154
155
156 @SuppressWarnings("deprecation")
157 @Test
158 public void testHttpTimeoutConfig() {
159 final FileSystemOptions opts = new FileSystemOptions();
160 final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
161
162
163 assertEquals(0, builder.getConnectionTimeout(opts));
164 assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
165 assertEquals(0, builder.getSoTimeout(opts));
166 assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
167 assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
168
169
170 builder.setConnectionTimeout(opts, 60000);
171 builder.setSoTimeout(opts, 60000);
172 builder.setUserAgent(opts, "foo/bar");
173
174
175 assertEquals(60_000, builder.getConnectionTimeout(opts));
176 assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
177 assertEquals(60_000, builder.getSoTimeout(opts));
178 assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
179 assertEquals("foo/bar", builder.getUserAgent(opts));
180
181
182 builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
183 builder.setSoTimeout(opts, Duration.ofMinutes(1));
184 builder.setUserAgent(opts, "foo/bar");
185
186
187 assertEquals(60_000, builder.getConnectionTimeout(opts));
188 assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
189 assertEquals(60_000, builder.getSoTimeout(opts));
190 assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
191 assertEquals("foo/bar", builder.getUserAgent(opts));
192 }
193
194 private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
195 VFS.getManager().getFilesCache().close();
196 final FileSystemOptions opts = new FileSystemOptions();
197 Http4FileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
198 @SuppressWarnings("resource")
199 final FileObject file = VFS.getManager().resolveFile(uri, opts);
200 try {
201 checkReadTestsFolder(file);
202 } catch (final FileNotFolderException e) {
203
204 }
205 }
206
207 @Test
208 public void testResolveFolderSlashNoRedirectOff() throws FileSystemException {
209 testResolveFolderSlash(connectionUri + "/read-tests", false);
210 }
211
212 @Test
213 public void testResolveFolderSlashNoRedirectOn() throws FileSystemException {
214 testResolveFolderSlash(connectionUri + "/read-tests", true);
215 }
216
217 @Test
218 public void testResolveFolderSlashYesRedirectOff() throws FileSystemException {
219 testResolveFolderSlash(connectionUri + "/read-tests/", false);
220 }
221
222 @Test
223 public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
224 testResolveFolderSlash(connectionUri + "/read-tests/", true);
225 }
226
227 @Test
228 public void testResolveIPv6Url() throws FileSystemException {
229 final String ipv6Url = "http4://[fe80::1c42:dae:8370:aea6%en1]";
230
231 @SuppressWarnings("rawtypes")
232 final Http4FileObject fileObject = (Http4FileObject)
233 VFS.getManager().resolveFile(ipv6Url, new FileSystemOptions());
234
235 assertEquals("http://[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getInternalURI().toString());
236 }
237 }