+htp #16543 add directives to add or remove trailing slash

This commit is contained in:
Jisoo Park 2014-12-16 02:41:47 +09:00 committed by Roland Kuhn
parent 9f1d074d27
commit e785c374b3
5 changed files with 137 additions and 10 deletions

View file

@ -405,6 +405,16 @@ object Uri {
def isEmpty: Boolean
def startsWithSlash: Boolean
def startsWithSegment: Boolean
def endsWithSlash: Boolean = {
import Path.{ Empty PEmpty, _ }
@tailrec def check(path: Path): Boolean = path match {
case PEmpty false
case Slash(PEmpty) true
case Slash(tail) check(tail)
case Segment(head, tail) check(tail)
}
check(this)
}
def head: Head
def tail: Path
def length: Int

View file

@ -73,15 +73,8 @@ protected[model] case class JavaUri(uri: model.Uri) extends Uri {
import model.Uri.Path
import Path._
@tailrec def endsWithSlash(path: Path): Boolean = path match {
case Empty false
case Slash(Empty) true
case Slash(tail) endsWithSlash(tail)
case Segment(head, tail) endsWithSlash(tail)
}
val newPath =
if (endsWithSlash(u.path)) u.path ++ Path(segment)
if (u.path.endsWithSlash) u.path ++ Path(segment)
else u.path ++ Path./(segment)
u.withPath(newPath)

View file

@ -241,6 +241,15 @@ class UriSpec extends WordSpec with Matchers {
Path("/abc/def") startsWith Path("/abc/def") shouldBe true
Path("/abc/def") startsWith Path("/abc/def/") shouldBe false
}
"support the `endsWithSlash` predicate" in {
Empty.endsWithSlash shouldBe false
Path./.endsWithSlash shouldBe true
Path("abc").endsWithSlash shouldBe false
Path("abc/").endsWithSlash shouldBe true
Path("/abc").endsWithSlash shouldBe false
Path("/abc/def").endsWithSlash shouldBe false
Path("/abc/def/").endsWithSlash shouldBe true
}
"support the `dropChars` modifier" in {
Path./.dropChars(0) shouldEqual Path./
Path./.dropChars(1) shouldEqual Empty