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

Fixing partial provision of environment to Routes when resulting env is intersection type. #3128

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
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
Loading