Skip to content

Commit

Permalink
Part of the query is removed when URL is passed in Intent (#344)
Browse files Browse the repository at this point in the history
* Reproduced failing test

Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>

* Fixed stripUrl

Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>

---------

Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
  • Loading branch information
ArnyminerZ committed Jun 13, 2024
1 parent dde075d commit 3acbdfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/at/bitfire/icsdroid/UriUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ object UriUtils {

return false
}

/**
* Strips the URL from a string. For example, the following string:
* ```
* "This is a URL: https://example.com"
* ```
* will return:
* ```
* "https://example.com"
* ```
* _Quotes are not included_
* @return The URL found in the string
* @throws IllegalArgumentException if no URL is found in the string
*/
fun String.stripUrl(): String? {
return "([a-zA-Z]+)://(\\w+)(.\\w+)*[\\w.&?=*]*".toRegex()
.find(this)
?.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.platform.LocalContext
import at.bitfire.icsdroid.Constants
import at.bitfire.icsdroid.HttpClient
import at.bitfire.icsdroid.R
import at.bitfire.icsdroid.UriUtils.stripUrl
import at.bitfire.icsdroid.calendar.LocalCalendar
import at.bitfire.icsdroid.model.CreateSubscriptionModel
import at.bitfire.icsdroid.model.CredentialsModel
Expand Down Expand Up @@ -148,23 +149,4 @@ class AddCalendarActivity : AppCompatActivity() {
HttpClient.setForeground(true)
}

/**
* Strips the URL from a string. For example, the following string:
* ```
* "This is a URL: https://example.com"
* ```
* will return:
* ```
* "https://example.com"
* ```
* _Quotes are not included_
* @return The URL found in the string
* @throws IllegalArgumentException if no URL is found in the string
*/
private fun String.stripUrl(): String? {
return "([a-zA-Z]+)://(\\w+)(.\\w+)*[/\\w*]*".toRegex()
.find(this)
?.value
}

}
14 changes: 14 additions & 0 deletions app/src/test/kotlin/at/bitfire/icsdroid/UrlUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package at.bitfire.icsdroid

import at.bitfire.icsdroid.UriUtils.stripUrl
import org.junit.Assert.assertEquals
import org.junit.Test

class UrlUtilsTest {
@Test
fun testStripUrl() {
val url = "This is a URL: https://example.com/more/and/more?query=true.&par_am=test.more"
val strippedUrl = url.stripUrl()
assertEquals("https://example.com/more/and/more?query=true.&par_am=test.more", strippedUrl)
}
}

0 comments on commit 3acbdfc

Please sign in to comment.