Loading src/main/java/at/bitfire/dav4android/DavResource.kt +13 −5 Original line number Diff line number Diff line Loading @@ -87,17 +87,21 @@ 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") Loading @@ -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 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) } } Loading src/test/java/at/bitfire/dav4android/DavResourceTest.kt +21 −14 Original line number Diff line number Diff line Loading @@ -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 */ Loading @@ -87,31 +86,37 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_CREATED)) var called = false DavResource(httpClient, url).let { dav -> dav.move(destination, false) { called = true } assertTrue(called) assertEquals(destination, dav.location) } 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) { DavResource(httpClient, url).let { dav -> dav.move(destination, true) { called = true } assertTrue(called) assertEquals(destination, dav.location) } 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 */ Loading @@ -122,8 +127,10 @@ class DavResourceTest { .setResponseCode(207)) try { called = false DavResource(httpClient, url).let { dav -> dav.move(destination, false) { called = true } fail("Expected HttpException") } } catch(e: HttpException) { assertFalse(called) } Loading Loading
src/main/java/at/bitfire/dav4android/DavResource.kt +13 −5 Original line number Diff line number Diff line Loading @@ -87,17 +87,21 @@ 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") Loading @@ -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 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) } } Loading
src/test/java/at/bitfire/dav4android/DavResourceTest.kt +21 −14 Original line number Diff line number Diff line Loading @@ -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 */ Loading @@ -87,31 +86,37 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_CREATED)) var called = false DavResource(httpClient, url).let { dav -> dav.move(destination, false) { called = true } assertTrue(called) assertEquals(destination, dav.location) } 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) { DavResource(httpClient, url).let { dav -> dav.move(destination, true) { called = true } assertTrue(called) assertEquals(destination, dav.location) } 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 */ Loading @@ -122,8 +127,10 @@ class DavResourceTest { .setResponseCode(207)) try { called = false DavResource(httpClient, url).let { dav -> dav.move(destination, false) { called = true } fail("Expected HttpException") } } catch(e: HttpException) { assertFalse(called) } Loading