summaryrefslogtreecommitdiff
path: root/example/websockets4/app/test/src/ExampleTests.scala
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:44:14 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:47:20 +0800
commit0f0f945abd365c9f6f83b3d315cea30eacb6ed51 (patch)
tree4b812b4f6650a42056aa31c2f712b3d7e4125925 /example/websockets4/app/test/src/ExampleTests.scala
parente21d5ab3526937619c59dd01114b48521eee9d26 (diff)
downloadcask-0f0f945abd365c9f6f83b3d315cea30eacb6ed51.tar.gz
cask-0f0f945abd365c9f6f83b3d315cea30eacb6ed51.tar.bz2
cask-0f0f945abd365c9f6f83b3d315cea30eacb6ed51.zip
Add test to verify `compress` decorator is properly passing through irrelevant results
Diffstat (limited to 'example/websockets4/app/test/src/ExampleTests.scala')
-rw-r--r--example/websockets4/app/test/src/ExampleTests.scala51
1 files changed, 51 insertions, 0 deletions
diff --git a/example/websockets4/app/test/src/ExampleTests.scala b/example/websockets4/app/test/src/ExampleTests.scala
new file mode 100644
index 0000000..cd33bb4
--- /dev/null
+++ b/example/websockets4/app/test/src/ExampleTests.scala
@@ -0,0 +1,51 @@
+package app
+
+import java.util.concurrent.atomic.AtomicInteger
+
+import org.asynchttpclient.ws.{WebSocket, WebSocketListener, WebSocketUpgradeHandler}
+import utest._
+import concurrent.ExecutionContext.Implicits.global
+import cask.Logger.Console.globalLogger
+object ExampleTests extends TestSuite{
+
+
+ def withServer[T](example: cask.main.Main)(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{
+ test("Websockets") - withServer(Websockets4Main){ host =>
+ @volatile var out = List.empty[String]
+ // 4. open websocket
+ val ws = cask.WsClient.connect("ws://localhost:8080/connect/haoyi"){
+ case cask.Ws.Text(s) => out = s :: out
+ }
+
+ try {
+ // 5. send messages
+ ws.send(cask.Ws.Text("hello"))
+ ws.send(cask.Ws.Text("world"))
+ ws.send(cask.Ws.Text(""))
+ Thread.sleep(100)
+ out ==> List("haoyi world", "haoyi hello")
+
+ var error: String = ""
+ val ws2 = cask.WsClient.connect("ws://localhost:8080/connect/nobody") {
+ case cask.Ws.Text(s) => out = s :: out
+ case cask.Ws.Error(t) => error += t.toString
+ case cask.Ws.Close(code, reason) => error += reason
+ }
+
+ assert(error.contains("403"))
+ }finally ws.close()
+ }
+ }
+}