Skip to content

Commit

Permalink
Handle special characters in paths: .$+()
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed Apr 9, 2010
1 parent 98c8b5e commit 76979c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/scala/com/thinkminimo/step/StepKernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ trait StepKernel
var names = new ListBuffer[String]
var pos = 0
var regex = new StringBuffer("^")
""":\w+|\*""".r.findAllIn(path).matchData foreach { md =>
val specialCharacters = List('.', '+', '(', ')')
""":\w+|[\*\.\+\(\)\$]""".r.findAllIn(path).matchData foreach { md =>
regex.append(path.substring(pos, md.start))
md.toString match {
case "*" =>
names += ":splat"
regex.append("(.*?)")
case "." | "+" | "(" | ")" | "$" =>
regex.append("\\").append(md.toString)
case x =>
names += x
regex.append("([^/?]+)")
Expand Down
60 changes: 60 additions & 0 deletions src/test/scala/com/thinkminimo/step/RouteTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ class RouteTestServlet extends Step {
get("/mix-named-and-splat-params/:foo/*") {
params(":foo")+":"+params(":splat")
}

get("/dot-in-named-param/:foo/:bar") {
params(":foo")
}

get("/dot-outside-named-param/:file.:ext") {
List(":file", ":ext") foreach { x => response.setHeader(x, params(x)) }
}

get("/literal.dot.in.path") {
"matched literal dot"
}

get("/test$") {
"test$"
}

get("/te+st") {
"te+st"
}

get("/test(bar)") {
"test(bar)"
}
}

class RouteTest extends StepSuite with ShouldMatchers {
Expand Down Expand Up @@ -72,4 +96,40 @@ class RouteTest extends StepSuite with ShouldMatchers {
}
}

test("matches a dot ('.') as part of a named param") {
get("/dot-in-named-param/user@example.com/name") {
body should equal("user@example.com")
}
}

test("matches a literal dot ('.') outside of named params") {
get("/dot-outside-named-param/pony.jpg") {
header("file") should equal ("pony")
header("ext") should equal ("jpg")
}
}

test("literally matches . in paths") {
get("/literal.dot.in.path") {
body should equal("matched literal dot")
}
}

test("literally matches $ in paths") {
get("/test$") {
body should equal ("test$")
}
}

test("literally matches + in paths") {
get("/te+st") {
body should equal ("te+st")
}
}

test("literally matches () in paths") {
get("/test(bar)") {
body should equal ("test(bar)")
}
}
}

0 comments on commit 76979c6

Please sign in to comment.