softwaremill / sttp

The Scala HTTP client you always wanted!
https://sttp.softwaremill.com
Apache License 2.0
1.46k stars 309 forks source link

stubbing websockets with ZIO backends causes request to terminate immediately #981

Closed jportway closed 3 years ago

jportway commented 3 years ago

Sending a request with a responseType : asWebSocketStreamAlways(ZioStreams)(websocketHandler) works fine with a normal "live" backend (AsyncHttpClientZioBackend or HttpClientZioBackend), but when I attempt to use a stubbed backend the "send" function returns immediately, with a "WebsocketStub" in the response, rather than actually opening a websocket.

example code here : https://scastie.scala-lang.org/jportway/xB6GpNXfTeKsUvTQCnc11g/16

When using a "live" backend the code seems to work fine, however when I stub the backend like this :

  val webSocketStub = WebSocketStub
    .initialReceive(
      List(WebSocketFrame.text("Hello from the other side"))
    )
    .thenRespond { _ => List(WebSocketFrame.text("this is your reply")) }

  val stubEffect: URIO[SttpClientStubbing, Unit] = for {
    _ <- whenAnyRequest.thenRespond(webSocketStub)
  } yield ()

the "send" function terminates immediately, returning : Success(Response(sttp.ws.testing.WebSocketStub@7c00b63d,200,OK,List(),List(),RequestMetadata(GET,http://example.com,List())))

adamw commented 3 years ago

Ah, this is because stubbing web socket streams is not supported. See: https://github.com/softwaremill/sttp/blob/8dcc4878b9f400fb45c10d80e5878f5a3d605830/core/src/main/scala/sttp/client3/testing/SttpBackendStub.scala#L239 - in this case, the response is not adapted corresponding to the response specification. I think it would require more knowledge of the streams than is currently captured by sttp.capabilities.Streams, and the ability to "run" the stream.

jportway commented 3 years ago

thanks for looking into this - sorry for delay in closing the issue