summaryrefslogtreecommitdiff
path: root/server/test/src/ServiceConfSpec.scala
blob: d886b4f5dd79699c54ae1e2800e8d0b68824db78 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package net.usebox.gemini.server

import java.nio.file.FileSystems

import scala.concurrent.duration._

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ServiceConfSpec extends AnyFlatSpec with Matchers {

  def getPath(value: String) =
    FileSystems.getDefault().getPath(getClass.getResource("/").getPath(), value)

  behavior of "getDirectoryListing"

  it should "resolve directory listing using vhost conf if no directory override" in {
    val vh = TestData.conf.virtualHosts.head
    vh.getDirectoryListing(getPath("/dir")) shouldBe vh.directoryListing
  }

  it should "resolve directory listing using directory override" in {
    val vh = TestData.conf.virtualHosts.head.copy(
      directoryListing = false,
      directories = List(
        Directory(
          getPath("dir").toString(),
          directoryListing = Some(true),
          None
        )
      )
    )
    vh.getDirectoryListing(getPath("dir")) shouldBe true
  }

  it should "ignore non matching directories resolving directory listing" in {
    val vh = TestData.conf.virtualHosts.head.copy(
      directoryListing = false,
      directories = List(
        Directory(
          getPath("no-match").toString(),
          directoryListing = Some(true),
          allowCgi = None
        )
      )
    )
    vh.getDirectoryListing(getPath("dir")) shouldBe false
  }

  behavior of "getCgi"

  it should "return None as allow CGI is off by default" in {
    val vh = TestData.conf.virtualHosts.head
    vh.getCgi(getPath("dir/cgi")) shouldBe None
  }

  it should "set allow CGI via directory override" in {
    List(true, false).foreach { value =>
      val vh = TestData.conf.virtualHosts.head.copy(
        directories = List(
          Directory(
            getPath("dir").toString(),
            directoryListing = None,
            allowCgi = Some(value)
          )
        )
      )
      vh.getCgi(getPath("dir/cgi")) should matchPattern {
        case Some(_) if value =>
        case None if !value   =>
      }
    }
  }

  it should "return the CGI path minus path info" in {
    val vh = TestData.conf.virtualHosts.head.copy(
      directories = List(
        Directory(
          getPath("dir").toString(),
          directoryListing = None,
          allowCgi = Some(true)
        )
      )
    )
    vh.getCgi(getPath("dir/cgi/path/info")) shouldBe Some(
      getPath("dir/cgi")
    )
  }

  it should "not return the CGI path if is exactly the CGI dir" in {
    val vh = TestData.conf.virtualHosts.head.copy(
      directories = List(
        Directory(
          getPath("dir").toString(),
          directoryListing = None,
          allowCgi = Some(true)
        )
      )
    )
    vh.getCgi(getPath("dir")) shouldBe None
    vh.getCgi(getPath("dir/")) shouldBe None
  }

  it should "not return the CGI path if allow CGI is false" in {
    val vh = TestData.conf.virtualHosts.head.copy(
      directories = List(
        Directory(
          getPath("dir").toString(),
          directoryListing = None,
          allowCgi = Some(false)
        )
      )
    )
    vh.getCgi(getPath("dir/cgi")) shouldBe None
    vh.getCgi(getPath("dir/cgit/with/path")) shouldBe None
  }

  object TestData {
    val conf = ServiceConf(
      address = "127.0.0.1",
      port = 1965,
      defaultMimeType = "text/plain",
      idleTimeout = 10.seconds,
      virtualHosts = List(
        VirtualHost(
          host = "localhost",
          root = getClass.getResource("/").getPath()
        )
      ),
      genCertValidFor = 1.day,
      enabledProtocols = Nil,
      enabledCipherSuites = Nil
    )
  }
}