From ba731fc96938d9f40f8a67ebd22d9453082e4d0b Mon Sep 17 00:00:00 2001 From: amitsingh Date: Mon, 17 Jan 2022 16:28:53 +0530 Subject: [PATCH] PR comments --- zio-http/src/main/scala/zhttp/http/Middleware.scala | 5 +---- zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala | 2 +- zio-http/src/main/scala/zhttp/http/middleware/Web.scala | 6 +++--- zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/Middleware.scala b/zio-http/src/main/scala/zhttp/http/Middleware.scala index 3d5d75a4e4..8fd78e9a33 100644 --- a/zio-http/src/main/scala/zhttp/http/Middleware.scala +++ b/zio-http/src/main/scala/zhttp/http/Middleware.scala @@ -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 diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala b/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala index 227fdaf6d8..707bfd39cb 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala @@ -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( diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Web.scala b/zio-http/src/main/scala/zhttp/http/middleware/Web.scala index cb3052e987..a9eeacebaa 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Web.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Web.scala @@ -67,7 +67,7 @@ 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 @@ -75,7 +75,7 @@ private[zhttp] trait Web extends Cors with Csrf with Auth with HeaderModifier[Ht 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)))) /** @@ -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)) } diff --git a/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala b/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala index 6e755b48f3..02c995bc59 100644 --- a/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala @@ -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)), )