|
| 1 | +package zio.http |
| 2 | + |
| 3 | +import zio._ |
| 4 | +import zio.test.TestAspect.shrinks |
| 5 | +import zio.test._ |
| 6 | + |
| 7 | +import zio.http.endpoint.{AuthType, Endpoint} |
| 8 | +import zio.http.netty.NettyConfig |
| 9 | +import zio.http.netty.server.NettyDriver |
| 10 | + |
| 11 | +object RoutesPrecedentsSpec extends ZIOSpecDefault { |
| 12 | + |
| 13 | + trait MyService { |
| 14 | + def code: UIO[Int] |
| 15 | + } |
| 16 | + object MyService { |
| 17 | + def live(code: Int): ULayer[MyService] = ZLayer.succeed(new MyServiceLive(code)) |
| 18 | + } |
| 19 | + final class MyServiceLive(_code: Int) extends MyService { |
| 20 | + def code: UIO[Int] = ZIO.succeed(_code) |
| 21 | + } |
| 22 | + |
| 23 | + val endpoint: Endpoint[Unit, String, ZNothing, Int, AuthType.None] = |
| 24 | + Endpoint(RoutePattern.POST / "api").in[String].out[Int] |
| 25 | + |
| 26 | + val api = endpoint.implement(_ => ZIO.serviceWithZIO[MyService](_.code)) |
| 27 | + |
| 28 | + // when adding the same route multiple times to the server, the last one should take precedence |
| 29 | + override def spec: Spec[TestEnvironment & Scope, Any] = |
| 30 | + test("test") { |
| 31 | + check(Gen.fromIterable(List(1, 2, 3, 4, 5))) { code => |
| 32 | + ( |
| 33 | + for { |
| 34 | + client <- ZIO.service[Client] |
| 35 | + port <- ZIO.serviceWithZIO[Server](_.port) |
| 36 | + url = URL.root.port(port) / "api" |
| 37 | + request = Request |
| 38 | + .post(url = url, body = Body.fromString(""""this is some input"""")) |
| 39 | + .addHeader(Header.Accept(MediaType.application.json)) |
| 40 | + _ <- TestServer.addRoutes(api.toRoutes) |
| 41 | + result <- client.batched(request) |
| 42 | + output <- result.body.asString |
| 43 | + } yield assertTrue(output == code.toString) |
| 44 | + ).provideSome[TestServer & Client]( |
| 45 | + ZLayer.succeed(new MyServiceLive(code)), |
| 46 | + ) |
| 47 | + }.provide( |
| 48 | + ZLayer.succeed(Server.Config.default.onAnyOpenPort), |
| 49 | + TestServer.layer, |
| 50 | + Client.default, |
| 51 | + NettyDriver.customized, |
| 52 | + ZLayer.succeed(NettyConfig.defaultWithFastShutdown), |
| 53 | + ) |
| 54 | + } @@ shrinks(0) |
| 55 | +} |
0 commit comments