aboutsummaryrefslogtreecommitdiff
path: root/server/test/src/handlers/ProtocolHandlerSpec.scala
blob: d35c9f2993f01c45e640de240d3d8fd1576e0f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
  }
}