在PlayFramework中测试WebSocket [英] Test WebSocket in PlayFramework

查看:264
本文介绍了在PlayFramework中测试WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Play应用程序中有一个WebSocket,我想为它编写一个测试,但我找不到任何关于如何编写这样的测试的例子。我在播放框架 Google小组中找到了一个讨论,但是最近没有活动。

I have a WebSocket in my Play application and I want to write a test for it, but I couldn't find any example on how to write such a test. I found a discussion in the play-framework Google group but there has been no activity recently.

那么,有没有关于如何在Java测试中测试WebSocket的想法?

So, are there any ideas on how to test WebSocket's in a Java test?

推荐答案

您可以检索底层的Iteratee,Enumerator并直接测试它们。这样您就不需要使用浏览器了。你需要akka-testkit,以应对迭代的异步性质。

You can retrieve underlying Iteratee,Enumerator and test them directly. This way you don't need to use a browser. You need akka-testkit though, to cope with asynchronous nature of iteratees.

Scala示例:

object WebSocket extends Controller {
  def websocket = WebSocket.async[JsValue] { request =>
    Future.successful(Iteratee.ignore[JsValue] -> Enumerator.apply[JsValue](Json.obj("type" -> "error")))   
  }
}

class WebSocketSpec extends PlaySpecification {    
  "WebSocket" should {
    "respond with error packet" in new WithApplication {
      val request = FakeRequest()

      var message: JsValue = null
      val iteratee = Iteratee.foreach[JsValue](chunk => message = chunk)(Akka.system.dispatcher)

      Controller.websocket().f(request)(Enumerator.empty[JsValue],iteratee)

      TestKit.awaitCond(message == Json.obj("type" -> "error"), 1 second)
    }
  }
}

这篇关于在PlayFramework中测试WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆