mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-30 01:48:14 +00:00 
			
		
		
		
	fix ip address without http prefix not recognized
This commit is contained in:
		
							parent
							
								
									b207fd1c67
								
							
						
					
					
						commit
						91c7900d5c
					
				| @ -0,0 +1,118 @@ | ||||
| package com.owncloud.android.lib | ||||
| 
 | ||||
| import android.net.Uri | ||||
| import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_PREFIX | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_PREFIX | ||||
| import org.junit.Assert.assertEquals | ||||
| import org.junit.Assert.assertFalse | ||||
| import org.junit.Assert.assertTrue | ||||
| import org.junit.Test | ||||
| 
 | ||||
| class GetRemoteStatusOperationTest { | ||||
| 
 | ||||
|     @Test | ||||
|     fun urlStartingWithHttpMustBeDetectedAsSuch() { | ||||
|         assertTrue(GetRemoteStatusOperation.usesHttpOrHttps(Uri.parse(HTTP_SOME_OWNCLOUD))) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun urlStartingWithHttpsMustBeDetectedAsSuch() { | ||||
|         assertTrue(GetRemoteStatusOperation.usesHttpOrHttps(Uri.parse(HTTPS_SOME_OWNCLOUD))) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun incompleteUrlWithoutHttpsOrHttpSchemeMustBeDetectedAsSuch() { | ||||
|         assertFalse(GetRemoteStatusOperation.usesHttpOrHttps(Uri.parse(SOME_OWNCLOUD))) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun completeUrlWithHttpMustBeReturnedAsSuch() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTP_SOME_OWNCLOUD), | ||||
|             GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(HTTP_SOME_OWNCLOUD)) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun completeUrlWithHttpsMustBeReturnedAsSuch() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTPS_SOME_OWNCLOUD), | ||||
|             GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(HTTPS_SOME_OWNCLOUD)) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun incompleteUrlWithoutHttpPrefixMustBeConvertedToProperUrlWithHttpsPrefix() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTPS_SOME_OWNCLOUD), | ||||
|             GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(SOME_OWNCLOUD)) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun completeUrlWithSubdirAndHttpsMustBeReturnedAsSuch() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), GetRemoteStatusOperation.buildFullHttpsUrl( | ||||
|                 Uri.parse( | ||||
|                     HTTPS_SOME_OWNCLOUD_WITH_SUBDIR | ||||
|                 ) | ||||
|             ) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun incompleteUrlWithSubdirAndWithoutHttpPrefixMustBeConvertedToProperUrlWithHttpsPrefix() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTPS_SOME_OWNCLOUD_WITH_SUBDIR), GetRemoteStatusOperation.buildFullHttpsUrl( | ||||
|                 Uri.parse( | ||||
|                     SOME_OWNCLOUD_WITH_SUBDIR | ||||
|                 ) | ||||
|             ) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun ipMustBeConvertedToProperUrl() { | ||||
|         assertEquals(Uri.parse(HTTPS_SOME_IP), GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(SOME_IP))) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun urlContainingIpAndHttpPrefixMustBeReturnedAsSuch() { | ||||
|         assertEquals(Uri.parse(HTTP_SOME_IP), GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(HTTP_SOME_IP))) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun ipAndPortMustBeConvertedToProperUrl() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTPS_SOME_IP_WITH_PORT), | ||||
|             GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(SOME_IP_WITH_PORT)) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun urlContainingIpAndPortAndHttpPrefixMustBeReturnedAsSuch() { | ||||
|         assertEquals( | ||||
|             Uri.parse(HTTP_SOME_IP_WITH_PORT), | ||||
|             GetRemoteStatusOperation.buildFullHttpsUrl(Uri.parse(HTTP_SOME_IP_WITH_PORT)) | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
|     companion object { | ||||
|         val SOME_OWNCLOUD = "some_owncloud.com" | ||||
|         val HTTP_SOME_OWNCLOUD = "$HTTP_PREFIX$SOME_OWNCLOUD" | ||||
|         val HTTPS_SOME_OWNCLOUD = "$HTTPS_PREFIX$SOME_OWNCLOUD" | ||||
| 
 | ||||
|         val SOME_OWNCLOUD_WITH_SUBDIR = "some_owncloud.com/subdir" | ||||
|         val HTTP_SOME_OWNCLOUD_WITH_SUBDIR = "$HTTP_PREFIX$SOME_OWNCLOUD_WITH_SUBDIR" | ||||
|         val HTTPS_SOME_OWNCLOUD_WITH_SUBDIR = "$HTTPS_PREFIX$SOME_OWNCLOUD_WITH_SUBDIR" | ||||
| 
 | ||||
|         val SOME_IP = "184.123.185.12" | ||||
|         val HTTP_SOME_IP = "$HTTP_PREFIX$SOME_IP" | ||||
|         val HTTPS_SOME_IP = "$HTTPS_PREFIX$SOME_IP" | ||||
| 
 | ||||
|         val SOME_IP_WITH_PORT = "184.123.185.12:5678" | ||||
|         val HTTP_SOME_IP_WITH_PORT = "$HTTP_PREFIX$SOME_IP_WITH_PORT" | ||||
|         val HTTPS_SOME_IP_WITH_PORT = "$HTTPS_PREFIX$SOME_IP_WITH_PORT" | ||||
|     } | ||||
| } | ||||
| @ -32,26 +32,25 @@ class StatusRequestorTest { | ||||
|     private val requestor = StatusRequester() | ||||
| 
 | ||||
|     @Test | ||||
|     fun `update location with an absolute path`() { | ||||
|     fun testUpdateLocationWithAnAbsolutePath() { | ||||
|         val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, "$TEST_DOMAIN$SUB_PATH") | ||||
|         assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| 
 | ||||
|     fun `update location with a smaller absolute path`() { | ||||
|     fun updateLocationWithASmallerAbsolutePath() { | ||||
|         val newLocation = requestor.updateLocationWithRedirectPath("$TEST_DOMAIN$SUB_PATH", TEST_DOMAIN) | ||||
|         assertEquals(TEST_DOMAIN, newLocation) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun `update location with a relative path`() { | ||||
|     fun updateLocationWithARelativePath() { | ||||
|         val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, SUB_PATH) | ||||
|         assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun `update location by replacing the relative path`() { | ||||
|     fun updateLocationByReplacingTheRelativePath() { | ||||
|         val newLocation = requestor.updateLocationWithRedirectPath( | ||||
|             "$TEST_DOMAIN/some/other/subdir", SUB_PATH | ||||
|         ) | ||||
| @ -28,7 +28,9 @@ import com.owncloud.android.lib.common.OwnCloudClient | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_PREFIX | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_PREFIX | ||||
| import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME | ||||
| import org.json.JSONException | ||||
| import timber.log.Timber | ||||
| @ -44,8 +46,7 @@ import timber.log.Timber | ||||
| class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { | ||||
|         if (client.baseUri.scheme.isNullOrEmpty()) | ||||
|             client.baseUri = Uri.parse("$HTTPS_SCHEME://${client.baseUri}") | ||||
|         client.baseUri = buildFullHttpsUrl(client.baseUri) | ||||
| 
 | ||||
|         var result = tryToConnect(client) | ||||
|         if (result.code != ResultCode.OK_SSL && !result.isSslRecoverableException) { | ||||
| @ -70,4 +71,16 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | ||||
|             RemoteOperationResult(e) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     companion object { | ||||
|         fun usesHttpOrHttps(uri: Uri) = | ||||
|             uri.toString().startsWith(HTTPS_PREFIX) || uri.toString().startsWith(HTTP_PREFIX) | ||||
| 
 | ||||
|         fun buildFullHttpsUrl(baseUri: Uri): Uri { | ||||
|             if (usesHttpOrHttps(baseUri)) { | ||||
|                 return baseUri | ||||
|             } | ||||
|             return Uri.parse("$HTTPS_PREFIX$baseUri") | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -27,4 +27,6 @@ package com.owncloud.android.lib.resources.status | ||||
| object HttpScheme { | ||||
|     const val HTTP_SCHEME = "http" | ||||
|     const val HTTPS_SCHEME = "https" | ||||
|     const val HTTP_PREFIX = "$HTTP_SCHEME://" | ||||
|     const val HTTPS_PREFIX = "$HTTPS_SCHEME://" | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user