Skip to content

Commit

Permalink
Simple default layer for test server (#3094) (#3161)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Oct 16, 2024
1 parent f29c699 commit dff0b93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 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.default,
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),
)

}
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 dff0b93

Please sign in to comment.