Skip to content

Commit

Permalink
zio-http-2559 Update the test kit so that it supports the current dev…
Browse files Browse the repository at this point in the history
…elopment style as supported ...

Implementation for addRoutes in TestServer

Took 6 hours 9 minutes
  • Loading branch information
gmixa committed Dec 22, 2023
1 parent f4b68d2 commit 365bbc9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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 @@ -75,6 +75,13 @@ final case class TestServer(driver: Driver, bindPort: Int) extends Server {
_ <- driver.addApp(app, r)
} yield ()

def addRoutes[Env](routes : Routes[Env,Response]): ZIO[Env, Nothing, Unit] =
for{
env <- ZIO.environment[Env]
app = HttpApp(routes)
_ <- driver.addApp(app,env)
} yield ()

override def install[R](httpApp: HttpApp[R])(implicit
trace: zio.Trace,
): URIO[R, Unit] =
Expand All @@ -91,6 +98,9 @@ final case class TestServer(driver: Driver, bindPort: Int) extends Server {
}

object TestServer {
def addRoutes[R](value: Routes[R, Response]) : ZIO[R with TestServer,Nothing,Unit] =
ZIO.serviceWithZIO[TestServer](_.addRoutes(value))

def addHandler[R](
pf: PartialFunction[Request, ZIO[R, Response, Response]],
): ZIO[R with TestServer, Nothing, Unit] =
Expand Down
28 changes: 24 additions & 4 deletions zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package zio.http

import zio._
import zio.test._

import zio.http.codec.PathCodec
import zio.http.netty.server.NettyDriver
import zio.test._

object TestServerSpec extends ZIOHttpSpec {
def status(response: Response): Status = response.status
Expand Down Expand Up @@ -85,11 +85,31 @@ object TestServerSpec extends ZIOHttpSpec {
)
} yield assertTrue(status(finalResponse) == Status.NotFound)
},
)
.provideSome[Client with Driver](
).provideSome[Client with Driver](
TestServer.layer,
Scope.default,
),
suite("Test Routes")(
test("Get A Respones Ok"){
for{
client <- ZIO.service[Client]
testRequest <- requestToCorrectPort
_ <- TestServer.addRoutes(
Routes(
Method.GET / PathCodec.empty -> handler {
Response.ok
}
)
)
response <- client(testRequest)

} yield assertTrue(status(response)==Status.Ok)
},

).provideSome[Client with Driver](
TestServer.layer,
Scope.default
)
).provide(
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Client.default,
Expand Down

0 comments on commit 365bbc9

Please sign in to comment.