dubeejw commented on a change in pull request #250: Update websocket test to
use local websocket server
URL:
https://github.com/apache/incubator-openwhisk-catalog/pull/250#discussion_r155822596
##########
File path: tests/src/test/scala/packages/websocket/WebSocketTests.scala
##########
@@ -17,81 +17,141 @@
package packages.websocket
+import java.io.File
import java.net.URI
import scala.concurrent.duration.DurationInt
import scala.language.postfixOps
-
import org.junit.runner.RunWith
import org.scalatest.BeforeAndAfterAll
import org.scalatest.junit.JUnitRunner
-
-import common.TestHelpers
-import common.Wsk
-import common.WskProps
-import common.WskTestHelpers
+import common._
import spray.json._
import spray.json.DefaultJsonProtocol._
@RunWith(classOf[JUnitRunner])
-class WebSocketTests
- extends TestHelpers
- with WskTestHelpers
- with BeforeAndAfterAll {
-
- implicit val wskprops = WskProps()
- val wsk = new Wsk()
-
- val websocketSendAction = "/whisk.system/websocket/send"
-
- behavior of "Websocket action"
-
- /**
- * This test requires a websocket server running on the given URI.
- */
- var serverURI: URI = new URI("ws://169.46.21.246:80")
-
- ignore should "Use the websocket action to send a payload" in {
- val uniquePayload = s"The cow says
${System.currentTimeMillis()}".toJson
- val run = wsk.action.invoke(websocketSendAction, Map("uri" ->
serverURI.toString.toJson, "payload" -> uniquePayload))
- withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) {
- activation =>
- activation.response.success shouldBe true
- activation.response.result shouldBe Some(JsObject(
- "payload" -> uniquePayload))
- }
+class WebSocketTests extends TestHelpers with WskTestHelpers with
BeforeAndAfterAll {
+
+ implicit val wskprops: WskProps = WskProps()
+ val wsk = new Wsk()
+
+ val websocketSendAction = "/whisk.system/websocket/send"
+
+ val containerName = "websocketServerOW"
+ val userDir: String = System.getProperty("user.dir")
+ val websocketDir: String = userDir + "/dat/websocket"
+ val websocketHost: String = wskprops.apihost
+ val websocketPort = "20002"
+
+ behavior of "Websocket action"
+
+ /**
+ * This test requires a websocket server running on the given URI.
+ */
+ val serverURI: URI = new URI(s"ws://$websocketHost:$websocketPort")
+
+ it should "Use the websocket action to send a payload" in {
+ val uniquePayload = s"The cow says ${System.currentTimeMillis()}".toJson
+ val run =
+ wsk.action.invoke(websocketSendAction, Map("uri" ->
serverURI.toString.toJson, "payload" -> uniquePayload))
+ withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) {
activation =>
+ activation.response.success shouldBe true
+ activation.response.result shouldBe Some(JsObject("payload" ->
uniquePayload))
}
+ }
- it should "Return an error due to a malformed URI" in {
- val badURI = new URI("ws://localhost:80")
+ it should "Return an error due to a malformed URI" in {
+ val badURI = new URI("ws://localhost:80")
- val run = wsk.action.invoke(websocketSendAction, Map("uri" ->
badURI.toString.toJson, "payload" -> "This is the message to send".toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
+ val run = wsk.action.invoke(
+ websocketSendAction,
+ Map("uri" -> badURI.toString.toJson, "payload" -> "This is the message
to send".toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
- // the exact error content comes from the ws Node module
- activation.response.result.get.fields.get("error") shouldBe
defined
- }
+ // the exact error content comes from the ws Node module
+ activation.response.result.get.fields.get("error") shouldBe defined
}
+ }
- it should "Require a payload parameter" in {
- val run = wsk.action.invoke(websocketSendAction, Map("uri" ->
serverURI.toString.toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
- activation.response.result shouldBe Some(JsObject(
- "error" -> "You must specify a payload parameter.".toJson))
- }
+ it should "Require a payload parameter" in {
+ val run = wsk.action.invoke(websocketSendAction, Map("uri" ->
serverURI.toString.toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
+ activation.response.result shouldBe Some(JsObject("error" -> "You must
specify a payload parameter.".toJson))
}
+ }
- it should "Require a uri parameter" in {
- val run = wsk.action.invoke(websocketSendAction, Map("payload" ->
"This is the message to send".toJson))
- withActivation(wsk.activation, run) {
- activation =>
- activation.response.success shouldBe false
- activation.response.result shouldBe Some(JsObject(
- "error" -> "You must specify a uri parameter.".toJson))
- }
+ it should "Require a uri parameter" in {
+ val run = wsk.action.invoke(websocketSendAction, Map("payload" -> "This is
the message to send".toJson))
+ withActivation(wsk.activation, run) { activation =>
+ activation.response.success shouldBe false
+ activation.response.result shouldBe Some(JsObject("error" -> "You must
specify a uri parameter.".toJson))
}
+ }
+
+ override def beforeAll() {
+
+ val websocketServer =
+ """
+ | const WebSocket = require("ws");
+ | const wss = new WebSocket.Server({ port: 8080 });
+ | wss.on("connection", function connection(ws) {
+ | ws.on("message", function incoming(message) {
+ | ws.send(message);
+ | });
+ | });
+ """.replace("\r", "").stripMargin.lines.mkString
+ println(websocketServer)
Review comment:
Print statement not needed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services