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
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)
}
}
--- /dev/null
+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())
+ }
+
+}