2013-05-03 44 views
10

我的Play應用程序中有一個WebSocket,我想爲它編寫一個測試,但是我找不到任何關於如何編寫這樣的測試的示例。我在Google小組play-framework中發現了一個討論,但最近沒有任何活動。在PlayFramework中測試WebSocket

那麼,有沒有關於如何在Java測試中測試WebSocket的想法?

回答

3

你可以獲取底層Iteratee,枚舉並直接對其進行測試。這樣你就不需要使用瀏覽器。儘管如此,您仍然需要akka-testkit來應對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) 
    } 
    } 
} 
0

假設你有一個返回未來的WebSocket庫[Itearatee [JsValue,單位],枚舉[JsValue]您的控制器使用

trait WSLib { 
    def connect: Future[Itearatee[JsValue, Unit], Enumerator[JsValue]] 
} 

而且你想測試這個庫。

這裏有一個背景下,你可以使用:

trait WebSocketContext extends WithApplication { 
    val aSecond = FiniteDuration(1, TimeUnit.SECONDS) 


    case class Incoming(iteratee: Iteratee[JsValue, Unit]) { 

    def feed(message: JsValue) = { 
     iteratee.feed(Input.El(message)) 
    } 

    def end(wait: Long = 100) = { 
     Thread.sleep(wait) //wait until all previous fed messages are handled 
     iteratee.feed(Input.EOF) 
    } 
    } 

    case class OutGoing(enum: Enumerator[JsValue]) { 
    val messages = enum(Iteratee.fold(List[JsValue]()) { 
     (l, jsValue) => jsValue :: l 
    }).flatMap(_.run) 

    def get: List[JsValue] = { 
     Await.result(messages, aSecond) 
    } 
    } 

    def wrapConnection(connection: => Future[Iteratee[JsValue, Unit], Enumerator[JsValue]]): (Incoming, OutGoing) = { 
    val (iteratee, enumerator) = Await.result(conn, aSecond) 
    (Incoming(iteratee), OutGoing(enumerator)) 
    } 

} 

然後你的測試可以寫成

"return all subscribers when asked for info" in new WebSocketContext { 
    val (incoming, outgoing) = wrapConnection(myWSLib.connect) 

    incoming.feed(JsObject("message" => "hello")) 
    incoming.end() //this closes the connection 


    val responseMessages = outgoing.get //you only call this "get" after the connection is closed 
    responseMessages.size must equalTo(1) 
    responseMessages must contain(JsObject("reply" => "Hey")) 
} 

來電錶示從客戶端傳來的消息,而即將離任的代表發送的消息從服務器。要編寫測試,首先要從incoming傳入傳入消息,然後通過調用incoming.end關閉連接,然後從outgoing.get方法獲得傳出消息的完整列表。

1

Chrome提供了一個plugin來測試websocket服務。

編輯

因此,使用插件(如下面圖中所示),可以提供的WebSocket URL和請求的數據和發送消息給服務。消息日誌顯示從客戶端發送的消息以及服務響應。

enter image description here

+1

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – 2015-06-05 14:02:43

+0

@勞倫斯·艾羅你同意你的觀點,我不好認爲這很微不足道。我已經更新了更多細節。 – 2015-06-05 14:26:27