From d0a517e8f092316889e6d085f66f7aa3fffbdabb Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Wed, 18 Oct 2023 23:50:54 +0400 Subject: [PATCH] Add a test of a middleware providing a context to a `Routes` (#2487) * Add a test of a middleware providing a context to a `Routes` * Add a test of a middleware providing a context to a `Routes` * scalafmt * scalafmt --- .../zio/http/internal/middlewares/AuthSpec.scala | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/zio-http/src/test/scala/zio/http/internal/middlewares/AuthSpec.scala b/zio-http/src/test/scala/zio/http/internal/middlewares/AuthSpec.scala index a59f875e47..7b679c7883 100644 --- a/zio-http/src/test/scala/zio/http/internal/middlewares/AuthSpec.scala +++ b/zio-http/src/test/scala/zio/http/internal/middlewares/AuthSpec.scala @@ -77,6 +77,19 @@ object AuthSpec extends ZIOHttpSpec with HttpAppTestExtensions { } @@ basicAuthContextM).merge.mapZIO(_.body.asString) assertZIO(app.runZIO(Request.get(URL.empty).copy(headers = successBasicHeader)))(equalTo("user")) }, + 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) }, + ) + }.toHttpApp + assertZIO( + app + .runZIO(Request.get(URL.root / "context").copy(headers = successBasicHeader)) + .flatMap(_.body.asString), + )(equalTo("user")) + }, ), suite("basicAuthZIO")( test("HttpApp is accepted if the basic authentication succeeds") {