gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-kotlin] 02/02: Add payto URI parsing with basic tests


From: gnunet
Subject: [taler-wallet-kotlin] 02/02: Add payto URI parsing with basic tests
Date: Wed, 15 Jul 2020 22:12:33 +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 3e1418bd3061bd7a387273934fab752a5999432f
Author: Torsten Grote <t@grobox.de>
AuthorDate: Wed Jul 15 16:31:36 2020 -0300

    Add payto URI parsing with basic tests
---
 .../kotlin/net/taler/wallet/kotlin/PaytoUri.kt     | 45 +++++++++++++++++
 .../kotlin/net/taler/wallet/kotlin/PaytoUriTest.kt | 58 ++++++++++++++++++++++
 2 files changed, 103 insertions(+)

diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt 
b/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt
new file mode 100644
index 0000000..f6b11d2
--- /dev/null
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt
@@ -0,0 +1,45 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+data class PaytoUri(
+    val targetType: String,
+    val targetPath: String,
+    val params: Map<String, String>
+) {
+    companion object {
+        private const val SCHEMA = "payto://"
+        fun fromString(s: String): PaytoUri? {
+            if (!s.startsWith(SCHEMA)) return null
+            val rest = s.slice(SCHEMA.length until s.length).split('?')
+            val account = rest[0]
+            val query = if (rest.size > 1) rest[1] else null
+            val firstSlashPos = account.indexOf('/')
+            if (firstSlashPos == -1) return null
+            return PaytoUri(
+                targetType = account.slice(0 until firstSlashPos),
+                targetPath = account.slice((firstSlashPos + 1) until 
account.length),
+                params = HashMap<String, String>().apply {
+                    query?.split('&')?.forEach {
+                        val field = it.split('=')
+                        if (field.size > 1) put(field[0], field[1])
+                    }
+                }
+            )
+        } // end fromString()
+    }
+}
diff --git a/src/commonTest/kotlin/net/taler/wallet/kotlin/PaytoUriTest.kt 
b/src/commonTest/kotlin/net/taler/wallet/kotlin/PaytoUriTest.kt
new file mode 100644
index 0000000..4f080e3
--- /dev/null
+++ b/src/commonTest/kotlin/net/taler/wallet/kotlin/PaytoUriTest.kt
@@ -0,0 +1,58 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
+
+class PaytoUriTest {
+
+    @Test
+    fun testFromString() {
+        // wrong scheme
+        var uri = "https://example.com/";
+        assertNull(PaytoUri.fromString(uri))
+
+        // incomplete scheme
+        uri = "payto:blabla"
+        assertNull(PaytoUri.fromString(uri))
+
+        // proper URI
+        uri = "payto://x-taler-bank/123"
+        var parsedUri = PaytoUri.fromString(uri)
+        assertNotNull(parsedUri)
+        assertEquals("x-taler-bank", parsedUri.targetType)
+        assertEquals("123", parsedUri.targetPath)
+
+        // proper URI with incomplete query
+        uri = "payto://x-taler-bank/123?foo"
+        parsedUri = PaytoUri.fromString(uri)
+        assertNotNull(parsedUri)
+        assertEquals(0, parsedUri.params.size)
+
+        // proper URI with two query param
+        uri = "payto://x-taler-bank/123?foo=bar&hip=hop"
+        parsedUri = PaytoUri.fromString(uri)
+        assertNotNull(parsedUri)
+        assertEquals(2, parsedUri.params.size)
+        assertEquals("bar", parsedUri.params["foo"])
+        assertEquals("hop", parsedUri.params["hip"])
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]