Commit 3911d771 authored by Ricki Hirner's avatar Ricki Hirner

Merge branch 'copy_method_support' into 'master'

Include copy method with corresponding tests

See merge request bitfireAT/dav4android!3
parents 8f7b0821 d2e1024c
Pipeline #31350114 passed with stages
in 6 minutes and 4 seconds
......@@ -127,6 +127,43 @@ open class DavResource @JvmOverloads constructor(
}
}
/**
* Sends a COPY request for this resource. Follows up to [MAX_REDIRECTS] redirects.
*
* @param destination where the resource shall be copied to
* @param forceOverride whether resources are overwritten when they already exist in destination
*
* @throws IOException on I/O error
* @throws HttpException on HTTP error
* @throws DavException on WebDAV error
*/
@Throws(IOException::class, HttpException::class, DavException::class)
fun copy(destination:HttpUrl, forceOverride:Boolean, callback: (response: Response) -> Unit) {
val requestBuilder = Request.Builder()
.method("COPY", null)
.header("Content-Length", "0")
.header("Destination", destination.toString())
if(forceOverride) requestBuilder.header("Overwrite", "F")
followRedirects {
requestBuilder.url(location)
httpClient.newCall(requestBuilder
.build())
.execute()
}.use{ response ->
checkStatus(response)
if (response.code() == 207)
/* Multiple resources were to be affected by the COPY, but errors on some
of them prevented the operation from taking place.
[_] (RFC 4918 9.8.5. Status Codes for COPY Method) */
throw HttpException(response)
callback(response)
}
}
/**
* Sends a MKCOL request to this resource. Follows up to [MAX_REDIRECTS] redirects.
*
......
......@@ -136,6 +136,66 @@ class DavResourceTest {
}
}
@Test
fun testCopy() {
val url = sampleUrl()
val destination = url.resolve("test")!!
/* POSITIVE TEST CASES */
// no preconditions, 201 Created, resulted in the creation of a new resource
mockServer.enqueue(MockResponse()
.setResponseCode(HttpURLConnection.HTTP_CREATED))
var called = false
DavResource(httpClient, url).let { dav ->
dav.copy(destination, false) {
called = true
}
assertTrue(called)
}
var rq = mockServer.takeRequest()
assertEquals("COPY", rq.method)
assertEquals(url.encodedPath(), rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertNull(rq.getHeader("Overwrite"))
// no preconditions, 204 No content, resource successfully copied to a preexisting
// destination resource
mockServer.enqueue(MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NO_CONTENT))
called = false
DavResource(httpClient, url).let { dav ->
dav.copy(destination, true) {
called = true
}
assertTrue(called)
}
rq = mockServer.takeRequest()
assertEquals("COPY", rq.method)
assertEquals(url.encodedPath(), rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertEquals("F", rq.getHeader("Overwrite"))
/* NEGATIVE TEST CASES */
// 207 multi-status (e.g. errors on some of resources affected by
// the COPY prevented the operation from taking place)
mockServer.enqueue(MockResponse()
.setResponseCode(207))
try {
called = false
DavResource(httpClient, url).let { dav ->
dav.copy(destination, false) { called = true }
fail("Expected HttpException")
}
} catch(e: HttpException) {
assertFalse(called)
}
}
@Test
fun testGet() {
val url = sampleUrl()
......
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