summaryrefslogtreecommitdiff
path: root/cask/test/src/test/cask/ExampleTests.scala
blob: ffc4ef2a8089c81ef6bdcb57a3d6ce783b925d18 (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
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 = f("http://localhost:8080")
    server.stop()
    res
  }
  val tests = Tests{
    'MinimalApplication - test(MinimalApplication){ host =>
      val success = requests.get(host)

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

      val failure = requests.get(host + "/doesnt-exist")
      failure.text() ==> "Error 404: Not Found"
      failure.statusCode ==> 404

      val successInfo = requests.get(host + "/request-info?my-query-param=my-query-value")
      assert(
        successInfo.text().contains("my-query-param"),
        successInfo.text().contains("my-query-value")
      )
      successInfo.statusCode ==> 200
    }
    'VariableRoutes - test(VariableRoutes){ host =>
      val noIndexPage = requests.get(host)
      noIndexPage.statusCode ==> 404

      val userPage = requests.get(host + "/user/lihaoyi")
      userPage.text() ==> "User lihaoyi"
      userPage.statusCode ==> 200

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

      val postPage = requests.get(host + "/post/123?query=xyz&query=abc")
      postPage.text() ==> "Post 123 ArrayBuffer(xyz, abc)"
      userPage.statusCode ==> 200

      val badPostPage = requests.get(host + "/post/123")
      badPostPage.text()
    }
  }
}