Skip to content

Commit

Permalink
Simple default layer for test server (#3094)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Sep 23, 2024
1 parent 087ee94 commit 7e1f3d6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
6 changes: 2 additions & 4 deletions docs/guides/testing-http-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
```
10 changes: 10 additions & 0 deletions zio-http-testkit/src/main/scala/zio/http/TestServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")(
Expand Down Expand Up @@ -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") {
Expand All @@ -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 =
Expand Down

0 comments on commit 7e1f3d6

Please sign in to comment.