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

Commit 8f7b0821 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Merge branch 'ownclouders/dav4android-move_method_support'

parents 10abc3ec 6c663582
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -85,6 +85,48 @@ 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: HttpUrl, forceOverride: Boolean, callback: (response: Response) -> Unit) {
        val requestBuilder = Request.Builder()
                .method("MOVE", 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 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)
        }
    }

    /**
     * Sends a MKCOL request to this resource. Follows up to [MAX_REDIRECTS] redirects.
     *
+61 −0
Original line number Diff line number Diff line
@@ -75,6 +75,67 @@ class DavResourceTest {
        assertTrue(called)
    }

    @Test
    fun testMove() {
        val url = sampleUrl()
        val destination = url.resolve("test")!!

        /* POSITIVE TEST CASES */

        // no preconditions, 201 Created, new URL mapping at the destination
        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.toString(), rq.getHeader("Destination"))
        assertNull(rq.getHeader("Overwrite"))

        // no preconditions, 204 No content, URL already mapped, overwrite
        mockServer.enqueue(MockResponse()
                .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT))
        called = 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.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 MOVE prevented the operation from taking place)

        mockServer.enqueue(MockResponse()
                .setResponseCode(207))
        try {
            called = false
            DavResource(httpClient, url).let { dav ->
                dav.move(destination, false) { called = true }
                fail("Expected HttpException")
            }
        } catch(e: HttpException) {
            assertFalse(called)
        }
    }

    @Test
    fun testGet() {
        val url = sampleUrl()