-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middlewares to forward header and ensure the existence of a header (
#2808) * Add middlewares to forward header and ensure the existence of a header * Fix Scala 3 * Migrate main changes
- Loading branch information
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
zio-http/jvm/src/test/scala/zio/http/ForwardHeaderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package zio.http | ||
|
||
import zio._ | ||
import zio.test._ | ||
|
||
object ForwardHeaderSpec extends ZIOSpecDefault { | ||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("ForwardHeaderSpec")( | ||
test("should forward headers") { | ||
val routes = Routes( | ||
Method.GET / "get" -> handler((_: Request) => | ||
for { | ||
client <- ZIO.service[Client] | ||
response <- (client @@ ZClientAspect.forwardHeaders) | ||
.batched(Request.post(url"http://localhost:8080/post", Body.empty)) | ||
} yield response, | ||
), | ||
Method.POST / "post" -> handler((req: Request) => Response.ok.addHeader(req.header(Header.Accept).get)), | ||
).sandbox @@ Middleware.forwardHeaders(Header.Accept) | ||
|
||
for { | ||
_ <- Server.install(routes) | ||
response <- Client.batched( | ||
Request.get(url"http://localhost:8080/get").addHeader(Header.Accept(MediaType.application.json)), | ||
) | ||
} yield assertTrue(response.headers(Header.Accept).contains(Header.Accept(MediaType.application.json))) | ||
}, | ||
).provide(Client.default, Server.default) @@ TestAspect.withLiveClock | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
zio-http/shared/src/main/scala/zio/http/RequestStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package zio.http | ||
|
||
import zio.{FiberRef, Tag, Unsafe, ZIO} | ||
|
||
object RequestStore { | ||
|
||
private[http] val requestStore: FiberRef[Map[Tag[_], Any]] = | ||
FiberRef.unsafe.make[Map[Tag[_], Any]](Map.empty)(Unsafe.unsafe) | ||
|
||
def get[A: Tag]: ZIO[Any, Nothing, Option[A]] = | ||
requestStore.get.map(_.get(implicitly[Tag[A]]).asInstanceOf[Option[A]]) | ||
|
||
def set[A: Tag](a: A): ZIO[Any, Nothing, Unit] = | ||
requestStore.update(_.updated(implicitly[Tag[A]], a)) | ||
|
||
def update[A: Tag](a: Option[A] => A): ZIO[Any, Nothing, Unit] = | ||
for { | ||
current <- get[A] | ||
_ <- set(a(current)) | ||
} yield () | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters