summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-07-03 11:38:46 -0300
committerTorsten Grote <t@grobox.de>2020-07-03 11:38:46 -0300
commit42190ddd43178e40725a6ae3e604fa0e3bede432 (patch)
tree042b3ffd97fc8450365c2dcb9aa2f75769a40a99 /src
parent4a712e1da5bb66d7a7e27271e65e09d4910e2339 (diff)
downloadwallet-kotlin-42190ddd43178e40725a6ae3e604fa0e3bede432.tar.gz
wallet-kotlin-42190ddd43178e40725a6ae3e604fa0e3bede432.tar.bz2
wallet-kotlin-42190ddd43178e40725a6ae3e604fa0e3bede432.zip
Add Taler withdraw URI parsing with test
Diffstat (limited to 'src')
-rw-r--r--src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt25
-rw-r--r--src/commonTest/kotlin/net/taler/wallet/kotlin/TalerUriTest.kt41
2 files changed, 66 insertions, 0 deletions
diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt b/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt
new file mode 100644
index 0000000..068b271
--- /dev/null
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt
@@ -0,0 +1,25 @@
+package net.taler.wallet.kotlin
+
+internal object TalerUri {
+
+ private const val SCHEME = "taler://"
+ private const val AUTHORITY_PAY = "pay"
+ private const val AUTHORITY_WITHDRAW = "withdraw"
+ private const val AUTHORITY_REFUND = "refund"
+ private const val AUTHORITY_TIP = "tip"
+
+ /**
+ * Parses a withdraw URI and returns a bank status URL or null if the URI was invalid.
+ */
+ fun parseWithdrawUri(uri: String): String? {
+ val prefix = "${SCHEME}${AUTHORITY_WITHDRAW}"
+ if (!uri.startsWith(prefix, ignoreCase = true)) return null
+ val parts = uri.substring(prefix.length + 1).split('/')
+ if (parts.size != 3) return null
+ val (host, query, withdrawId) = parts
+ // TODO clarify what query is and if it can include '/' (docs seem out of date)
+ val urlQuery = if (query == "-") "api/withdraw-operation" else query
+ return "https://${host.toLowerCase()}/$urlQuery/$withdrawId"
+ }
+
+}
diff --git a/src/commonTest/kotlin/net/taler/wallet/kotlin/TalerUriTest.kt b/src/commonTest/kotlin/net/taler/wallet/kotlin/TalerUriTest.kt
new file mode 100644
index 0000000..bb4e633
--- /dev/null
+++ b/src/commonTest/kotlin/net/taler/wallet/kotlin/TalerUriTest.kt
@@ -0,0 +1,41 @@
+package net.taler.wallet.kotlin
+
+import net.taler.wallet.kotlin.TalerUri.parseWithdrawUri
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertNull
+
+class TalerUriTest {
+
+ @Test
+ fun testParseWithdrawUri() {
+ // correct parsing
+ var uri = "taler://withdraw/bank.example.com/-/12345"
+ assertEquals("https://bank.example.com/api/withdraw-operation/12345", parseWithdrawUri(uri))
+
+ // correct parsing with custom query
+ uri = "taler://withdraw/bank.example.com/foo/12345"
+ assertEquals("https://bank.example.com/foo/12345", parseWithdrawUri(uri))
+
+ // rejects incorrect scheme
+ uri = "talerx://withdraw/bank.example.com/-/12345"
+ assertNull(parseWithdrawUri(uri))
+
+ // rejects incorrect authority
+ uri = "taler://withdrawx/bank.example.com/-/12345"
+ assertNull(parseWithdrawUri(uri))
+
+ // rejects incorrect authority
+ uri = "taler://withdrawx/bank.example.com/-/12345"
+ assertNull(parseWithdrawUri(uri))
+
+ // rejects too few parts
+ uri = "taler://withdraw/bank.example.com/foo"
+ assertNull(parseWithdrawUri(uri))
+
+ // rejects too many parts
+ uri = "taler://withdraw/bank.example.com/-/12345/foo"
+ assertNull(parseWithdrawUri(uri))
+ }
+
+}