Loading src/main/kotlin/at/bitfire/dav4jvm/HttpUtils.kt +8 −4 Original line number Original line Diff line number Diff line Loading @@ -21,13 +21,17 @@ object HttpUtils { /** /** * Gets the resource name (the last segment of the path) from an URL. * Gets the resource name (the last segment of the path) from an URL. * Empty if the resource is the base directory. * * * @return resource name or `` (empty string) if the URL ends with a slash * * `dir` for `https://example.com/dir/` * (i.e. the resource is a collection). * * `file` for `https://example.com/file` * * `` for `https://example.com` or `https://example.com/` * * @return resource name */ */ fun fileName(url: HttpUrl): String { fun fileName(url: HttpUrl): String { val pathSegments = url.pathSegments val pathSegments = url.pathSegments.dropLastWhile { it == "" } return pathSegments[pathSegments.size - 1] return pathSegments.lastOrNull() ?: "" } } fun listHeader(response: Response, name: String): Array<String> { fun listHeader(response: Response, name: String): Array<String> { Loading src/test/kotlin/at/bitfire/dav4jvm/HttpUtilsTest.kt +7 −7 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,7 @@ package at.bitfire.dav4jvm package at.bitfire.dav4jvm import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrl import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals import org.junit.Test import org.junit.Test Loading @@ -14,12 +14,12 @@ class HttpUtilsTest { @Test @Test fun testFilename() { fun testFilename() { val sampleUrl = "https://example.com".toHttpUrlOrNull()!! assertEquals("", HttpUtils.fileName("https://example.com".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/")!!)) assertEquals("", HttpUtils.fileName("https://example.com/".toHttpUrl())) assertEquals("hier1", HttpUtils.fileName(sampleUrl.resolve("/hier1")!!)) assertEquals("file1", HttpUtils.fileName("https://example.com/file1".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier1/")!!)) assertEquals("dir1", HttpUtils.fileName("https://example.com/dir1/".toHttpUrl())) assertEquals("hier2", HttpUtils.fileName(sampleUrl.resolve("/hier2")!!)) assertEquals("file2", HttpUtils.fileName("https://example.com/dir1/file2".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier2/")!!)) assertEquals("dir2", HttpUtils.fileName("https://example.com/dir1/dir2/".toHttpUrl())) } } } } Loading
src/main/kotlin/at/bitfire/dav4jvm/HttpUtils.kt +8 −4 Original line number Original line Diff line number Diff line Loading @@ -21,13 +21,17 @@ object HttpUtils { /** /** * Gets the resource name (the last segment of the path) from an URL. * Gets the resource name (the last segment of the path) from an URL. * Empty if the resource is the base directory. * * * @return resource name or `` (empty string) if the URL ends with a slash * * `dir` for `https://example.com/dir/` * (i.e. the resource is a collection). * * `file` for `https://example.com/file` * * `` for `https://example.com` or `https://example.com/` * * @return resource name */ */ fun fileName(url: HttpUrl): String { fun fileName(url: HttpUrl): String { val pathSegments = url.pathSegments val pathSegments = url.pathSegments.dropLastWhile { it == "" } return pathSegments[pathSegments.size - 1] return pathSegments.lastOrNull() ?: "" } } fun listHeader(response: Response, name: String): Array<String> { fun listHeader(response: Response, name: String): Array<String> { Loading
src/test/kotlin/at/bitfire/dav4jvm/HttpUtilsTest.kt +7 −7 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,7 @@ package at.bitfire.dav4jvm package at.bitfire.dav4jvm import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrl import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals import org.junit.Test import org.junit.Test Loading @@ -14,12 +14,12 @@ class HttpUtilsTest { @Test @Test fun testFilename() { fun testFilename() { val sampleUrl = "https://example.com".toHttpUrlOrNull()!! assertEquals("", HttpUtils.fileName("https://example.com".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/")!!)) assertEquals("", HttpUtils.fileName("https://example.com/".toHttpUrl())) assertEquals("hier1", HttpUtils.fileName(sampleUrl.resolve("/hier1")!!)) assertEquals("file1", HttpUtils.fileName("https://example.com/file1".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier1/")!!)) assertEquals("dir1", HttpUtils.fileName("https://example.com/dir1/".toHttpUrl())) assertEquals("hier2", HttpUtils.fileName(sampleUrl.resolve("/hier2")!!)) assertEquals("file2", HttpUtils.fileName("https://example.com/dir1/file2".toHttpUrl())) assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier2/")!!)) assertEquals("dir2", HttpUtils.fileName("https://example.com/dir1/dir2/".toHttpUrl())) } } } }