Fix bug in login page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 24 Apr 2017 19:42:17 +0000 (21:42 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 24 Apr 2017 19:42:17 +0000 (21:42 +0200)
src/main/kotlin/net/pterodactylus/sone/utils/Strings.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
src/test/kotlin/net/pterodactylus/sone/utils/StringsTest.kt [new file with mode: 0644]

diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Strings.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Strings.kt
new file mode 100644 (file)
index 0000000..9390f98
--- /dev/null
@@ -0,0 +1,3 @@
+package net.pterodactylus.sone.utils
+
+val String?.emptyToNull get() = if ((this?.trim() ?: "") == "") null else this
index 0d839db..560c607 100644 (file)
@@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.pages
 
 import freenet.clients.http.ToadletContext
 import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.utils.emptyToNull
 import net.pterodactylus.sone.utils.isPOST
 import net.pterodactylus.sone.web.pages.SoneTemplatePage
 import net.pterodactylus.sone.web.WebInterface
@@ -20,7 +21,7 @@ class LoginPage(template: Template, webInterface: WebInterface):
                        val soneId = request.httpRequest.getPartAsStringFailsafe("sone-id", 43)
                        webInterface.core.getLocalSone(soneId)?.let { sone ->
                                setCurrentSone(request.toadletContext, sone)
-                               val target = if (request.httpRequest.isParameterSet("target")) request.httpRequest.getPartAsStringFailsafe("target", 256) else "index.html"
+                               val target = request.httpRequest.getParam("target").emptyToNull ?: "index.html"
                                throw RedirectException(target)
                        }
                }
diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/StringsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/StringsTest.kt
new file mode 100644 (file)
index 0000000..e567c0f
--- /dev/null
@@ -0,0 +1,33 @@
+package net.pterodactylus.sone.utils
+
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Test
+
+/**
+ * Unit test for [StringsKt].
+ */
+class StringsTest {
+
+       @Test
+       fun `non-empty string is returned as-is`() {
+               assertThat("non-empty".emptyToNull, equalTo("non-empty"))
+       }
+
+       @Test
+       fun `string with whitespace only is returned as null`() {
+               assertThat("   ".emptyToNull, nullValue())
+       }
+
+       @Test
+       fun `zero-length string is returned as null`() {
+               assertThat("".emptyToNull, nullValue())
+       }
+
+       @Test
+       fun `null is returned as null`() {
+               assertThat(null.emptyToNull, nullValue())
+       }
+
+}