Skip to content

Commit 6a0b597

Browse files
authored
Simplify UrlUtils.equals(), fix percent sign in tests (#43)
1 parent 92f5f3d commit 6a0b597

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/main/kotlin/at/bitfire/dav4jvm/UrlUtils.kt

+15-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ package at.bitfire.dav4jvm
99
import okhttp3.HttpUrl
1010
import java.net.URI
1111
import java.net.URISyntaxException
12+
import java.net.URLDecoder
13+
import java.util.logging.Level
14+
import java.util.logging.Logger
1215

1316
object UrlUtils {
1417

18+
val logger by lazy { Logger.getLogger(javaClass.name) }
19+
1520
/**
1621
* Compares two URLs in WebDAV context. If two URLs are considered *equal*, both
1722
* represent the same WebDAV resource (e.g. `http://host:80/folder1` and `http://HOST/folder1#somefragment`).
@@ -35,15 +40,20 @@ object UrlUtils {
3540
if (url1 == url2)
3641
return true
3742

38-
// drop #fragment parts and convert to URI
39-
val uri1 = url1.newBuilder().fragment(null).build().toUri()
40-
val uri2 = url2.newBuilder().fragment(null).build().toUri()
43+
// convert to java.net.URI (also corrects some mistakes)
44+
val uri1 = url1.toUri()
45+
val uri2 = url2.toUri()
46+
47+
// if the URIs are the same (ignoring scheme case and fragments), they're equal for us
48+
if (uri1.scheme.equals(uri2.scheme, true) && uri1.schemeSpecificPart == uri2.schemeSpecificPart)
49+
return true
4150

4251
return try {
43-
val decoded1 = URI(uri1.scheme, uri1.schemeSpecificPart, uri1.fragment)
44-
val decoded2 = URI(uri2.scheme, uri2.schemeSpecificPart, uri2.fragment)
52+
val decoded1 = URI(url1.scheme, uri1.schemeSpecificPart, null)
53+
val decoded2 = URI(uri2.scheme, uri2.schemeSpecificPart, null)
4554
decoded1 == decoded2
4655
} catch (e: URISyntaxException) {
56+
logger.log(Level.WARNING, "Couldn't decode URI for comparison, assuming inequality", e)
4757
false
4858
}
4959
}

src/test/kotlin/at/bitfire/dav4jvm/UrlUtilsTest.kt

+16-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package at.bitfire.dav4jvm
88

9+
import okhttp3.HttpUrl
10+
import okhttp3.HttpUrl.Companion.toHttpUrl
911
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
1012
import org.junit.Assert.assertEquals
1113
import org.junit.Assert.assertFalse
@@ -17,17 +19,18 @@ class UrlUtilsTest {
1719

1820
@Test
1921
fun testEquals() {
20-
assertTrue(UrlUtils.equals("http://host/resource".toHttpUrlOrNull()!!, "http://host/resource".toHttpUrlOrNull()!!))
21-
assertTrue(UrlUtils.equals("http://host:80/resource".toHttpUrlOrNull()!!, "http://host/resource".toHttpUrlOrNull()!!))
22-
assertTrue(UrlUtils.equals("https://HOST:443/resource".toHttpUrlOrNull()!!, "https://host/resource".toHttpUrlOrNull()!!))
23-
assertTrue(UrlUtils.equals("https://host:443/my@dav/".toHttpUrlOrNull()!!, "https://host/my%40dav/".toHttpUrlOrNull()!!))
24-
assertTrue(UrlUtils.equals("http://host/resource".toHttpUrlOrNull()!!, "http://host/resource#frag1".toHttpUrlOrNull()!!))
22+
assertTrue(UrlUtils.equals("http://host/resource".toHttpUrl(), "http://host/resource".toHttpUrl()))
23+
assertTrue(UrlUtils.equals("http://host:80/resource".toHttpUrl(), "http://host/resource".toHttpUrl()))
24+
assertTrue(UrlUtils.equals("https://HOST:443/resource".toHttpUrl(), "https://host/resource".toHttpUrl()))
25+
assertTrue(UrlUtils.equals("https://host:443/my@dav/".toHttpUrl(), "https://host/my%40dav/".toHttpUrl()))
26+
assertTrue(UrlUtils.equals("http://host/resource".toHttpUrl(), "http://host/resource#frag1".toHttpUrl()))
2527

26-
// should work, but currently doesn't (see MR #5)
27-
// assertTrue(UrlUtils.equals(HttpUrl.parse("https://host/%5bresource%5d/")!!, HttpUrl.parse("https://host/[resource]/")!!))
28+
assertFalse(UrlUtils.equals("http://host/resource".toHttpUrl(), "http://host/resource/".toHttpUrl()))
29+
assertFalse(UrlUtils.equals("http://host/resource".toHttpUrl(), "http://host:81/resource".toHttpUrl()))
2830

29-
assertFalse(UrlUtils.equals("http://host/resource".toHttpUrlOrNull()!!, "http://host/resource/".toHttpUrlOrNull()!!))
30-
assertFalse(UrlUtils.equals("http://host/resource".toHttpUrlOrNull()!!, "http://host:81/resource".toHttpUrlOrNull()!!))
31+
assertTrue(UrlUtils.equals("https://www.example.com/folder/[X]Y!.txt".toHttpUrl(), "https://www.example.com/folder/[X]Y!.txt".toHttpUrl()))
32+
assertTrue(UrlUtils.equals("https://www.example.com/folder/%5BX%5DY!.txt".toHttpUrl(), "https://www.example.com/folder/[X]Y!.txt".toHttpUrl()))
33+
assertTrue(UrlUtils.equals("https://www.example.com/folder/%5bX%5dY%21.txt".toHttpUrl(), "https://www.example.com/folder/[X]Y!.txt".toHttpUrl()))
3134
}
3235

3336
@Test
@@ -48,14 +51,14 @@ class UrlUtilsTest {
4851

4952
@Test
5053
fun testOmitTrailingSlash() {
51-
assertEquals("http://host/resource".toHttpUrlOrNull()!!, UrlUtils.omitTrailingSlash("http://host/resource".toHttpUrlOrNull()!!))
52-
assertEquals("http://host/resource".toHttpUrlOrNull()!!, UrlUtils.omitTrailingSlash("http://host/resource/".toHttpUrlOrNull()!!))
54+
assertEquals("http://host/resource".toHttpUrl(), UrlUtils.omitTrailingSlash("http://host/resource".toHttpUrl()))
55+
assertEquals("http://host/resource".toHttpUrl(), UrlUtils.omitTrailingSlash("http://host/resource/".toHttpUrl()))
5356
}
5457

5558
@Test
5659
fun testWithTrailingSlash() {
57-
assertEquals("http://host/resource/".toHttpUrlOrNull()!!, UrlUtils.withTrailingSlash("http://host/resource".toHttpUrlOrNull()!!))
58-
assertEquals("http://host/resource/".toHttpUrlOrNull()!!, UrlUtils.withTrailingSlash("http://host/resource/".toHttpUrlOrNull()!!))
60+
assertEquals("http://host/resource/".toHttpUrl(), UrlUtils.withTrailingSlash("http://host/resource".toHttpUrl()))
61+
assertEquals("http://host/resource/".toHttpUrl(), UrlUtils.withTrailingSlash("http://host/resource/".toHttpUrl()))
5962
}
6063

6164
}

0 commit comments

Comments
 (0)