Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

Move fileName() to HttpUtils

parent bc77df0f
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -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)


    /**
+5 −0
Original line number Diff line number Diff line
@@ -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.
+12 −0
Original line number Diff line number Diff line
@@ -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()
+0 −9
Original line number Diff line number Diff line
@@ -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()
+11 −0
Original line number Diff line number Diff line
@@ -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