forked from zio/zio-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make newly added Routes take precedence over old ones (zio#3066)
- Loading branch information
Showing
7 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
zio-http-testkit/src/test/scala/zio/http/RoutesPrecedentsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package zio.http | ||
|
||
import zio._ | ||
import zio.test.TestAspect.shrinks | ||
import zio.test._ | ||
|
||
import zio.http.endpoint.{AuthType, Endpoint} | ||
import zio.http.netty.NettyConfig | ||
import zio.http.netty.server.NettyDriver | ||
|
||
object RoutesPrecedentsSpec extends ZIOSpecDefault { | ||
|
||
trait MyService { | ||
def code: UIO[Int] | ||
} | ||
object MyService { | ||
def live(code: Int): ULayer[MyService] = ZLayer.succeed(new MyServiceLive(code)) | ||
} | ||
final class MyServiceLive(_code: Int) extends MyService { | ||
def code: UIO[Int] = ZIO.succeed(_code) | ||
} | ||
|
||
val endpoint: Endpoint[Unit, String, ZNothing, Int, AuthType.None] = | ||
Endpoint(RoutePattern.POST / "api").in[String].out[Int] | ||
|
||
val api = endpoint.implement(_ => ZIO.serviceWithZIO[MyService](_.code)) | ||
|
||
// when adding the same route multiple times to the server, the last one should take precedence | ||
override def spec: Spec[TestEnvironment & Scope, Any] = | ||
test("test") { | ||
check(Gen.fromIterable(List(1, 2, 3, 4, 5))) { code => | ||
( | ||
for { | ||
client <- ZIO.service[Client] | ||
port <- ZIO.serviceWithZIO[Server](_.port) | ||
url = URL.root.port(port) / "api" | ||
request = Request | ||
.post(url = url, body = Body.fromString(""""this is some input"""")) | ||
.addHeader(Header.Accept(MediaType.application.json)) | ||
_ <- TestServer.addRoutes(api.toRoutes) | ||
result <- client.batched(request) | ||
output <- result.body.asString | ||
} yield assertTrue(output == code.toString) | ||
).provideSome[TestServer & Client]( | ||
ZLayer.succeed(new MyServiceLive(code)), | ||
) | ||
}.provide( | ||
ZLayer.succeed(Server.Config.default.onAnyOpenPort), | ||
TestServer.layer, | ||
Client.default, | ||
NettyDriver.customized, | ||
ZLayer.succeed(NettyConfig.defaultWithFastShutdown), | ||
) | ||
} @@ shrinks(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters