diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 24ef6e1c..3c9f5417 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -15,6 +15,8 @@ dependencies { exclude module: "kotlin-reflect" } kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion" + + testImplementation "junit:junit:4.12" } allOpen { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt index 0a4e3654..bab89609 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt @@ -68,7 +68,7 @@ class GetRemoteStatusOperation : RemoteOperation() { return latestResult } - private fun updateLocationWithRelativePath(oldLocation: String, redirectedLocation: String): String { + fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String { if(!redirectedLocation.startsWith("/")) return redirectedLocation val oldLocation = URL(oldLocation) @@ -97,7 +97,7 @@ class GetRemoteStatusOperation : RemoteOperation() { return successfulConnection } - var redirectedLocation = updateLocationWithRelativePath(baseUrlStr, latestResult.redirectedLocation) + var redirectedLocation = updateLocationWithRedirectPath(baseUrlStr, latestResult.redirectedLocation) while (!redirectedLocation.isNullOrEmpty() && !latestResult.isSuccess) { isRedirectToNonSecureConnection = isRedirectToNonSecureConnection || @@ -112,7 +112,7 @@ class GetRemoteStatusOperation : RemoteOperation() { status = client.executeHttpMethod(getMethod) latestResult = RemoteOperationResult(getMethod) - redirectedLocation = updateLocationWithRelativePath(redirectedLocation, latestResult.redirectedLocation) + redirectedLocation = updateLocationWithRedirectPath(redirectedLocation, latestResult.redirectedLocation) } if (isSuccess(status)) { diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt new file mode 100644 index 00000000..2f3b3d8b --- /dev/null +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/GetRemoteStatusOperationTest.kt @@ -0,0 +1,42 @@ +package com.owncloud.android.lib + +import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation +import org.junit.Assert.assertEquals +import org.junit.Test + +class GetRemoteStatusOperationTest { + private val remoteStatusOperation = GetRemoteStatusOperation() + + @Test + fun `update location with an absolute path`() { + val newLocation = remoteStatusOperation.updateLocationWithRedirectPath( + "https://cloud.somewhere.com", "https://cloud.somewhere.com/subdir" + ) + assertEquals("https://cloud.somewhere.com/subdir", newLocation) + } + + @Test + fun `update location with a smaler aboslute path`() { + + val newLocation = remoteStatusOperation.updateLocationWithRedirectPath( + "https://cloud.somewhere.com/subdir", "https://cloud.somewhere.com/" + ) + assertEquals("https://cloud.somewhere.com/", newLocation) + } + + @Test + fun `update location with a relative path`() { + val newLocation = remoteStatusOperation.updateLocationWithRedirectPath( + "https://cloud.somewhere.com", "/subdir" + ) + assertEquals("https://cloud.somewhere.com/subdir", newLocation) + } + + @Test + fun `update location by replacing the relative path`() { + val newLocation = remoteStatusOperation.updateLocationWithRedirectPath( + "https://cloud.somewhere.com/some/other/subdir", "/subdir" + ) + assertEquals("https://cloud.somewhere.com/subdir", newLocation) + } +} \ No newline at end of file