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

add DavCollection.post()

parent 6ec5b007
Pipeline #163249218 passed with stages
in 1 minute and 57 seconds
......@@ -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.
*
......
......@@ -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)
/**
......
......@@ -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
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