Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for reading headers from web socket responses #3965

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,7 @@ lazy val sttpClient: ProjectMatrix = (projectMatrix in file("client/sttp-client"
"com.softwaremill.sttp.client3" %% "fs2" % Versions.sttp % Test,
"com.softwaremill.sttp.client3" %% "zio" % Versions.sttp % Test,
"com.softwaremill.sttp.client3" %% "pekko-http-backend" % Versions.sttp % Test,
"com.softwaremill.sttp.client3" %% "async-http-client-backend-fs2" % Versions.sttp % Test,
"com.softwaremill.sttp.shared" %% "fs2" % Versions.sttpShared % Optional,
"com.softwaremill.sttp.shared" %% "zio" % Versions.sttpShared % Optional,
"com.softwaremill.sttp.shared" %% "pekko" % Versions.sttpShared % Optional,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import cats.effect.IO
import fs2._
import sttp.capabilities.WebSockets
import sttp.capabilities.fs2.Fs2Streams
import sttp.client3._
import sttp.client3.asynchttpclient.fs2.AsyncHttpClientFs2Backend
import sttp.tapir._
import sttp.tapir.client.tests.ClientWebSocketTests
import sttp.tapir.client.sttp.ws.fs2._

Expand All @@ -16,4 +19,41 @@ class SttpClientWebSocketTests extends SttpClientTests[WebSockets with Fs2Stream
}

webSocketTests()

test("web sockets, string echo, custom header") {
// HttpClient doesn't expose web socket response headers
AsyncHttpClientFs2Backend
.resource[IO]()
.use { asyncHttpClientBackend =>
def sendAsResponse[A, I, E, O](
e: Endpoint[A, I, E, O, WebSockets with Fs2Streams[IO]],
port: Port,
securityArgs: A,
args: I,
scheme: String = "http"
): IO[Response[Either[E, O]]] = {
SttpClientInterpreter()
.toSecureRequestThrowDecodeFailures(e, Some(uri"$scheme://localhost:$port"))
.apply(securityArgs)
.apply(args)
.send(asyncHttpClientBackend)
}

sendAsResponse(
endpoint.get
.in("ws" / "echo" / "header")
.out(webSocketBody[String, CodecFormat.TextPlain, String, CodecFormat.TextPlain].apply(streams)),
port,
(),
(),
"ws"
)
.flatMap { r =>
r.header("Correlation-id") shouldBe Some("ABC-DEF-123")
sendAndReceiveLimited(r.body.toOption.get, 2, List("test1", "test2"))
}
.map(_ shouldBe List("echo: test1", "echo: test2"))
}
.unsafeToFuture()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ class HttpServer(port: Port) {
wsb.build(d, e)
}

case GET -> Root / "ws" / "echo" / "header" =>
val echoReply: fs2.Pipe[IO, WebSocketFrame, WebSocketFrame] =
_.collect { case WebSocketFrame.Text(msg, _) => WebSocketFrame.Text("echo: " + msg) }

Queue
.unbounded[IO, WebSocketFrame]
.flatMap { q =>
val d = Stream.repeatEval(q.take).through(echoReply)
val e: Pipe[IO, WebSocketFrame, Unit] = s => s.evalMap(q.offer)
wsb
.withHeaders(Headers(Header.Raw(CIString("Correlation-id"), "ABC-DEF-123")))
.build(d, e)
}

case GET -> Root / "entity" / entityType =>
if (entityType == "person") Created("""{"name":"mary","age":20}""")
else Ok("""{"name":"work"}""")
Expand Down
Loading