Skip to content

Commit

Permalink
Fixing partial provision of environment to Routes when resulting env …
Browse files Browse the repository at this point in the history
…is intersection type. (#3128)
  • Loading branch information
pawelsadlo authored Sep 11, 2024
1 parent 39d7b93 commit 8c70090
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 45 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package zio.http
import zio._
import zio.test._

import zio.http.codec.{HttpCodec, StatusCodec}
import zio.http.endpoint.{AuthType, Endpoint}

object RouteSpec extends ZIOHttpSpec {
def extractStatus(response: Response): Status = response.status

Expand Down Expand Up @@ -163,6 +166,48 @@ object RouteSpec extends ZIOHttpSpec {
bodyString <- response.body.asString
} yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error")
},
test(
"Routes with context can eliminate environment type partially when elimination produces intersection type environment",
) {

val authContext: HandlerAspect[Any, String] = HandlerAspect.customAuthProviding[String] { request =>
{
request.headers.get(Header.Authorization).flatMap {
case Header.Authorization.Basic(uname, secret) if uname.reverse == secret.value.mkString =>
Some(uname)
case _ =>
None
}
}
}

val endpoint = Endpoint(RoutePattern(Method.GET, Path.root))
.outCodec[String](StatusCodec.Ok ++ HttpCodec.content[String])
.auth(AuthType.Basic)

val effectWithTwoDependency: ZIO[Int & Long, Nothing, String] = for {
int <- ZIO.service[Int]
long <- ZIO.service[Long]
} yield s"effectWithTwoDependencyResult $int $long"

val route: Route[Int & Long & String, Nothing] =
endpoint.implement((_: Unit) => withContext((_: String) => "") *> effectWithTwoDependency)
val routes = Routes(route).@@[Int & Long](authContext)

val env: ZEnvironment[Int with Long] = ZEnvironment(1).add(2L)
val expected = "\"effectWithTwoDependencyResult 1 2\""
for {
response <- routes
.provideEnvironment(env)
.apply(
Request(
headers = Headers("accept", "text") ++ Headers(Header.Authorization.Basic("123", "321")),
method = Method.GET,
).path(Path.root),
)
bodyString <- response.body.asString
} yield assertTrue(bodyString == expected)
},
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ trait RoutesCompanionVersionSpecific {
private[http] class ApplyContextAspect[-Env, +Err, Env0](private val self: Routes[Env, Err]) {
def apply[Env1, Env2 <: Env, Ctx: Tag](aspect: HandlerAspect[Env1, Ctx])(implicit
ev: Env0 with Ctx <:< Env,
tag: Tag[Env0],
tag1: Tag[Env1],
): Routes[Env0 with Env1, Err] = self.transform(_.@@[Env0](aspect))
}

Expand Down

0 comments on commit 8c70090

Please sign in to comment.