summaryrefslogtreecommitdiff
path: root/cask/test/src/test/cask/ExampleTests.scala
blob: 1a5cf23120941c6df4c86944916978502038ba10 (plain) (blame)
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
package test.cask
import io.undertow.Undertow
import io.undertow.server.handlers.BlockingHandler
import utest._

object ExampleTests extends TestSuite{
  def test[T](example: cask.main.MainRoutes)(f: String => T): T = {
    val server = Undertow.builder
      .addHttpListener(8080, "localhost")
      .setHandler(new BlockingHandler(example.defaultHandler))
      .build
    server.start()
    val res =
      try f("http://localhost:8080")
      finally server.stop()
    res
  }

  val tests = Tests{
    'MinimalApplication - test(MinimalApplication){ host =>
      val success = requests.get(host)

      success.text() ==> "Hello World!"
      success.statusCode ==> 200

      requests.get(host + "/doesnt-exist").statusCode ==> 404

      requests.post(host + "/do-thing", data = "hello").text() ==> "olleh"

      requests.get(host + "/do-thing").statusCode ==> 404
    }
    'VariableRoutes - test(VariableRoutes){ host =>
      val noIndexPage = requests.get(host)
      noIndexPage.statusCode ==> 404

      requests.get(host + "/user/lihaoyi").text() ==> "User lihaoyi"

      requests.get(host + "/user").statusCode ==> 404


      requests.get(host + "/post/123?param=xyz&param=abc").text() ==>
        "Post 123 ArrayBuffer(xyz, abc)"

      requests.get(host + "/post/123").text() ==>
        """Missing argument: (param: Seq[String])
          |
          |Arguments provided did not match expected signature:
          |
          |showPost
          |  postId  Int
          |  param  Seq[String]
          |
          |""".stripMargin

      requests.get(host + "/path/one/two/three").text() ==>
        "Subpath List(one, two, three)"
    }

    'StaticFiles - test(StaticFiles){ host =>
      requests.get(host + "/static/example.txt").text() ==>
        "the quick brown fox jumps over the lazy dog"
    }

    'RedirectAbort - test(RedirectAbort){ host =>
      val resp = requests.get(host + "/")
      resp.statusCode ==> 401
      resp.history.get.statusCode ==> 301
    }

    'FormJsonPost - test(FormJsonPost){ host =>
      requests.post(host + "/json", data = """{"value1": true, "value2": [3]}""").text() ==>
        "OK true Vector(3)"

      requests.post(
        host + "/form",
        data = Seq("value1" -> "hello", "value2" -> "1", "value2" -> "2")
      ).text() ==>
      "OK FormValue(hello,null) List(1, 2)"

      val resp = requests.post(
        host + "/upload",
        data = requests.MultiPart(
          requests.MultiItem("image", "...", "my-best-image.txt")
        )
      )
      resp.text() ==> "my-best-image.txt"
    }
    'Decorated - test(Decorated){ host =>
      requests.get(host + "/hello/woo").text() ==> "woo31337"
      requests.get(host + "/internal/boo").text() ==> "boo[haoyi]"
      requests.get(host + "/internal-extra/goo").text() ==> "goo[haoyi]31337"

    }
  }
}