Skip to content

Commit

Permalink
routeParams are now properly urldecoded, fixes #68
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Capote committed Jan 10, 2014
1 parent 5cf334f commit 86ff2d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/scala/com/twitter/finatra/Router.scala
@@ -1,11 +1,12 @@
package com.twitter.finatra

import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import org.jboss.netty.handler.codec.http.HttpMethod
import org.jboss.netty.handler.codec.http.{QueryStringDecoder, HttpMethod}
import scala.collection.mutable.ListBuffer
import scala.collection.Map
import com.twitter.util.Future
import com.twitter.app.App
import org.jboss.netty.util.CharsetUtil

class Router(controller: Controller) extends App with Logging {

Expand Down Expand Up @@ -34,8 +35,11 @@ class Router(controller: Controller) extends App with Logging {
}
}

def extractParams(request: Request, xs: Tuple2[_, _]): Map[String, String] =
request.routeParams += (xs._1.toString -> xs._2.asInstanceOf[ListBuffer[String]].head.toString)
def extractParams(request: Request, xs: Tuple2[_, _]): Map[String, String] = {
val decodedKey = QueryStringDecoder.decodeComponent(xs._1.toString, CharsetUtil.UTF_8)
val decodedValue = QueryStringDecoder.decodeComponent(xs._2.asInstanceOf[ListBuffer[String]].head.toString)
request.routeParams += (decodedKey -> decodedValue)
}

def findRouteAndMatch(request: Request, method: HttpMethod):
Option[(HttpMethod, PathPattern, (Request) => Future[ResponseBuilder])] = {
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/com/twitter/finatra/RouteParamsSpec.scala
@@ -0,0 +1,23 @@
package com.twitter.finatra

import com.twitter.finatra.test.{FlatSpecHelper, SpecHelper}

class RouteParamsSpec extends FlatSpecHelper {

class ExampleApp extends Controller {
get("/:foo") { request =>
val decoded = request.routeParams.get("foo").get
render.plain(decoded).toFuture
}
}

val server = new FinatraServer
server.register(new ExampleApp)

"Response" should "contain decoded params" in {
get("/hello%3Aworld")
response.body should equal("hello:world")
}


}

0 comments on commit 86ff2d2

Please sign in to comment.