# How test the websocket?

**URL:** <https://discuss.akka.io/t/how-test-the-websocket/3791>\
**Category:** Akka HTTP\
**Tags:** scala\
**Created:** [March 28, 2019, 7:19pm UTC](https://discuss.akka.io/t/how-test-the-websocket/3791 "2019-03-28T19:19:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bifunctor](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/bifunctor/32/1335_2.png) [@bifunctor](https://discuss.akka.io/u/bifunctor)\
**Post date:** [March 28, 2019, 7:19pm UTC](https://discuss.akka.io/t/how-test-the-websocket/3791/1 "2019-03-28T19:19:06Z")

</div>

Hi all

I’ve created a websocket as the following:

```auto

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}

import scala.io.StdIn

object WebServer {

  def messenger(implicit materializer: ActorMaterializer): Flow[Message, Message, Any] =
    Flow[Message].mapConcat {
      case tm: TextMessage =>
        TextMessage(tm.textStream) :: Nil
      case bm: BinaryMessage =>
        // ignore binary messages but drain content to avoid the stream being clogged
        bm.dataStream.runWith(Sink.ignore)
        Nil
    }

  def main(args: Array[String]) {

    implicit val system = ActorSystem("sapbroker")
    implicit val materializer = ActorMaterializer()
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.dispatcher

    val route =
      pathSingleSlash {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Welcome to SAP broker.</h1>"))
        }
      } ~ path("sap") {
        handleWebSocketMessages(messenger)
      }

    val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)

    println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
    StdIn.readLine() // let it run until user presses return
    bindingFuture
      .flatMap(_.unbind()) // trigger unbinding from the port
      .onComplete(_ => system.terminate()) // and shutdown when done
  }
}

```

How to write the test for the websocket?

Thanks

---

<div class="post-metadata">

**Author:** ![jrudolph](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/jrudolph/32/80_2.png) [@jrudolph](https://discuss.akka.io/u/jrudolph)\
**Post date:** [April 23, 2019, 9:36am UTC](https://discuss.akka.io/t/how-test-the-websocket/3791/2 "2019-04-23T09:36:42Z")

</div>

See the snippet in [this paragraph](https://doc.akka.io/docs/akka-http/current/server-side/websocket-support.html#routing-support) of the documentation which contains code for testing WS services.

---

<div class="post-metadata">

**Author:** ![vkulikov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/vkulikov/32/1355_2.png) [@vkulikov](https://discuss.akka.io/u/vkulikov)\
**Post date:** [May 14, 2019, 6:19pm UTC](https://discuss.akka.io/t/how-test-the-websocket/3791/3 "2019-05-14T18:19:24Z")

</div>

Hi!  
Looks like that code assumes something that is not mentioned in the documentation.  
It requires to import `akka.http.scaladsl.server.RoutingSpec`  
but

```auto
not found: type RoutingSpec
[error] class ServerTest extends RoutingSpec {
[error] ^

```

Thanks!

---

<div class="post-metadata">

**Author:** ![vkulikov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/vkulikov/32/1355_2.png) [@vkulikov](https://discuss.akka.io/u/vkulikov)\
**Post date:** [May 15, 2019, 7:30pm UTC](https://discuss.akka.io/t/how-test-the-websocket/3791/4 "2019-05-15T19:30:33Z")

</div>

Working imports are:

```auto
import akka.http.scaladsl.testkit.{ ScalatestRouteTest, WSProbe }
import org.scalatest.{ Matchers, WordSpec }

import scala.concurrent.duration._

class WebServerTest extends WordSpec with Matchers with ScalatestRouteTest {

```
