Skip to content

Commit

Permalink
Merge branch 'main' into docs-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 authored Aug 8, 2024
2 parents ce08739 + 41bb5c2 commit d85cff9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
32 changes: 28 additions & 4 deletions zio-http/jvm/src/test/scala/zio/http/RoutePatternSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package zio.http

import java.util.UUID

import scala.collection.Seq

import zio.Chunk
Expand Down Expand Up @@ -106,8 +108,10 @@ object RoutePatternSpec extends ZIOHttpSpec {

tree = tree.add(pattern, ())

assertTrue(tree.get(Method.GET, Path("/users")).nonEmpty) &&
assertTrue(tree.get(Method.POST, Path("/users")).isEmpty)
assertTrue(
tree.get(Method.GET, Path("/users")).nonEmpty,
tree.get(Method.POST, Path("/users")).isEmpty,
)
},
test("GET /users/{user-id}/posts/{post-id}") {
var tree: Tree[Unit] = RoutePattern.Tree.empty
Expand All @@ -116,8 +120,28 @@ object RoutePatternSpec extends ZIOHttpSpec {

tree = tree.add(pattern, ())

assertTrue(tree.get(Method.GET, Path("/users/1/posts/abc")).nonEmpty) &&
assertTrue(tree.get(Method.GET, Path("/users/abc/posts/1")).isEmpty)
assertTrue(
tree.get(Method.GET, Path("/users/1/posts/abc")).nonEmpty,
tree.get(Method.GET, Path("/users/abc/posts/1")).isEmpty,
)
},
test("GET /users/{string-param}/{user-id:uuid} issue 3005") {
val routePattern1 = Method.GET / "users" / string("param") / "abc" / uuid("id") / "hello"
val routePattern2 = Method.GET / "users" / string("param") / uuid("id") / "hello"

val id = UUID.randomUUID()
var tree: Tree[Int] = RoutePattern.Tree.empty
tree = tree.add(routePattern1, 1)
tree = tree.add(routePattern2, 2)

val p1 = Path(s"/users/some_value/abc/$id/hello")
val p2 = Path(s"/users/some_value/$id/hello")
assertTrue(
routePattern1.decode(Method.GET, p1) == Right(("some_value", id)),
routePattern2.decode(Method.GET, p2) == Right(("some_value", id)),
tree.get(Method.GET, p1).contains(1),
tree.get(Method.GET, p2).contains(2),
)
},
test("on conflict, first one wins") {
var tree: Tree[Int] = RoutePattern.Tree.empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ object PathCodecSpec extends ZIOHttpSpec {
val p = s"/abc/foo/${id}__13/bar/42"
assertTrue(codec.decode(Path(p)) == Right(("abc", id, 13, 42)))
},
test("uuid after string") {
val codec = PathCodec.empty / "foo" / "bar" / string("baz") / "xyz" / uuid(
"id",
) / "abc"
val id = UUID.randomUUID()
val p = s"/foo/bar/some_value/xyz/$id/abc"
assertTrue(codec.decode(Path(p)) == Right(("some_value", id)))
},
test("string before literal") {
val codec = PathCodec.empty /
string("foo") /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ sealed trait PathCodec[A] { self =>
val t = value.regionMatches(true, idx, "true", 0, 4)
if (t) idx + 4 else if (value.regionMatches(true, idx, "false", 0, 5)) idx + 5 else -1
case UUIDOpt =>
val until = SegmentCodec.UUID.isUUIDUntil(value, idx)
val until = SegmentCodec.UUID.inUUIDUntil(value, idx)
if (until == -1) -1 else idx + until
case MatchAny(values) =>
var end = -1
Expand Down
11 changes: 6 additions & 5 deletions zio-http/shared/src/main/scala/zio/http/codec/SegmentCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,23 +440,24 @@ object SegmentCodec {
if (index < 0 || index >= segments.length) -1
else {
val lastIndex = inSegmentUntil(segments(index), 0)
if (lastIndex == -1 || lastIndex + 1 != segments(index).length) -1
if (lastIndex == -1 || lastIndex != segments(index).length) -1
else 1
}
}

override def inSegmentUntil(segment: String, from: Int): Int =
UUID.isUUIDUntil(segment, from)
UUID.inUUIDUntil(segment, from)
}

private[http] object UUID {
def isUUIDUntil(segment: String, from: Int): Int = {
def inUUIDUntil(segment: String, from: Int): Int = {
var i = from
var defined = true
var group = 0
var count = 0
if (segment.length + from < 36) return -1
while (i < 36 && defined) {
val until = from + 36
while (i < until && defined) {
val char = segment.charAt(i)
if ((char >= 48 && char <= 57) || (char >= 65 && char <= 70) || (char >= 97 && char <= 102))
count += 1
Expand All @@ -475,7 +476,7 @@ object SegmentCodec {
}
i += 1
}
if (defined && from + 36 == i) i else -1
if (defined && until == i) i else -1
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zio.http.codec

import java.util.UUID

import scala.util._

import zio._
Expand Down Expand Up @@ -91,5 +93,13 @@ object SegmentCodecSpec extends ZIOSpecDefault {
uuidLongInt.failed.toOption.map(_.getMessage).contains(expectedErrorMsg),
)
},
suite("matches")(
test("uuid successful matches") {
val codec = SegmentCodec.uuid("entityId")
val uuid = new UUID(101, 304)
val path = Chunk("api", uuid.toString())
assertTrue(codec.matches(path, 1) == 1)
},
),
)
}

0 comments on commit d85cff9

Please sign in to comment.