Repository: mjaakko/NeoStumbler (language: kotlin)

Issue:
Normalize submission URL
I have two users who are sending data to `//v2/geosubmit`, resulting in a 404 error.

```
"POST //v2/geosubmit HTTP/2.0" 404 0 "-" "NeoStumbler/9"
```


Discussion hints:
nan

Pull request: Remove consecutive slashes from the Geosubmit endpoint URL
Fixes #126

Test command: ./gradlew :app:testFullDebugUnitTest --tests "xyz.malkki.neostumbler.geosubmit.GeosubmitParamsTest"

Fix patch:
diff --git a/app/src/main/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParams.kt b/app/src/main/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParams.kt
index 3ecb2771..8104bf9a 100644
--- a/app/src/main/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParams.kt
+++ b/app/src/main/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParams.kt
@@ -17,7 +17,8 @@ data class GeosubmitParams(
     }
 
     fun toUrl(): HttpUrl? {
-        val url = "$baseUrl$path".toHttpUrlOrNull()
+        val urlStr = "$baseUrl$path"
+        val url = urlStr.removeConsecutiveSlashes().toHttpUrlOrNull()
 
         return if (apiKey != null && url != null) {
             url.newBuilder()
@@ -27,4 +28,19 @@ data class GeosubmitParams(
             url
         }
     }
+
+    /**
+     * Removes consecutive slashes from the URL, as it's probably just a typo
+     */
+    private fun String.removeConsecutiveSlashes(): String {
+        return mapIndexedNotNull { i, char ->
+            val next = getOrNull(i + 1)
+
+            if (char == '/' && char == next) {
+                null
+            } else {
+                char
+            }
+        }.joinToString("")
+    }
 }


Test patch:
diff --git a/app/src/test/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParamsTest.kt b/app/src/test/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParamsTest.kt
new file mode 100644
index 00000000..07108ff1
--- /dev/null
+++ b/app/src/test/java/xyz/malkki/neostumbler/geosubmit/GeosubmitParamsTest.kt
@@ -0,0 +1,20 @@
+package xyz.malkki.neostumbler.geosubmit
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class GeosubmitParamsTest {
+    @Test
+    fun `Test creating an URL`() {
+        val params = GeosubmitParams("http://example.com", "/geosubmit", null)
+
+        assertEquals("http://example.com/geosubmit", params.toUrl().toString())
+    }
+
+    @Test
+    fun `Test creating an URL when both base URL and path contain a slash`() {
+        val params = GeosubmitParams("https://example.com/api/", "/v2/geosubmit", null)
+
+        assertEquals("https://example.com/api/v2/geosubmit", params.toUrl().toString())
+    }
+}
\ No newline at end of file