From c760d7c9bd14d7e56ae1553a0b3c4a1258448686 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 3 Jul 2021 14:14:31 +0100 Subject: CGI support --- server/test/src/ServiceConfSpec.scala | 135 ++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 server/test/src/ServiceConfSpec.scala (limited to 'server/test/src/ServiceConfSpec.scala') 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 + ) + } +} -- cgit v1.2.3