Commit 6c663582 authored by Ricki Hirner's avatar Ricki Hirner

MOVE: use HttpUrl for destination, update location on success

parent 8a150270
......@@ -87,19 +87,23 @@ open class DavResource @JvmOverloads constructor(
/**
* Sends a MOVE request to this resource. Follows up to [MAX_REDIRECTS] redirects.
* Updates [location] on success.
*
* @param destination where the resource shall be moved 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 move(destination:String, forceOverride:Boolean, callback: (response: Response) -> Unit) {
fun move(destination: HttpUrl, forceOverride: Boolean, callback: (response: Response) -> Unit) {
val requestBuilder = Request.Builder()
.method("MOVE", null)
.header("Content-Length", "0")
.header("Destination", destination);
.header("Destination", destination.toString())
if(forceOverride) requestBuilder.header("Overwrite", "F")
if (forceOverride) requestBuilder.header("Overwrite", "F")
followRedirects {
requestBuilder.url(location)
......@@ -108,13 +112,17 @@ open class DavResource @JvmOverloads constructor(
.execute()
}.use { response ->
checkStatus(response)
if (response.code() == 207)
/* Multiple resources were to be affected by the MOVE, but errors on some
/* Multiple resources were to be affected by the MOVE, but errors on some
of them prevented the operation from taking place.
[_] (RFC 4918 9.9.4. Status Codes for MOVE Method) */
throw HttpException(response)
// update location
location.resolve(response.header("Location") ?: destination.toString())?.let {
location = it
}
callback(response)
}
}
......
......@@ -78,8 +78,7 @@ class DavResourceTest {
@Test
fun testMove() {
val url = sampleUrl()
val dav = DavResource(httpClient, url)
val destination = "$url/test";
val destination = url.resolve("test")!!
/* POSITIVE TEST CASES */
......@@ -87,31 +86,37 @@ class DavResourceTest {
mockServer.enqueue(MockResponse()
.setResponseCode(HttpURLConnection.HTTP_CREATED))
var called = false
dav.move(destination, false) {
called = true
DavResource(httpClient, url).let { dav ->
dav.move(destination, false) {
called = true
}
assertTrue(called)
assertEquals(destination, dav.location)
}
assertTrue(called)
var rq = mockServer.takeRequest()
assertEquals("MOVE", rq.method)
assertEquals(url.encodedPath(), rq.path)
assertEquals(destination, rq.getHeader("Destination"))
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertNull(rq.getHeader("Overwrite"))
// no preconditions, 204 No content, URL already mapped
// no preconditions, 204 No content, URL already mapped, overwrite
mockServer.enqueue(MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NO_CONTENT))
called = false
dav.move(destination, false) {
called = true
DavResource(httpClient, url).let { dav ->
dav.move(destination, true) {
called = true
}
assertTrue(called)
assertEquals(destination, dav.location)
}
assertTrue(called)
rq = mockServer.takeRequest()
assertEquals("MOVE", rq.method)
assertEquals(url.encodedPath(), rq.path)
assertEquals(destination, rq.getHeader("Destination"))
assertNull(rq.getHeader("Overwrite"))
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertEquals("F", rq.getHeader("Overwrite"))
/* NEGATIVE TEST CASES */
......@@ -122,8 +127,10 @@ class DavResourceTest {
.setResponseCode(207))
try {
called = false
dav.move(destination, false) { called = true }
fail("Expected HttpException")
DavResource(httpClient, url).let { dav ->
dav.move(destination, false) { called = true }
fail("Expected HttpException")
}
} catch(e: HttpException) {
assertFalse(called)
}
......
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