diff options
author | Torsten Grote <t@grobox.de> | 2020-07-15 16:31:36 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-15 16:31:36 -0300 |
commit | 3e1418bd3061bd7a387273934fab752a5999432f (patch) | |
tree | 1385d00d3d2572c5f57e0581a7b86aa4a20db1b8 /src | |
parent | 3704cda578d0ad4ccbc5392dc40cbc8252da90e9 (diff) | |
download | wallet-kotlin-3e1418bd3061bd7a387273934fab752a5999432f.tar.gz wallet-kotlin-3e1418bd3061bd7a387273934fab752a5999432f.tar.bz2 wallet-kotlin-3e1418bd3061bd7a387273934fab752a5999432f.zip |
Add payto URI parsing with basic tests
Diffstat (limited to 'src')
-rw-r--r-- | src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt | 45 | ||||
-rw-r--r-- | src/commonTest/kotlin/net/taler/wallet/kotlin/PaytoUriTest.kt | 58 |
2 files changed, 103 insertions, 0 deletions
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"]) + } + +} |