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

Unverified Commit b3f19017 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Add tests

skip-checks: false
parent 9aafb6c8
Loading
Loading
Loading
Loading
+46 −41
Original line number Diff line number Diff line
@@ -71,6 +71,51 @@ open class DavResource @JvmOverloads constructor(
        val HREF = Property.Name(XmlUtils.NS_WEBDAV, "href")

        val XML_SIGNATURE = "<?xml".toByteArray()


        /**
         * Creates a request body for the PROPPATCH request.
         */
        internal fun createProppatchXml(
            setProperties: Map<Property.Name, String>,
            removeProperties: List<Property.Name>
        ): String {
            // build XML request body
            val serializer = XmlUtils.newSerializer()
            val writer = StringWriter()
            serializer.setOutput(writer)
            serializer.setPrefix("d", XmlUtils.NS_WEBDAV)
            serializer.startDocument("UTF-8", null)
            serializer.insertTag(PROPERTYUPDATE) {
                // DAV:set
                if (setProperties.isNotEmpty()) {
                    serializer.insertTag(SET) {
                        for (prop in setProperties) {
                            serializer.insertTag(PROP) {
                                serializer.insertTag(prop.key) {
                                    text(prop.value)
                                }
                            }
                        }
                    }
                }

                // DAV:remove
                if (removeProperties.isNotEmpty()) {
                    serializer.insertTag(REMOVE) {
                        for (prop in removeProperties) {
                            insertTag(PROP) {
                                insertTag(prop)
                            }
                        }
                    }
                }
            }

            serializer.endDocument()
            return writer.toString()
        }

    }

    /**
@@ -477,7 +522,7 @@ open class DavResource @JvmOverloads constructor(
        callback: (at.bitfire.dav4jvm.Response, at.bitfire.dav4jvm.Response.HrefRelation) -> Unit
    ) {
        followRedirects {
            val rqBody = createPropPatchXml(setProperties, removeProperties)
            val rqBody = createProppatchXml(setProperties, removeProperties)

            httpClient.newCall(
                Request.Builder()
@@ -493,46 +538,6 @@ open class DavResource @JvmOverloads constructor(
        }
    }

    private fun createPropPatchXml(
        setProperties: Map<Property.Name, String>,
        removeProperties: List<Property.Name>
    ): String {
        // build XML request body
        val serializer = XmlUtils.newSerializer()
        val writer = StringWriter()
        serializer.setOutput(writer)
        serializer.setPrefix("d", XmlUtils.NS_WEBDAV)
        serializer.startDocument("UTF-8", null)
        serializer.insertTag(PROPERTYUPDATE) {
            // DAV:set
            if (setProperties.isNotEmpty()) {
                serializer.insertTag(SET) {
                    for (prop in setProperties) {
                        serializer.insertTag(PROP) {
                            serializer.insertTag(prop.key) {
                                text(prop.value)
                            }
                        }
                    }
                }
            }

            // DAV:remove
            if (removeProperties.isNotEmpty()) {
                serializer.insertTag(REMOVE) {
                    for (prop in removeProperties) {
                        insertTag(PROP) {
                            insertTag(prop)
                        }
                    }
                }
            }
        }

        serializer.endDocument()
        return writer.toString()
    }

    /**
     * Sends a SEARCH request (RFC 5323) with the given body to the server.
     *
+51 −13
Original line number Diff line number Diff line
@@ -392,19 +392,6 @@ class DavResourceTest {
        assertTrue(called)
    }

    @Test
    fun testProppatch() {
        val url = sampleUrl()
        val dav = DavResource(httpClient, url)

        dav.proppatch(
            setProperties = mapOf(Pair(Property.Name("sample", "setThis"), "Some Value")),
            removeProperties = listOf(Property.Name("sample", "removeThis"))
        ) { response, hrefRelation ->
            
        }
    }

    @Test
    fun testPropfindAndMultiStatus() {
        val url = sampleUrl()
@@ -747,6 +734,57 @@ class DavResourceTest {
        assertTrue(called)
    }

    @Test
    fun testProppatch() {
        val url = sampleUrl()
        val dav = DavResource(httpClient, url)

        // multi-status response with <response>/<propstat> elements
        mockServer.enqueue(MockResponse()
            .setResponseCode(207)
            .setHeader("Content-Type", "application/xml; charset=utf-8")
            .setBody("<multistatus xmlns='DAV:' xmlns:s='sample'>" +
                    "  <response>" +
                    "    <href>/dav</href>" +
                    "    <propstat>" +
                    "      <prop>" +
                    "         <s:setThis>Some Value</s:setThis>" +
                    "      </prop>" +
                    "      <status>HTTP/1.1 200 OK</status>" +
                    "    </propstat>" +
                    "    <propstat>" +
                    "      <prop>" +
                    "         <s:removeThis/>" +
                    "      </prop>" +
                    "      <status>HTTP/1.1 404 Not Found</status>" +
                    "    </propstat>" +
                    "  </response>" +
                    "</multistatus>"))

        var called = false
        dav.proppatch(
            setProperties = mapOf(Pair(Property.Name("sample", "setThis"), "Some Value")),
            removeProperties = listOf(Property.Name("sample", "removeThis"))
        ) { response, hrefRelation ->
            called = true
            assertEquals(Response.HrefRelation.SELF, hrefRelation)
        }
        assertTrue(called)
    }

    @Test
    fun testProppatch_createProppatchXml() {
        val xml = DavResource.createProppatchXml(
            setProperties = mapOf(Pair(Property.Name("sample", "setThis"), "Some Value")),
            removeProperties = listOf(Property.Name("sample", "removeThis"))
        )
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<d:propertyupdate xmlns:d=\"DAV:\">" +
                    "<d:set><d:prop><n1:setThis xmlns:n1=\"sample\">Some Value</n1:setThis></d:prop></d:set>" +
                    "<d:remove><d:prop><n2:removeThis xmlns:n2=\"sample\" /></d:prop></d:remove>" +
                "</d:propertyupdate>", xml)
    }

    @Test
    fun testPut() {
        val url = sampleUrl()