From 7e1f3d6fb34a699f011b30604573ee31a0f4d79d Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Mon, 23 Sep 2024 23:06:11 +0200 Subject: [PATCH] Simple default layer for test server (#3094) --- docs/guides/testing-http-apps.md | 6 ++---- .../src/main/scala/zio/http/TestServer.scala | 10 ++++++++++ .../test/scala/zio/http/SocketContractSpec.scala | 8 +------- .../src/test/scala/zio/http/TestServerSpec.scala | 15 ++++++--------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/docs/guides/testing-http-apps.md b/docs/guides/testing-http-apps.md index 578d958ee2..c9766c0558 100644 --- a/docs/guides/testing-http-apps.md +++ b/docs/guides/testing-http-apps.md @@ -117,12 +117,10 @@ object TestServerExampleSpec extends ZIOSpecDefault { fallbackResponse <- client.batched(Request.get(testRequest.url / "any")) fallbackBody <- fallbackResponse.body.asString } yield assertTrue(helloBody == "Hey there!", fallbackBody == "fallback") - }.provideSome[Client with Driver](TestServer.layer) + } }.provide( - ZLayer.succeed(Server.Config.default.onAnyOpenPort), + TestServer.simple, Client.default, - NettyDriver.customized, - ZLayer.succeed(NettyConfig.defaultWithFastShutdown), ) } ``` diff --git a/zio-http-testkit/src/main/scala/zio/http/TestServer.scala b/zio-http-testkit/src/main/scala/zio/http/TestServer.scala index 0bdd25dad9..d551f693dd 100644 --- a/zio-http-testkit/src/main/scala/zio/http/TestServer.scala +++ b/zio-http-testkit/src/main/scala/zio/http/TestServer.scala @@ -2,6 +2,9 @@ package zio.http import zio._ +import zio.http.netty.NettyConfig +import zio.http.netty.server.NettyDriver + /** * Enables tests that make calls against "localhost" with user-specified * Behavior/Responses. @@ -136,4 +139,11 @@ object TestServer { } yield TestServer(driver, result.port) } + val default: ZLayer[Any, Nothing, Server with TestServer] = ZLayer.make[Server with TestServer][Nothing]( + TestServer.layer.orDie, + ZLayer.succeed(Server.Config.default.onAnyOpenPort), + NettyDriver.customized.orDie, + ZLayer.succeed(NettyConfig.defaultWithFastShutdown), + ) + } diff --git a/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala b/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala index f8a50cfcc7..c0dfa391ad 100644 --- a/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala +++ b/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala @@ -102,13 +102,7 @@ object SocketContractSpec extends ZIOHttpSpec { ) _ <- promise.await.timeout(10.seconds) } yield assert(response.status)(equalTo(Status.SwitchingProtocols)) - }.provideSome[Client]( - TestServer.layer, - NettyDriver.customized, - ZLayer.succeed(NettyConfig.defaultWithFastShutdown), - ZLayer.succeed(Server.Config.default.onAnyOpenPort), - Scope.default, - ).provide(Client.default), + }.provide(TestServer.default, Client.default), test("Test") { for { portAndPromise <- testServerSetup(serverApp) diff --git a/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala b/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala index 084d9197e2..a6b90871bb 100644 --- a/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala +++ b/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala @@ -37,8 +37,8 @@ object TestServerSpec extends ZIOHttpSpec { ) } yield assertTrue(status(response1) == Status.Ok) && assertTrue(status(response2) == Status.InternalServerError) - }.provideSome[Client with Driver]( - TestServer.layer, + }.provideSome[Client]( + TestServer.default, Scope.default, ), suite("Exact Request=>Response version")( @@ -83,8 +83,8 @@ object TestServerSpec extends ZIOHttpSpec { } yield assertTrue(status(finalResponse) == Status.NotFound) }, ) - .provideSome[Client with Driver]( - TestServer.layer, + .provideSome[Client]( + TestServer.default, Scope.default, ), test("add routes to the server") { @@ -102,15 +102,12 @@ object TestServerSpec extends ZIOHttpSpec { fallbackResponse <- client(Request.get(testRequest.url / "any")) fallbackBody <- fallbackResponse.body.asString } yield assertTrue(helloBody == "Hey there!", fallbackBody == "fallback") - }.provideSome[Client with Driver]( - TestServer.layer, + }.provideSome[Client]( + TestServer.default, Scope.default, ), ).provide( - ZLayer.succeed(Server.Config.default.onAnyOpenPort), Client.default, - NettyDriver.customized, - ZLayer.succeed(NettyConfig.defaultWithFastShutdown), ) private def requestToCorrectPort =