Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 committed Jan 17, 2022
1 parent acccb77 commit ba731fc
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
5 changes: 1 addition & 4 deletions zio-http/src/main/scala/zhttp/http/Middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ sealed trait Middleware[-R, +E, +AIn, -BIn, -AOut, +BOut] { self =>
* Applies Middleware based only if the condition function evaluates to true
*/
final def when[AOut0 <: AOut](cond: AOut0 => Boolean): Middleware[R, E, AIn, BIn, AOut0, BOut] =
Middleware.ifThenElse[AOut0](cond(_))(
isTrue = _ => self,
isFalse = _ => Middleware.identity,
)
whenZIO(a => UIO(cond(a)))

/**
* Applies Middleware based only if the condition effectful function evaluates to true
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private[zhttp] trait Csrf {
tokenName: String = "x-csrf-token",
tokenGen: ZIO[R, Nothing, String] = UIO(UUID.randomUUID.toString),
): HttpMiddleware[R, E] =
Middleware.addCookieM(tokenGen.map(Cookie(tokenName, _)))
Middleware.addCookieZIO(tokenGen.map(Cookie(tokenName, _)))

def csrfValidate(tokenName: String = "x-csrf-token"): HttpMiddleware[Any, Nothing] = {
Middleware.whenHeader(
Expand Down
6 changes: 3 additions & 3 deletions zio-http/src/main/scala/zhttp/http/middleware/Web.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ private[zhttp] trait Web extends Cors with Csrf with Auth with HeaderModifier[Ht
* Sets cookie in response headers
*/
def addCookie(cookie: Cookie): HttpMiddleware[Any, Nothing] =
self.addHeaders(Headers.setCookie(cookie))
self.withSetCookie(cookie)

/**
* Updates the provided list of headers to the response
*/
override def updateHeaders(update: Headers => Headers): HttpMiddleware[Any, Nothing] =
Web.updateHeaders(update)

def addCookieM[R, E](cookie: ZIO[R, E, Cookie]): HttpMiddleware[R, E] =
def addCookieZIO[R, E](cookie: ZIO[R, E, Cookie]): HttpMiddleware[R, E] =
patchZIO((_, _) => cookie.mapBoth(Option(_), c => Patch.addHeader(Headers.setCookie(c))))

/**
Expand Down Expand Up @@ -191,5 +191,5 @@ object Web extends HeaderModifier[HttpMiddleware[Any, Nothing]] {
* Updates the current Headers with new one, using the provided update function passed.
*/
override def updateHeaders(update: Headers => Headers): HttpMiddleware[Any, Nothing] =
Middleware.intercept[Request, Response](req => MiddlewareRequest(req))((r, _) => r.updateHeaders(update))
Middleware.patch((_, _) => Patch.updateHeaders(update))
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ object WebSpec extends DefaultRunnableSpec with HttpAppTestExtensions {
testM("addCookieM") {
val cookie = Cookie("test", "testValue")
val app =
(Http.ok @@ addCookieM(UIO(cookie))).getHeader("set-cookie")
(Http.ok @@ addCookieZIO(UIO(cookie))).getHeader("set-cookie")
assertM(app(Request()))(
equalTo(Some(cookie.encode)),
)
Expand Down

1 comment on commit ba731fc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 830785.26

Please sign in to comment.