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 java.io.File;
20 import java.time.Duration;
21
22 import org.apache.commons.vfs2.AbstractProviderTestConfig;
23 import org.apache.commons.vfs2.FileNotFolderException;
24 import org.apache.commons.vfs2.FileObject;
25 import org.apache.commons.vfs2.FileSystemException;
26 import org.apache.commons.vfs2.FileSystemManager;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.ProviderTestSuite;
29 import org.apache.commons.vfs2.VFS;
30 import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
31 import org.apache.commons.vfs2.util.NHttpFileServer;
32 import org.junit.Assert;
33
34 import junit.framework.Test;
35
36
37
38
39
40 public class HttpProviderTestCase extends AbstractProviderTestConfig {
41 private static final Duration ONE_MINUTE = Duration.ofMinutes(1);
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 = "http://localhost:" + SocketPort;
67 }
68
69
70
71
72
73
74
75 public static Test suite() throws Exception {
76 return new ProviderTestSuite(new HttpProviderTestCase()) {
77
78
79
80 @Override
81 protected void addBaseTests() throws Exception {
82 super.addBaseTests();
83 addTests(HttpProviderTestCase.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 private static void tearDownClass() {
106 if (Server != null) {
107 Server.close();
108 }
109 }
110
111 private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
112 Assert.assertNotNull(file.getChildren());
113 Assert.assertTrue(file.getChildren().length > 0);
114 }
115
116
117
118
119 @Override
120 public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
121 String uri = getSystemTestUriOverride();
122 if (uri == null) {
123 uri = ConnectionUri;
124 }
125 return manager.resolveFile(uri);
126 }
127
128
129 public void ignoreTestHttp405() throws FileSystemException {
130 try (final FileObject fileObject = VFS.getManager()
131 .resolveFile("http://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL")) {
132 assert fileObject.getContent().getSize() > 0;
133 assert !fileObject.getContent().isEmpty();
134
135 }
136 }
137
138
139
140
141 @Override
142 public void prepare(final DefaultFileSystemManager manager) throws Exception {
143 manager.addProvider("http", new HttpFileProvider());
144 }
145
146
147 public void testHttpTimeoutConfig() {
148 final FileSystemOptions options = new FileSystemOptions();
149 final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
150
151
152 assertEquals(0, builder.getConnectionTimeout(options));
153 assertEquals(0, builder.getConnectionTimeoutDuration(options).toMillis());
154 assertEquals(0, builder.getSoTimeout(options));
155 assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(options));
156
157
158 builder.setConnectionTimeout(options, 60000);
159 builder.setSoTimeout(options, 60000);
160 builder.setUserAgent(options, "foo/bar");
161
162
163 assertEquals(60000, builder.getConnectionTimeout(options));
164 assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
165 assertEquals(60000, builder.getSoTimeout(options));
166 assertEquals("foo/bar", builder.getUserAgent(options));
167
168
169 builder.setConnectionTimeout(options, ONE_MINUTE);
170 builder.setSoTimeout(options, ONE_MINUTE);
171
172
173 assertEquals(60000, builder.getConnectionTimeout(options));
174 assertEquals(ONE_MINUTE, builder.getConnectionTimeoutDuration(options));
175 assertEquals(60000, builder.getSoTimeout(options));
176 assertEquals(ONE_MINUTE, builder.getSoTimeoutDuration(options));
177 assertEquals("foo/bar", builder.getUserAgent(options));
178
179
180 }
181
182 private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
183 VFS.getManager().getFilesCache().close();
184 final FileSystemOptions opts = new FileSystemOptions();
185 HttpFileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect);
186 try (final FileObject file = VFS.getManager().resolveFile(uri, opts)) {
187 checkReadTestsFolder(file);
188 } catch (final FileNotFolderException e) {
189
190 }
191 }
192
193 public void testResloveFolderSlashNoRedirectOff() throws FileSystemException {
194 testResloveFolderSlash(ConnectionUri + "/read-tests", false);
195 }
196
197 public void testResloveFolderSlashNoRedirectOn() throws FileSystemException {
198 testResloveFolderSlash(ConnectionUri + "/read-tests", true);
199 }
200
201 public void testResloveFolderSlashYesRedirectOff() throws FileSystemException {
202 testResloveFolderSlash(ConnectionUri + "/read-tests/", false);
203 }
204
205 public void testResloveFolderSlashYesRedirectOn() throws FileSystemException {
206 testResloveFolderSlash(ConnectionUri + "/read-tests/", true);
207 }
208 }