diff options
author | Juan J. Martinez <jjm@usebox.net> | 2021-07-03 14:14:31 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2021-07-22 19:49:53 +0100 |
commit | c760d7c9bd14d7e56ae1553a0b3c4a1258448686 (patch) | |
tree | e60a35310a2a9f120a5c3e769ea862e950d322f5 /server/test/src/ServiceConfSpec.scala | |
parent | ab4a5268bd2971a364bf026aede2cb50c885a03d (diff) | |
download | spacebeans-c760d7c9bd14d7e56ae1553a0b3c4a1258448686.tar.gz spacebeans-c760d7c9bd14d7e56ae1553a0b3c4a1258448686.zip |
CGI support
Diffstat (limited to 'server/test/src/ServiceConfSpec.scala')
-rw-r--r-- | server/test/src/ServiceConfSpec.scala | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/server/test/src/ServiceConfSpec.scala b/server/test/src/ServiceConfSpec.scala new file mode 100644 index 0000000..d886b4f --- /dev/null +++ b/server/test/src/ServiceConfSpec.scala @@ -0,0 +1,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 + ) + } +} |