Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 270e127

Browse files
theScrabiabelgardep
authored andcommitted
fix according to review
1 parent 84240ef commit 270e127

File tree

4 files changed

+119
-45
lines changed

4 files changed

+119
-45
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import com.owncloud.android.lib.common.OwnCloudClient
2828
import com.owncloud.android.lib.common.operations.RemoteOperation
2929
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3030
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
31+
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
32+
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
3133
import org.json.JSONException
3234
import timber.log.Timber
3335

34-
3536
/**
3637
* Checks if the server is valid
3738
*
@@ -41,14 +42,10 @@ import timber.log.Timber
4142
* @author Abel García de Prada
4243
*/
4344
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
44-
companion object {
45-
const val HTTPS_SCHEME = "https"
46-
const val HTTP_SCHEME = "http"
47-
}
4845

4946
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
5047
if (client.baseUri.scheme.isNullOrEmpty())
51-
client.baseUri = Uri.parse(HTTPS_SCHEME + "://" + client.baseUri.toString())
48+
client.baseUri = Uri.parse("$HTTPS_SCHEME://${client.baseUri}")
5249

5350
var result = tryToConnect(client)
5451
if (result.code != ResultCode.OK_SSL && !result.isSslRecoverableException) {
@@ -64,9 +61,9 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
6461
val baseUrl = client.baseUri.toString()
6562
client.setFollowRedirects(false)
6663
return try {
67-
val requestor = StatusRequestor()
68-
val requestResult = requestor.requestAndFollowRedirects(baseUrl, client)
69-
requestor.handleRequestResult(requestResult, baseUrl)
64+
val requester = StatusRequester()
65+
val requestResult = requester.requestAndFollowRedirects(baseUrl, client)
66+
requester.handleRequestResult(requestResult, baseUrl)
7067
} catch (e: JSONException) {
7168
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
7269
} catch (e: Exception) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
25+
package com.owncloud.android.lib.resources.status
26+
27+
object HttpScheme {
28+
const val HTTP_SCHEME = "http"
29+
const val HTTPS_SCHEME = "https"
30+
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequestor.kt renamed to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
125
package com.owncloud.android.lib.resources.status
226

327
import com.owncloud.android.lib.common.OwnCloudClient
428
import com.owncloud.android.lib.common.http.HttpConstants
529
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
630
import com.owncloud.android.lib.common.operations.RemoteOperationResult
31+
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
32+
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
733
import org.json.JSONObject
834
import java.net.URL
935
import java.util.concurrent.TimeUnit
1036

11-
internal class StatusRequestor {
12-
13-
companion object {
14-
/**
15-
* Maximum time to wait for a response from the server when the connection is being tested,
16-
* in MILLISECONDs.
17-
*/
18-
private const val TRY_CONNECTION_TIMEOUT: Long = 5000
19-
private const val NODE_INSTALLED = "installed"
20-
private const val HTTPS_SCHEME = "https"
21-
private const val HTTP_SCHEME = "http"
22-
private const val NODE_VERSION = "version"
23-
}
37+
internal class StatusRequester {
2438

2539
private fun checkIfConnectionIsRedirectedToNoneSecure(
2640
isConnectionSecure: Boolean,
2741
baseUrl: String,
2842
redirectedUrl: String
2943
): Boolean {
3044
return isConnectionSecure ||
31-
(baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(
32-
HTTP_SCHEME
33-
))
45+
(baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(HTTP_SCHEME))
3446
}
3547

3648
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
@@ -64,7 +76,7 @@ internal class StatusRequestor {
6476

6577
status = client.executeHttpMethod(getMethod)
6678
val result =
67-
if (isSuccess(status)) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
79+
if (status.isSuccess()) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
6880
else RemoteOperationResult(getMethod)
6981

7082
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
@@ -82,13 +94,13 @@ internal class StatusRequestor {
8294
}
8395
}
8496

85-
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
97+
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK
8698

8799
fun handleRequestResult(
88100
requestResult: RequestResult,
89101
baseUrl: String
90102
): RemoteOperationResult<OwnCloudVersion> {
91-
if (!isSuccess(requestResult.status))
103+
if (!requestResult.status.isSuccess())
92104
return RemoteOperationResult(requestResult.getMethod)
93105

94106
val respJSON = JSONObject(requestResult.getMethod.getResponseBodyAsString() ?: "")
@@ -110,4 +122,14 @@ internal class StatusRequestor {
110122
result.data = ocVersion
111123
return result
112124
}
113-
}
125+
126+
companion object {
127+
/**
128+
* Maximum time to wait for a response from the server when the connection is being tested,
129+
* in MILLISECONDs.
130+
*/
131+
private const val TRY_CONNECTION_TIMEOUT: Long = 5000
132+
private const val NODE_INSTALLED = "installed"
133+
private const val NODE_VERSION = "version"
134+
}
135+
}
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,67 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
125
package com.owncloud.android.lib
226

3-
import com.owncloud.android.lib.resources.status.StatusRequestor
27+
import com.owncloud.android.lib.resources.status.StatusRequester
428
import org.junit.Assert.assertEquals
529
import org.junit.Test
630

731
class StatusRequestorTest {
8-
private val requestor = StatusRequestor()
32+
private val requestor = StatusRequester()
933

1034
@Test
1135
fun `update location with an absolute path`() {
12-
val newLocation = requestor.updateLocationWithRedirectPath(
13-
"https://cloud.somewhere.com", "https://cloud.somewhere.com/subdir"
14-
)
15-
assertEquals("https://cloud.somewhere.com/subdir", newLocation)
36+
val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, "$TEST_DOMAIN/subdir")
37+
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
1638
}
1739

1840
@Test
1941

20-
fun `update location with a smaler aboslute path`() {
21-
val newLocation = requestor.updateLocationWithRedirectPath(
22-
"https://cloud.somewhere.com/subdir", "https://cloud.somewhere.com/"
23-
)
24-
assertEquals("https://cloud.somewhere.com/", newLocation)
42+
fun `update location with a smaller absolute path`() {
43+
val newLocation = requestor.updateLocationWithRedirectPath("$TEST_DOMAIN$SUB_PATH", TEST_DOMAIN)
44+
assertEquals(TEST_DOMAIN, newLocation)
2545
}
2646

2747
@Test
2848
fun `update location with a relative path`() {
2949
val newLocation = requestor.updateLocationWithRedirectPath(
30-
"https://cloud.somewhere.com", "/subdir"
50+
TEST_DOMAIN, SUB_PATH
3151
)
32-
assertEquals("https://cloud.somewhere.com/subdir", newLocation)
52+
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
3353
}
3454

3555
@Test
3656
fun `update location by replacing the relative path`() {
3757
val newLocation = requestor.updateLocationWithRedirectPath(
38-
"https://cloud.somewhere.com/some/other/subdir", "/subdir"
58+
"$TEST_DOMAIN/some/other/subdir", SUB_PATH
3959
)
40-
assertEquals("https://cloud.somewhere.com/subdir", newLocation)
60+
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
61+
}
62+
63+
companion object {
64+
const val TEST_DOMAIN = "https://cloud.somewhere.com"
65+
const val SUB_PATH = "/subdir"
4166
}
42-
}
67+
}

0 commit comments

Comments
 (0)