[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-kotlin] 01/02: Add Taler withdraw URI parsing with test
From: |
gnunet |
Subject: |
[taler-wallet-kotlin] 01/02: Add Taler withdraw URI parsing with test |
Date: |
Fri, 03 Jul 2020 16:44:12 +0200 |
This is an automated email from the git hooks/post-receive script.
torsten-grote pushed a commit to branch master
in repository wallet-kotlin.
commit 42190ddd43178e40725a6ae3e604fa0e3bede432
Author: Torsten Grote <t@grobox.de>
AuthorDate: Fri Jul 3 11:38:46 2020 -0300
Add Taler withdraw URI parsing with test
---
.../kotlin/net/taler/wallet/kotlin/TalerUri.kt | 25 +++++++++++++
.../kotlin/net/taler/wallet/kotlin/TalerUriTest.kt | 41 ++++++++++++++++++++++
2 files changed, 66 insertions(+)
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))
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.