Skip to content

Commit

Permalink
Restructure Route.Builder to allow unified route syntax using ->
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Feb 25, 2024
1 parent df3f4fb commit 957a088
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 141 deletions.
16 changes: 11 additions & 5 deletions zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ object RouteSpec extends ZIOHttpSpec {
),
suite("error handle")(
test("handleErrorCauseZIO should execute a ZIO effect") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.fail(new Exception("hmm...")) }
val route: Route[Any, Exception] = Method.GET / "endpoint" -> handler { (_: Request) =>
ZIO.fail(new Exception("hmm..."))
}
for {
p <- zio.Promise.make[Exception, String]

Expand All @@ -88,7 +90,9 @@ object RouteSpec extends ZIOHttpSpec {
} yield assertTrue(extractStatus(response) == Status.InternalServerError, result.contains("hmm..."))
},
test("handleErrorCauseRequestZIO should produce an error based on the request") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.fail(new Exception("hmm...")) }
val route: Route[Any, Exception] = Method.GET / "endpoint" -> handler { (_: Request) =>
ZIO.fail(new Exception("hmm..."))
}
for {
p <- zio.Promise.make[Exception, String]

Expand All @@ -109,12 +113,14 @@ object RouteSpec extends ZIOHttpSpec {
)
},
test("handleErrorCauseRequest should produce an error based on the request") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.fail(new Exception("hmm...")) }
val errorHandled =
val route: Route[Any, Exception] = Method.GET / "endpoint" -> handler { (_: Request) =>
ZIO.fail(new Exception("hmm..."))
}
val errorHandled =
route.handleErrorRequest((e, req) =>
Response.internalServerError(s"error accessing ${req.path.encode}: ${e.getMessage}"),
)
val request = Request.get(URL.decode("/endpoint").toOption.get)
val request = Request.get(URL.decode("/endpoint").toOption.get)
for {
response <- errorHandled.toHttpApp.runZIO(request)
resultWarning <- ZIO.fromOption(response.headers.get(Header.Warning).map(_.text))
Expand Down
10 changes: 6 additions & 4 deletions zio-http/jvm/src/test/scala/zio/http/ServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ object ServerSpec extends HttpRunnableSpec {
}
} +
suite("echo content") {
val app = (RoutePattern.any ->
handler { (_: Path, req: Request) =>
req.body.asString.map(text => Response.text(text))
}).sandbox.toHttpApp
val app = Routes(
RoutePattern.any ->
handler { (_: Path, req: Request) =>
req.body.asString.map(text => Response.text(text))
},
).sandbox.toHttpApp

test("status is 200") {
val res = app.deploy.status.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ object AuthSpec extends ZIOHttpSpec with HttpAppTestExtensions {
test("Extract username via context with Routes") {
val app = {
Routes(
Method.GET / "context" -> basicAuthContextM ->
Handler.fromFunction[(AuthContext, Request)] { case (c: AuthContext, _) => Response.text(c.value) },
Method.GET / "context" ->
Handler.fromFunction[(AuthContext, Request)] { case (c: AuthContext, _) =>
Response.text(c.value)
} @@ basicAuthContextM,
)
}.toHttpApp
assertZIO(
Expand All @@ -106,15 +108,15 @@ object AuthSpec extends ZIOHttpSpec with HttpAppTestExtensions {
assertZIO(app.runZIO(Request.get(URL.empty).copy(headers = failureBasicHeader)))(isSome)
},
test("Provide for multiple routes") {
val secureRoutes = Routes(basicAuthContextM)(
Method.GET / "a" --> handler((ctx: AuthContext, _: Request) => Response.text(ctx.value)),
Method.GET / "b" / int("id") --> handler((id: Int, ctx: AuthContext, _: Request) =>
val secureRoutes = Routes.build(
Method.GET / "a" -> handler((ctx: AuthContext, _: Request) => Response.text(ctx.value)),
Method.GET / "b" / int("id") -> handler((id: Int, ctx: AuthContext, _: Request) =>
Response.text(s"for id: $id: ${ctx.value}"),
),
Method.GET / "c" / string("name") --> handler((name: String, ctx: AuthContext, _: Request) =>
Method.GET / "c" / string("name") -> handler((name: String, ctx: AuthContext, _: Request) =>
Response.text(s"for name: $name: ${ctx.value}"),
),
)
) @@ basicAuthContextM
val app = secureRoutes.toHttpApp
for {
s1 <- app.runZIO(Request.get(URL(Root / "a")).copy(headers = successBasicHeader))
Expand Down
2 changes: 1 addition & 1 deletion zio-http/shared/src/main/scala/zio/http/Middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object Middleware extends HandlerAspects {
response.addHeaders(headers)
})

val optionsRoute = {
val optionsRoute: Route[Any, Nothing] = {
implicit val trace: Trace = Trace.empty

Method.OPTIONS / trailing -> handler { (_: Path, request: Request) =>
Expand Down
Loading

0 comments on commit 957a088

Please sign in to comment.