Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 4eb1af9

Browse files
always resolve paths relative to the WebDAV base URL (fixing tests)
1 parent 862bcce commit 4eb1af9

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/main/java/org/cryptomator/cloudaccess/webdav/WebDavClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.ArrayList;
2323
import java.util.Comparator;
2424
import java.util.List;
25+
import java.util.stream.IntStream;
26+
import java.util.stream.Stream;
2527

2628
public class WebDavClient {
2729

@@ -283,10 +285,12 @@ private void checkExecutionSucceeded(final int status) throws CloudProviderExcep
283285
}
284286
}
285287

286-
private URL absoluteURLFrom(final Path relativePath) {
287-
// TODO improve path appending
288+
// visible for testing
289+
URL absoluteURLFrom(final Path relativePath) {
290+
var basePath = Path.of(baseUrl.getPath());
291+
var fullPath = IntStream.range(0, relativePath.getNameCount()).mapToObj(i -> relativePath.getName(i)).reduce(basePath, Path::resolve);
288292
try {
289-
return new URL(baseUrl, relativePath.toString());
293+
return new URL(baseUrl, fullPath.toString());
290294
} catch (MalformedURLException e) {
291295
throw new IllegalArgumentException("The relative path contains invalid URL elements.");
292296
}

src/test/java/org/cryptomator/cloudaccess/webdav/WebDavClientTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.DisplayName;
1515
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.CsvSource;
1618
import org.mockito.ArgumentMatchers;
1719
import org.mockito.Mockito;
1820

@@ -48,6 +50,21 @@ public void setup() throws MalformedURLException {
4850
webDavClient = new WebDavClient(webDavCompatibleHttpClient, webDavCredential);
4951
}
5052

53+
@ParameterizedTest
54+
@DisplayName("getPath()")
55+
@CsvSource(value = {
56+
"'',/cloud/remote.php/webdav",
57+
"/,/cloud/remote.php/webdav",
58+
"/foo,/cloud/remote.php/webdav/foo",
59+
"foo/bar,/cloud/remote.php/webdav/foo/bar",
60+
"//foo///bar/baz,/cloud/remote.php/webdav/foo/bar/baz",
61+
})
62+
public void testAbsoluteURLFrom(String absPath, String expectedResult) {
63+
var result = webDavClient.absoluteURLFrom(Path.of(absPath));
64+
65+
Assertions.assertEquals(expectedResult, result.getPath());
66+
}
67+
5168
@Test
5269
@DisplayName("get metadata of /Nextcloud Manual.pdf")
5370
public void testItemMetadata() throws IOException {

src/test/java/org/cryptomator/cloudaccess/webdav/WebDavCloudProviderTestIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public WebDavCloudProviderTestIT() throws IOException, InterruptedException {
4343
server = new MockWebServer();
4444
server.start();
4545

46-
baseUrl = new URL("http", server.getHostName(), server.getPort(), "cloud/remote.php/webdav");
46+
baseUrl = new URL("http", server.getHostName(), server.getPort(), "/cloud/remote.php/webdav");
4747

4848
final var response = getInterceptedResponse("item-meta-data-response.xml");
4949
server.enqueue(response);
@@ -89,7 +89,7 @@ public void testList() throws InterruptedException {
8989
RecordedRequest rq = server.takeRequest();
9090
Assertions.assertEquals("PROPFIND", rq.getMethod());
9191
Assertions.assertEquals("1", rq.getHeader("DEPTH"));
92-
Assertions.assertEquals("/cloud/remote.php/webdav/", rq.getPath());
92+
Assertions.assertEquals("/cloud/remote.php/webdav", rq.getPath());
9393
Assertions.assertEquals(webDavRequestBody, rq.getBody().readUtf8());
9494
}
9595

@@ -116,7 +116,7 @@ public void testListExhaustively() throws InterruptedException {
116116
RecordedRequest rq = server.takeRequest();
117117
Assertions.assertEquals("PROPFIND", rq.getMethod());
118118
Assertions.assertEquals("infinity", rq.getHeader("DEPTH"));
119-
Assertions.assertEquals("/cloud/remote.php/webdav/", rq.getPath());
119+
Assertions.assertEquals("/cloud/remote.php/webdav", rq.getPath());
120120
Assertions.assertEquals(webDavRequestBody, rq.getBody().readUtf8());
121121
}
122122

0 commit comments

Comments
 (0)