Skip to content

Commit 732f054

Browse files
committed
Add updated test from #700
Adapt test to "http4" provider
1 parent f5d7ac0 commit 732f054

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919
import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
2020

2121
import java.io.File;
22+
import java.io.InputStream;
2223
import java.time.Duration;
2324
import java.util.concurrent.TimeUnit;
2425

2526
import org.apache.commons.vfs2.AbstractProviderTestConfig;
27+
import org.apache.commons.vfs2.CacheStrategy;
28+
import org.apache.commons.vfs2.FileContent;
2629
import org.apache.commons.vfs2.FileNotFolderException;
2730
import org.apache.commons.vfs2.FileObject;
2831
import org.apache.commons.vfs2.FileSystemException;
2932
import org.apache.commons.vfs2.FileSystemManager;
3033
import org.apache.commons.vfs2.FileSystemOptions;
3134
import org.apache.commons.vfs2.ProviderTestSuite;
3235
import org.apache.commons.vfs2.VFS;
36+
import org.apache.commons.vfs2.cache.SoftRefFilesCache;
3337
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
3438
import org.apache.commons.vfs2.util.NHttpFileServer;
3539
import org.junit.Test;
@@ -191,6 +195,36 @@ public void testHttpTimeoutConfig() {
191195
assertEquals("foo/bar", builder.getUserAgent(opts));
192196
}
193197

198+
@Test
199+
public void testReadFileOperations() throws Exception {
200+
try (DefaultFileSystemManager manager = new DefaultFileSystemManager();
201+
Http4FileProvider provider = new Http4FileProvider();
202+
SoftRefFilesCache filesCache = new SoftRefFilesCache();) {
203+
manager.addProvider("http4", provider);
204+
manager.setFilesCache(filesCache);
205+
manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
206+
final String nonExistentFileUri = connectionUri + "/read-tests/nonexistent.txt";
207+
try (FileObject nonExistentFileObject = manager.resolveFile(nonExistentFileUri); FileContent content = nonExistentFileObject.getContent();) {
208+
// Attempt to read from the stream
209+
final FileSystemException e = Assertions.assertThrows(FileSystemException.class, content::getInputStream);
210+
Assertions.assertTrue(e.getCode().contains("read-not-file.error"),
211+
"Expected HTTP 404 Not Found error, but got: " + e.getMessage() + " " + e.getCode());
212+
}
213+
final String existentFileUri = connectionUri + "/read-tests/file1.txt";
214+
try (FileObject existentFileObject = manager.resolveFile(existentFileUri)) {
215+
Assertions.assertTrue(existentFileObject.exists(), "File should exist");
216+
try (FileContent content = existentFileObject.getContent(); InputStream inputStream = content.getInputStream()) {
217+
Assertions.assertNotNull(inputStream, "InputStream should not be null");
218+
final int available = inputStream.available();
219+
Assertions.assertTrue(available > 0, "InputStream should have content available");
220+
final byte[] buffer = new byte[1024];
221+
final int bytesRead = inputStream.read(buffer);
222+
Assertions.assertTrue(bytesRead > 0, "InputStream should read non-empty content");
223+
}
224+
}
225+
}
226+
}
227+
194228
private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
195229
VFS.getManager().getFilesCache().close();
196230
final FileSystemOptions opts = new FileSystemOptions();

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/Http5ProviderTestCase.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919
import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
2020

2121
import java.io.File;
22+
import java.io.InputStream;
2223
import java.time.Duration;
2324
import java.util.concurrent.TimeUnit;
2425

2526
import org.apache.commons.vfs2.AbstractProviderTestConfig;
27+
import org.apache.commons.vfs2.CacheStrategy;
28+
import org.apache.commons.vfs2.FileContent;
2629
import org.apache.commons.vfs2.FileNotFolderException;
2730
import org.apache.commons.vfs2.FileObject;
2831
import org.apache.commons.vfs2.FileSystemException;
2932
import org.apache.commons.vfs2.FileSystemManager;
3033
import org.apache.commons.vfs2.FileSystemOptions;
3134
import org.apache.commons.vfs2.ProviderTestSuite;
3235
import org.apache.commons.vfs2.VFS;
36+
import org.apache.commons.vfs2.cache.SoftRefFilesCache;
3337
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
3438
import org.apache.commons.vfs2.util.NHttpFileServer;
3539
import org.junit.Test;
@@ -185,6 +189,36 @@ public void testHttpTimeoutConfig() {
185189
assertEquals("foo/bar", builder.getUserAgent(opts));
186190
}
187191

192+
@Test
193+
public void testReadFileOperations() throws Exception {
194+
try (DefaultFileSystemManager manager = new DefaultFileSystemManager();
195+
Http5FileProvider provider = new Http5FileProvider();
196+
SoftRefFilesCache filesCache = new SoftRefFilesCache();) {
197+
manager.addProvider("http5", provider);
198+
manager.setFilesCache(filesCache);
199+
manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
200+
final String nonExistentFileUri = connectionUri + "/read-tests/nonexistent.txt";
201+
try (FileObject nonExistentFileObject = manager.resolveFile(nonExistentFileUri); FileContent content = nonExistentFileObject.getContent();) {
202+
// Attempt to read from the stream
203+
final FileSystemException e = Assertions.assertThrows(FileSystemException.class, content::getInputStream);
204+
Assertions.assertTrue(e.getCode().contains("read-not-file.error"),
205+
"Expected HTTP 404 Not Found error, but got: " + e.getMessage() + " " + e.getCode());
206+
}
207+
final String existentFileUri = connectionUri + "/read-tests/file1.txt";
208+
try (FileObject existentFileObject = manager.resolveFile(existentFileUri)) {
209+
Assertions.assertTrue(existentFileObject.exists(), "File should exist");
210+
try (FileContent content = existentFileObject.getContent(); InputStream inputStream = content.getInputStream()) {
211+
Assertions.assertNotNull(inputStream, "InputStream should not be null");
212+
final int available = inputStream.available();
213+
Assertions.assertTrue(available > 0, "InputStream should have content available");
214+
final byte[] buffer = new byte[1024];
215+
final int bytesRead = inputStream.read(buffer);
216+
Assertions.assertTrue(bytesRead > 0, "InputStream should read non-empty content");
217+
}
218+
}
219+
}
220+
}
221+
188222
private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
189223
VFS.getManager().getFilesCache().close();
190224
final FileSystemOptions opts = new FileSystemOptions();

0 commit comments

Comments
 (0)