scala tapir test IO endpoint

i cant find anywere how to make a stub of IO tapir endpoint.
In the docs https://tapir.softwaremill.com/en/latest/testing.html
i can see only example for scala Future.

Basically i need to make a stub for tapir endpoint

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>endpoint
.post
.in("v1" / "payment")
.securityIn(auth.basic[UsernamePassword]())
.in(jsonBody[HttpPaymentRequest])
.out(jsonBody[HttpPaymentResponse])
.errorOut(oneOf[StatusError](
oneOfVariant(statusCode(StatusCode.BadRequest) and jsonBody[Status400Error]),
oneOfVariant(statusCode(StatusCode.InternalServerError) and jsonBody[Status500Error]),
oneOfVariant(statusCode(StatusCode.Unauthorized) and jsonBody[Unauthorized])
))
.serverSecurityLogic[Unit, IO](basicAuth(secretLocal))
.serverLogic(_ => request => ???)
</code>
<code>endpoint .post .in("v1" / "payment") .securityIn(auth.basic[UsernamePassword]()) .in(jsonBody[HttpPaymentRequest]) .out(jsonBody[HttpPaymentResponse]) .errorOut(oneOf[StatusError]( oneOfVariant(statusCode(StatusCode.BadRequest) and jsonBody[Status400Error]), oneOfVariant(statusCode(StatusCode.InternalServerError) and jsonBody[Status500Error]), oneOfVariant(statusCode(StatusCode.Unauthorized) and jsonBody[Unauthorized]) )) .serverSecurityLogic[Unit, IO](basicAuth(secretLocal)) .serverLogic(_ => request => ???) </code>
endpoint
      .post
      .in("v1" / "payment")
      .securityIn(auth.basic[UsernamePassword]())
      .in(jsonBody[HttpPaymentRequest])
      .out(jsonBody[HttpPaymentResponse])
      .errorOut(oneOf[StatusError](
        oneOfVariant(statusCode(StatusCode.BadRequest) and jsonBody[Status400Error]),
        oneOfVariant(statusCode(StatusCode.InternalServerError) and jsonBody[Status500Error]),
        oneOfVariant(statusCode(StatusCode.Unauthorized) and jsonBody[Unauthorized])
      ))
      .serverSecurityLogic[Unit, IO](basicAuth(secretLocal))
      .serverLogic(_ => request => ???)

to test auth logic and server logic in 2 different tests

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>val backendStub: SttpBackend[Future, Any] = TapirStubInterpreter(SttpBackendStub.asynchronousFuture)
.whenServerEndpoint(someServerEndpoint)
.thenRunLogic()
.backend()
</code>
<code>val backendStub: SttpBackend[Future, Any] = TapirStubInterpreter(SttpBackendStub.asynchronousFuture) .whenServerEndpoint(someServerEndpoint) .thenRunLogic() .backend() </code>
val backendStub: SttpBackend[Future, Any] = TapirStubInterpreter(SttpBackendStub.asynchronousFuture)
      .whenServerEndpoint(someServerEndpoint)
      .thenRunLogic()
      .backend()

this example is working only for Future

if anybody have an example for cats IO, please help

2

Here’s an example test using ScalaTest:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//> using dep com.softwaremill.sttp.tapir::tapir-cats-effect:1.11.2
//> using dep com.softwaremill.sttp.tapir::tapir-sttp-stub-server:1.11.2
//> using dep com.softwaremill.sttp.tapir::tapir-cats:1.11.2
//> using dep com.softwaremill.sttp.client3::core:3.9.7
//> using dep org.scalatest::scalatest:3.2.19
package sttp.tapir.examples.testing
import cats.effect.IO
import org.scalatest.flatspec.AsyncFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.client3.*
import sttp.client3.testing.SttpBackendStub
import sttp.tapir.*
import sttp.tapir.integ.cats.effect.CatsMonadError
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.stub.TapirStubInterpreter
import scala.concurrent.Future
class CatsServerStubInterpreter extends AsyncFlatSpec with Matchers:
it should "run greet users logic" in {
// given
// We need to pass an SttpBackendStub which is configured for the IO effect. One way to do it is to pass a
// MonadError implementation, as here. Alternatively, you can use any sttp-client cats-effect backend, and obtain
// the stub using its .stub method. For example: AsyncHttpClientCatsBackend.stub[IO]
val stubBackend: SttpBackend[IO, Any] = TapirStubInterpreter(SttpBackendStub[IO, Any](CatsMonadError()))
.whenServerEndpoint(UsersApi.greetUser)
.thenRunLogic()
.backend()
// when
val response = sttp.client3.basicRequest
.get(uri"http://test.com/api/users/greet")
.header("Authorization", "Bearer password")
.send(stubBackend)
// then
// since we are using ScalaTest, we need to run the IO effect, here - synchronously. When using an IO-aware test
// framework, this might get simplified.
import cats.effect.unsafe.implicits.global
response.unsafeRunSync().body shouldBe Right("hello user123")
}
// The API under test
object UsersApi:
val greetUser: ServerEndpoint[Any, IO] = endpoint.get
.in("api" / "users" / "greet")
.securityIn(auth.bearer[String]())
.out(stringBody)
.errorOut(stringBody)
.serverSecurityLogic(token => IO(if token == "password" then Right("user123") else Left("unauthorized")))
.serverLogic(user => _ => IO(Right(s"hello $user")))
</code>
<code>//> using dep com.softwaremill.sttp.tapir::tapir-cats-effect:1.11.2 //> using dep com.softwaremill.sttp.tapir::tapir-sttp-stub-server:1.11.2 //> using dep com.softwaremill.sttp.tapir::tapir-cats:1.11.2 //> using dep com.softwaremill.sttp.client3::core:3.9.7 //> using dep org.scalatest::scalatest:3.2.19 package sttp.tapir.examples.testing import cats.effect.IO import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import sttp.client3.* import sttp.client3.testing.SttpBackendStub import sttp.tapir.* import sttp.tapir.integ.cats.effect.CatsMonadError import sttp.tapir.server.ServerEndpoint import sttp.tapir.server.stub.TapirStubInterpreter import scala.concurrent.Future class CatsServerStubInterpreter extends AsyncFlatSpec with Matchers: it should "run greet users logic" in { // given // We need to pass an SttpBackendStub which is configured for the IO effect. One way to do it is to pass a // MonadError implementation, as here. Alternatively, you can use any sttp-client cats-effect backend, and obtain // the stub using its .stub method. For example: AsyncHttpClientCatsBackend.stub[IO] val stubBackend: SttpBackend[IO, Any] = TapirStubInterpreter(SttpBackendStub[IO, Any](CatsMonadError())) .whenServerEndpoint(UsersApi.greetUser) .thenRunLogic() .backend() // when val response = sttp.client3.basicRequest .get(uri"http://test.com/api/users/greet") .header("Authorization", "Bearer password") .send(stubBackend) // then // since we are using ScalaTest, we need to run the IO effect, here - synchronously. When using an IO-aware test // framework, this might get simplified. import cats.effect.unsafe.implicits.global response.unsafeRunSync().body shouldBe Right("hello user123") } // The API under test object UsersApi: val greetUser: ServerEndpoint[Any, IO] = endpoint.get .in("api" / "users" / "greet") .securityIn(auth.bearer[String]()) .out(stringBody) .errorOut(stringBody) .serverSecurityLogic(token => IO(if token == "password" then Right("user123") else Left("unauthorized"))) .serverLogic(user => _ => IO(Right(s"hello $user"))) </code>
//> using dep com.softwaremill.sttp.tapir::tapir-cats-effect:1.11.2
//> using dep com.softwaremill.sttp.tapir::tapir-sttp-stub-server:1.11.2
//> using dep com.softwaremill.sttp.tapir::tapir-cats:1.11.2
//> using dep com.softwaremill.sttp.client3::core:3.9.7
//> using dep org.scalatest::scalatest:3.2.19

package sttp.tapir.examples.testing

import cats.effect.IO
import org.scalatest.flatspec.AsyncFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.client3.*
import sttp.client3.testing.SttpBackendStub
import sttp.tapir.*
import sttp.tapir.integ.cats.effect.CatsMonadError
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.stub.TapirStubInterpreter

import scala.concurrent.Future

class CatsServerStubInterpreter extends AsyncFlatSpec with Matchers:
  it should "run greet users logic" in {
    // given
    // We need to pass an SttpBackendStub which is configured for the IO effect. One way to do it is to pass a
    // MonadError implementation, as here. Alternatively, you can use any sttp-client cats-effect backend, and obtain
    // the stub using its .stub method. For example: AsyncHttpClientCatsBackend.stub[IO]
    val stubBackend: SttpBackend[IO, Any] = TapirStubInterpreter(SttpBackendStub[IO, Any](CatsMonadError()))
      .whenServerEndpoint(UsersApi.greetUser)
      .thenRunLogic()
      .backend()

    // when
    val response = sttp.client3.basicRequest
      .get(uri"http://test.com/api/users/greet")
      .header("Authorization", "Bearer password")
      .send(stubBackend)

    // then
    // since we are using ScalaTest, we need to run the IO effect, here - synchronously. When using an IO-aware test
    // framework, this might get simplified.
    import cats.effect.unsafe.implicits.global
    response.unsafeRunSync().body shouldBe Right("hello user123")
  }

  // The API under test
  object UsersApi:
    val greetUser: ServerEndpoint[Any, IO] = endpoint.get
      .in("api" / "users" / "greet")
      .securityIn(auth.bearer[String]())
      .out(stringBody)
      .errorOut(stringBody)
      .serverSecurityLogic(token => IO(if token == "password" then Right("user123") else Left("unauthorized")))
      .serverLogic(user => _ => IO(Right(s"hello $user")))

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật