Commit 83bad4d4 authored by Ricki Hirner's avatar Ricki Hirner

Move fileName() to HttpUtils

parent bc77df0f
Pipeline #22500573 passed with stages
......@@ -59,15 +59,9 @@ open class DavResource @JvmOverloads constructor(
/**
* The resource name (the last segment of the URL path).
*
* @return resource name or `` (empty string) if the URL ends with a slash
* (i.e. the resource is a collection).
* Gets the file name of this resource. See [HttpUtils.fileName] for details.
*/
fun fileName(): String {
val pathSegments = location.pathSegments()
return pathSegments[pathSegments.size - 1]
}
fun fileName() = HttpUtils.fileName(location)
/**
......
......@@ -55,6 +55,11 @@ class DavResponse private constructor(
body?.close()
}
/**
* Gets the file name of this resource. See [HttpUtils.fileName] for details.
*/
fun fileName() = HttpUtils.fileName(url)
/**
* Convenience method to get a certain property from the current response. Does't take
* members or related resources into consideration.
......
......@@ -6,6 +6,7 @@
package at.bitfire.dav4android
import okhttp3.HttpUrl
import okhttp3.Response
import java.util.*
import java.util.regex.Pattern
......@@ -14,6 +15,17 @@ object HttpUtils {
private val authSchemeWithParam = Pattern.compile("^([^ \"]+) +(.*)$")
/**
* Gets the resource name (the last segment of the path) from an URL.
*
* @return resource name or `` (empty string) if the URL ends with a slash
* (i.e. the resource is a collection).
*/
fun fileName(url: HttpUrl): String {
val pathSegments = url.pathSegments()
return pathSegments[pathSegments.size - 1]
}
fun listHeader(response: Response, name: String): Array<String> {
val value = response.headers(name).joinToString(",")
return value.split(',').filter { it.isNotEmpty() }.toTypedArray()
......
......@@ -47,15 +47,6 @@ class DavResourceTest {
private fun sampleUrl() = mockServer.url("/dav/")
@Test
fun testFilename() {
assertEquals("", DavResource(httpClient, sampleUrl().resolve("/")!!).fileName())
assertEquals("hier1", DavResource(httpClient, sampleUrl().resolve("/hier1")!!).fileName())
assertEquals("", DavResource(httpClient, sampleUrl().resolve("/hier1/")!!).fileName())
assertEquals("hier2", DavResource(httpClient, sampleUrl().resolve("/hier1/hier2")!!).fileName())
assertEquals("", DavResource(httpClient, sampleUrl().resolve("/hier1/hier2/")!!).fileName())
}
@Test
fun testOptions() {
val url = sampleUrl()
......
......@@ -6,11 +6,22 @@
package at.bitfire.dav4android
import okhttp3.HttpUrl
import org.junit.Assert.assertEquals
import org.junit.Test
class HttpUtilsTest {
@Test
fun testFilename() {
val sampleUrl = HttpUrl.parse("https://example.com")!!
assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/")!!))
assertEquals("hier1", HttpUtils.fileName(sampleUrl.resolve("/hier1")!!))
assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier1/")!!))
assertEquals("hier2", HttpUtils.fileName(sampleUrl.resolve("/hier2")!!))
assertEquals("", HttpUtils.fileName(sampleUrl.resolve("/hier2/")!!))
}
@Test
fun testParseWwwAuthenticate() {
// two schemes: one without param (illegal!), second with two params
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment