summaryrefslogtreecommitdiff
path: root/server/test/src/handlers
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-01-16 00:22:24 +0000
committerJuan J. Martinez <jjm@usebox.net>2022-01-16 00:22:24 +0000
commit60d4dce0193fb9d592f2fe065908bfc88da89dff (patch)
tree4a96331789a475dfbd587eaef95c4f9504081a6b /server/test/src/handlers
parent397bee70a0baa6ab7cc2115c3a5dd555b381a49d (diff)
downloadspacebeans-60d4dce0193fb9d592f2fe065908bfc88da89dff.tar.gz
spacebeans-60d4dce0193fb9d592f2fe065908bfc88da89dff.zip
Refactor Gemini protocol
Diffstat (limited to 'server/test/src/handlers')
-rw-r--r--server/test/src/handlers/ProtocolHandlerSpec.scala100
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
+ }
+}