From 60d4dce0193fb9d592f2fe065908bfc88da89dff Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 16 Jan 2022 00:22:24 +0000 Subject: Refactor Gemini protocol --- server/test/src/handlers/ProtocolHandlerSpec.scala | 100 +++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 server/test/src/handlers/ProtocolHandlerSpec.scala (limited to 'server/test/src/handlers/ProtocolHandlerSpec.scala') diff --git a/server/test/src/handlers/ProtocolHandlerSpec.scala b/server/test/src/handlers/ProtocolHandlerSpec.scala new file mode 100644 index 0000000..d35c9f2 --- /dev/null +++ b/server/test/src/handlers/ProtocolHandlerSpec.scala @@ -0,0 +1,100 @@ +package net.usebox.gemini.server.handlers + +import java.nio.file.FileSystems +import java.net.URI + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +import net.usebox.gemini.server.{ServiceConf, Response} +import net.usebox.gemini.server.TestData + +class ProtocolHandlerSpec extends AnyFlatSpec with Matchers { + + def getPath(value: String) = FileSystems.getDefault().getPath(value) + + class TestHandler(conf: ServiceConf) extends ProtocolHandler(conf) { + def handle(req: String, uri: URI, remoteAddr: String): Response = ??? + } + + val handler = new TestHandler(TestData.conf) + + behavior of "guessMimeType using the internal resolver" + + it should "resolve a known MIME type" in { + handler + .guessMimeType( + getPath("file.html"), + None + ) shouldBe "text/html" + } + + it should "resolve de default MIME type for unknown types" in { + handler + .guessMimeType( + getPath("unknow"), + None + ) shouldBe TestData.conf.defaultMimeType + } + + it should "resolve gemini MIME type" in { + handler + .guessMimeType( + getPath("file.gmi"), + None + ) shouldBe "text/gemini" + handler + .guessMimeType( + getPath("file.gemini"), + None + ) shouldBe "text/gemini" + } + + it should "resolve gemini MIME type, including parameters" in { + handler + .guessMimeType( + getPath("file.gmi"), + Some("param") + ) shouldBe "text/gemini; param" + handler + .guessMimeType( + getPath("file.gemini"), + Some("param") + ) shouldBe "text/gemini; param" + } + + it should "gemini MIME type parameters are sanitized" in { + handler + .guessMimeType( + getPath("file.gmi"), + Some(" ; param") + ) shouldBe "text/gemini; param" + } + + behavior of "guessMimeType using the configured types" + + it should "resolve a known MIME type" in { + new TestHandler(TestData.conf.copy(mimeTypes = TestData.mimeTypes)) + .guessMimeType( + getPath("file.gmi"), + None + ) shouldBe "config" + } + + it should "include parameters for text/gemini MIME types" in { + new TestHandler( + TestData.conf.copy(mimeTypes = Some(Map("text/gemini" -> List(".gmi")))) + ).guessMimeType( + getPath("file.gmi"), + Some("param") + ) shouldBe "text/gemini; param" + } + + it should "resolve de default MIME type for unknown types" in { + new TestHandler(TestData.conf.copy(mimeTypes = TestData.mimeTypes)) + .guessMimeType( + getPath("unknow"), + None + ) shouldBe TestData.conf.defaultMimeType + } +} -- cgit v1.2.3