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 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
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
51
52 private static String ConnectionUri;
53
54 private static String getSystemTestUriOverride() {
55 return System.getProperty(TEST_URI);
56 }
57
58
59
60
61
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
71
72
73
74
75 public static Test suite() throws Exception {
76 return new ProviderTestSuite(new Http4ProviderTestCase()) {
77
78
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
104
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
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
130 public void ignoreTestHttp405() throws FileSystemException {
131 @SuppressWarnings("resource")
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
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
148 @SuppressWarnings("deprecation")
149 public void testHttpTimeoutConfig() {
150 final FileSystemOptions opts = new FileSystemOptions();
151 final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
152
153
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
161 builder.setConnectionTimeout(opts, 60000);
162 builder.setSoTimeout(opts, 60000);
163 builder.setUserAgent(opts, "foo/bar");
164
165
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
173 builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
174 builder.setSoTimeout(opts, Duration.ofMinutes(1));
175 builder.setUserAgent(opts, "foo/bar");
176
177
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")
190 final FileObject file = VFS.getManager().resolveFile(uri, opts);
191 try {
192 checkReadTestsFolder(file);
193 } catch (final FileNotFolderException e) {
194
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 }