Skip to content

Commit

Permalink
Uncomment examples
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Feb 5, 2025
1 parent 5520820 commit 2e5c560
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ package sttp.client4.examples.testing
import sttp.client4.*
import sttp.client4.testing.*

@main def testEndpointMultipleQueryParameters(): Unit = ???
// val backend = SyncBackendStub
// .whenRequestMatches(_.uri.paramsMap.contains("filter"))
// .thenRespondAdjust("Filtered")
// .whenRequestMatches(_.uri.path.contains("secret"))
// .thenRespondAdjust("42")
@main def testEndpointMultipleQueryParameters(): Unit =
val backend = SyncBackendStub
.whenRequestMatches(_.uri.paramsMap.contains("filter"))
.thenRespondAdjust("Filtered")
.whenRequestMatches(_.uri.path.contains("secret"))
.thenRespondAdjust("42")

// val parameters1 = Map("filter" -> "name=mary", "sort" -> "asc")
// println(
// basicRequest
// .get(uri"http://example.org?search=true&$parameters1")
// .send(backend)
// .body
// )
val parameters1 = Map("filter" -> "name=mary", "sort" -> "asc")
println(
basicRequest
.get(uri"http://example.org?search=true&$parameters1")
.send(backend)
.body
)

// val parameters2 = Map("sort" -> "desc")
// println(
// basicRequest
// .get(uri"http://example.org/secret/read?$parameters2")
// .send(backend)
// .body
// )
val parameters2 = Map("sort" -> "desc")
println(
basicRequest
.get(uri"http://example.org/secret/read?$parameters2")
.send(backend)
.body
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ import sttp.client4.httpclient.fs2.HttpClientFs2Backend
import cats.effect.ExitCode

object WebSocketTesting extends IOApp:

// // the web socket-handling logic
// def useWebSocket(ws: WebSocket[IO]): IO[Unit] = {
// def send(i: Int) = ws.sendText(s"Hello $i!")
// val receive = ws.receiveText().flatMap(t => IO(println(s"RECEIVED [$t]")))
// send(1) *> send(2) *> receive *> receive
// }

// // the request description
// def openWebSocket(backend: WebSocketBackend[IO]): IO[Unit] =
// basicRequest
// .get(uri"wss://echo.websocket.org")
// .response(asWebSocket(useWebSocket))
// .send(backend)
// .void

// // the backend stub which we'll use instead of a "real" backend
// val stubBackend: WebSocketStreamBackendStub[IO, Fs2Streams[IO]] =
// HttpClientFs2Backend
// .stub[IO]
// .whenRequestMatches(_.uri.toString().contains("echo.websocket.org"))
// .thenRespondAdjust(
// WebSocketStub.noInitialReceive.thenRespond {
// case WebSocketFrame.Text(payload, _, _) =>
// List(WebSocketFrame.text(s"response to: $payload"))
// case _ => Nil // ignoring other types of messages
// },
// StatusCode.SwitchingProtocols
// )
// the web socket-handling logic
def useWebSocket(ws: WebSocket[IO]): IO[Unit] = {
def send(i: Int) = ws.sendText(s"Hello $i!")
val receive = ws.receiveText().flatMap(t => IO(println(s"RECEIVED [$t]")))
send(1) *> send(2) *> receive *> receive
}

// the request description
def openWebSocket(backend: WebSocketBackend[IO]): IO[Unit] =
basicRequest
.get(uri"wss://echo.websocket.org")
.response(asWebSocket(useWebSocket))
.send(backend)
.void

// the backend stub which we'll use instead of a "real" backend
val stubBackend: WebSocketStreamBackendStub[IO, Fs2Streams[IO]] =
HttpClientFs2Backend
.stub[IO]
.whenRequestMatches(_.uri.toString().contains("echo.websocket.org"))
.thenRespondAdjust(
WebSocketStub.noInitialReceive.thenRespond {
case WebSocketFrame.Text(payload, _, _) =>
List(WebSocketFrame.text(s"response to: $payload"))
case _ => Nil // ignoring other types of messages
},
StatusCode.SwitchingProtocols
)

// running the test
override def run(args: List[String]): IO[ExitCode] = ???
// openWebSocket(stubBackend).map(_ => ExitCode.Success)
override def run(args: List[String]): IO[ExitCode] =
openWebSocket(stubBackend).map(_ => ExitCode.Success)

0 comments on commit 2e5c560

Please sign in to comment.