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

Commit 5abed623 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

add DavCollection.post()

parent 6ec5b007
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -10,10 +10,10 @@ import at.bitfire.dav4jvm.XmlUtils.insertTag
import at.bitfire.dav4jvm.exception.DavException
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.property.SyncToken
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import java.io.IOException
import java.io.StringWriter
import java.util.logging.Logger

@@ -33,6 +33,27 @@ open class DavCollection @JvmOverloads constructor(
        val NRESULTS = Property.Name(XmlUtils.NS_WEBDAV, "nresults")
    }

    /**
     * Sends a POST request. Primarily intended to be used with an Add-Member URL (RFC 5995).
     */
    @Throws(IOException::class, HttpException::class)
    fun post(body: RequestBody, ifNoneMatch: Boolean = false, callback: (Response) -> Unit) {
        followRedirects {
            val builder = Request.Builder()
                    .post(body)
                    .url(location)

            if (ifNoneMatch)
                // don't overwrite anything existing
                builder.header("If-None-Match", "*")

            httpClient.newCall(builder.build()).execute()
        }.use { response ->
            checkStatus(response)
            callback(response)
        }
    }

    /**
     * Sends a REPORT sync-collection request.
     *
+1 −1
Original line number Diff line number Diff line
@@ -349,7 +349,7 @@ open class DavResource @JvmOverloads constructor(
     *
     * @throws HttpException in case of an HTTP error
     */
    private fun checkStatus(response: Response) =
    protected fun checkStatus(response: Response) =
            checkStatus(response.code, response.message, response)

    /**
+21 −0
Original line number Diff line number Diff line
@@ -9,16 +9,21 @@ package at.bitfire.dav4jvm
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.property.GetETag
import at.bitfire.dav4jvm.property.SyncToken
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import java.net.HttpURLConnection

class DavCollectionTest {

    private val sampleText = "SAMPLE RESPONSE"

    private val httpClient = OkHttpClient.Builder()
            .followRedirects(false)
            .build()
@@ -223,4 +228,20 @@ class DavCollectionTest {
        }
    }

    @Test
    fun testPost() {
        val url = sampleUrl()
        val dav = DavCollection(httpClient, url)

        // 201 Created
        mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_CREATED))
        var called = false
        dav.post(sampleText.toRequestBody("text/plain".toMediaType())) { response ->
            assertEquals("POST", mockServer.takeRequest().method)
            assertEquals(response.request.url, dav.location)
            called = true
        }
        assertTrue(called)
    }

}
 No newline at end of file