From: David ‘Bombe’ Roden Date: Mon, 24 Apr 2017 19:42:17 +0000 (+0200) Subject: Fix bug in login page X-Git-Tag: 0.9.7^2~237 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=d5bf199171aa509d295985453db0cc130c3665a8 Fix bug in login page --- 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 index 0000000..9390f98 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Strings.kt @@ -0,0 +1,3 @@ +package net.pterodactylus.sone.utils + +val String?.emptyToNull get() = if ((this?.trim() ?: "") == "") null else this diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt index 0d839db..560c607 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt @@ -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 index 0000000..e567c0f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/StringsTest.kt @@ -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()) + } + +}