Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Sep 18, 2024
1 parent 643957c commit 4441bd9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/HandlerAspectSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@ import zio._
import zio.test._

object HandlerAspectSpec extends ZIOSpecDefault {

case class WebSession(id: Int)

// Middleware to add session context
def maybeWebSession: HandlerAspect[Any, Option[WebSession]] =
HandlerAspect.interceptIncomingHandler(
Handler.fromFunctionZIO[Request] { req =>
ZIO.succeed((req, Some(WebSession(42))))
},
)

override def spec: Spec[TestEnvironment with Scope, Any] =
suite("HandlerAspect")(
test("HandlerAspect should correctly combine path params and middleware context") {
val route = Method.GET / "base" / string("param") -> handler((param: String, req: Request) => {
withContext((session: Option[WebSession]) => {
ZIO.succeed(Response.text(s"Param: $param, SessionId: ${session.get.id}"))
})
}) @@ maybeWebSession

for {
response <- route(Request.get(URL(!! / "base" / "testParam")))
bodyString <- response.body.asString
} yield assertTrue(bodyString == "Param: testParam, SessionId: 42")
},
test("HandlerAspect with context can eliminate environment type") {
val handler0 = handler((_: Request) => ZIO.serviceWith[Int](i => Response.text(i.toString))) @@
HandlerAspect.interceptIncomingHandler(handler((req: Request) => (req, req.headers.size)))
Expand Down

0 comments on commit 4441bd9

Please sign in to comment.