diff --git a/zio-http/jvm/src/test/scala/zio/http/RoutePatternSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RoutePatternSpec.scala index 369d03ef01..e5ed9ee28f 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RoutePatternSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RoutePatternSpec.scala @@ -399,6 +399,19 @@ object RoutePatternSpec extends ZIOHttpSpec { assertTrue(routePattern.format((1, "abc")) == Right(Path("/users/1/posts/abc"))) }, ) + def anyOfTests = + suite("anyOf")( + test("anyOf with multiple patterns") { + val routePattern = Method.GET / anyOf("", "index.html", "index.htm") + assertTrue( + routePattern.matches(Method.GET, Path("")), + routePattern.matches(Method.GET, Path("index.html")), + routePattern.matches(Method.GET, Path("index.htm")), + !routePattern.matches(Method.GET, Path("foo")), + !routePattern.matches(Method.POST, Path("index.html")), + ) + }, + ) def spec = suite("RoutePatternSpec")( @@ -406,5 +419,6 @@ object RoutePatternSpec extends ZIOHttpSpec { rendering, formatting, tree, + anyOfTests, ) } diff --git a/zio-http/shared/src/main/scala/zio/http/RoutePattern.scala b/zio-http/shared/src/main/scala/zio/http/RoutePattern.scala index f54cc50c98..a2822c4511 100644 --- a/zio-http/shared/src/main/scala/zio/http/RoutePattern.scala +++ b/zio-http/shared/src/main/scala/zio/http/RoutePattern.scala @@ -164,6 +164,18 @@ object RoutePattern { */ def fromMethod(method: Method): RoutePattern[Unit] = RoutePattern(method, PathCodec.empty) + /** + * Creates a RoutePattern that matches any of the specified paths. + */ + def anyOf(paths: String*): PathCodec[Unit] = { + if (paths.isEmpty) { + PathCodec.empty + } else { + val codecs = paths.map(PathCodec.literal) + codecs.reduceLeftOption(_ ++ _).getOrElse(PathCodec.empty) + } + } + /** * A tree of route patterns, indexed by method and path. */