summaryrefslogtreecommitdiff
path: root/example/websockets/app/test/src/ExampleTests.scala
blob: 01863e83245f0567a629ab71bb1a0aa1144614b0 (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
package app
import com.github.andyglow.websocket.WebsocketClient
import utest._

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

  val tests = Tests{
    'VariableRoutes - test(Websockets){ host =>
      @volatile var out = List.empty[String]
      val cli = WebsocketClient[String]("ws://localhost:8080/connect/haoyi") {
        case str => out = str :: out
      }

      // 4. open websocket
      val ws = cli.open()

      // 5. send messages
      ws ! "hello"
      ws ! "world"
      ws ! ""
      Thread.sleep(100)
      out ==> List("haoyi world", "haoyi hello")

      val cli2 = WebsocketClient[String]("ws://localhost:8080/connect/nobody") {
        case str => out = str :: out
      }

      val error =
        try cli2.open()
        catch{case e: Throwable => e.getMessage}

      assert(error.toString.contains("Invalid handshake response getStatus: 403 Forbidden"))
    }

  }
}