diff options
author | Juan J. MartÃnez <jjm@usebox.net> | 2022-01-20 21:44:34 +0000 |
---|---|---|
committer | Juan J. MartÃnez <jjm@usebox.net> | 2022-01-20 21:44:34 +0000 |
commit | f90e7bad9f1df2d652b82048e9073096ddc8df18 (patch) | |
tree | c6f53976dfcb03cabf474a9c8528db44da762c2e /server/test/src/handlers/ProtocolHandlerSpec.scala | |
parent | 397bee70a0baa6ab7cc2115c3a5dd555b381a49d (diff) | |
parent | aee32d5f42560ad0ff5d47034da699b3e608d2b4 (diff) | |
download | spacebeans-f90e7bad9f1df2d652b82048e9073096ddc8df18.tar.gz spacebeans-f90e7bad9f1df2d652b82048e9073096ddc8df18.zip |
Merge branch 'protocol-refactor' into 'main'
Protocol refactor
See merge request reidrac/spacebeans!2
Diffstat (limited to 'server/test/src/handlers/ProtocolHandlerSpec.scala')
-rw-r--r-- | server/test/src/handlers/ProtocolHandlerSpec.scala | 100 |
1 files changed, 100 insertions, 0 deletions
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 + } +} |