summaryrefslogtreecommitdiff
path: root/server/test/src/ServerSpec.scala
blob: cfb613a693c466da12c91d42e343d1d249657078 (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
package net.usebox.gemini.server

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import akka.util.ByteString

class ServerSpec extends AnyFlatSpec with Matchers {

  behavior of "decodeUTF8"

  it should "return right on valid UTF-8 codes" in {
    Server(TestData.conf)
      .decodeUTF8(ByteString("válid UTF-8")) shouldBe Symbol(
      "right"
    )
  }

  it should "return left on invalid UTF-8 codes" in {
    Server(TestData.conf)
      .decodeUTF8(ByteString(Array(0xc3.toByte, 0x28.toByte))) shouldBe Symbol(
      "left"
    )
  }

  behavior of "handleReq"

  it should "return bad request on empty URLs" in {
    Server(TestData.conf)
      .handleReq("", "127.0.0.1") should be(
      a[BadRequest]
    )
  }

  it should "return bad request for invalid URLs" in {
    Server(TestData.conf)
      .handleReq("gemini://localhost/ invalid", "127.0.0.1") should be(
      a[BadRequest]
    )
  }

  it should "return bad request on URLs with no scheme" in {
    Server(TestData.conf)
      .handleReq("//localhost/", "127.0.0.1") should be(a[BadRequest])
  }

  it should "return proxy request refused on port mismatch" in {
    Server(TestData.conf)
      .handleReq("gemini://localhost:8080/", "127.0.0.1") should be(
      a[ProxyRequestRefused]
    )
  }

  it should "return proxy request refused when port not provided and configured port is not default" in {
    Server(TestData.conf.copy(port = 8080))
      .handleReq("gemini://localhost/", "127.0.0.1") should be(
      a[ProxyRequestRefused]
    )
  }

  it should "return success when port is provided and matches configured port (not default)" in {
    Server(TestData.conf.copy(port = 8080))
      .handleReq("gemini://localhost:8080/", "127.0.0.1") should be(a[Success])
  }

  it should "return proxy request refused for non gemini schemes" in {
    Server(TestData.conf)
      .handleReq("https://localhost/", "127.0.0.1") should be(
      a[ProxyRequestRefused]
    )
  }
}