package net.usebox.gemini.server.handlers import java.net.URI import java.nio.file.FileSystems import net.usebox.gemini.server.{Response, ServiceConf} import net.usebox.gemini.server.TestData import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers 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 } }