Skip to content

Commit

Permalink
Correct base path of reverse routes in non-root contexts. [fixes scal…
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed Sep 21, 2011
1 parent c3d2058 commit 162797b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions core/src/main/scala/org/scalatra/ScalatraFilter.scala
Expand Up @@ -35,6 +35,8 @@ trait ScalatraFilter extends Filter with ScalatraKernel with Initializable {
// Unlike the Scalatra servlet, we'll use both here by default. Don't like it? Override it.
def requestPath = request.getServletPath + (if (request.getPathInfo != null) request.getPathInfo else "")

protected def routeBasePath = request.getContextPath

protected var doNotFound: Action = () => filterChain.doFilter(request, response)

var servletContext: ServletContext = _
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/org/scalatra/ScalatraKernel.scala
Expand Up @@ -438,11 +438,16 @@ trait ScalatraKernel extends Handler with CoreDsl with Initializable
* @see org.scalatra.ScalatraKernel#removeRoute
*/
protected def addRoute(method: HttpMethod, routeMatchers: Iterable[RouteMatcher], action: => Any): Route = {
val route = Route(routeMatchers, () => action, () => request.getServletPath)
val route = Route(routeMatchers, () => action, () => routeBasePath)
routes.prependRoute(method, route)
route
}

/**
* The base path for URL generation
*/
protected def routeBasePath: String

@deprecated("Use addRoute(HttpMethod, Iterable[RouteMatcher], =>Any)", "2.0")
protected[scalatra] def addRoute(verb: String, routeMatchers: Iterable[RouteMatcher], action: => Any): Route =
addRoute(HttpMethod(verb), routeMatchers, action)
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/org/scalatra/ScalatraServlet.scala
Expand Up @@ -46,6 +46,8 @@ abstract class ScalatraServlet
case null => "/"
}

protected def routeBasePath = request.getContextPath + request.getServletPath

/**
* Invoked when no route matches. By default, calls `serveStaticResource()`,
* and if that fails, calls `resourceNotFound()`.
Expand Down
54 changes: 47 additions & 7 deletions core/src/test/scala/org/scalatra/UrlGeneratorSupportTest.scala
Expand Up @@ -2,26 +2,66 @@ package org.scalatra

import test.scalatest.ScalatraFunSuite

trait UrlGeneratorContextTestServlet extends ScalatraServlet {
class UrlGeneratorContextTestServlet extends ScalatraServlet {
val servletRoute: Route = get("/foo") { UrlGenerator.url(servletRoute) }
}

val foo: Route = get("/foo") { UrlGenerator.url(foo) }
class UrlGeneratorContextTestFilter extends ScalatraFilter {
val filterRoute: Route = get("/filtered/foo") {
UrlGenerator.url(filterRoute)
}
}

class UrlGeneratorSupportTest extends ScalatraFunSuite {

addServlet(new UrlGeneratorContextTestServlet {}, "/*")

addServlet(new UrlGeneratorContextTestServlet {}, "/context/*")
addServlet(new UrlGeneratorContextTestServlet, "/*")
addServlet(new UrlGeneratorContextTestServlet, "/servlet-path/*")
addServlet(new UrlGeneratorContextTestServlet, "/filtered/*")
addFilter(new UrlGeneratorContextTestFilter, "/*")

test("Url of a servlet mounted on /*") {
get("/foo") {
body should equal ("/foo")
}
}

test("Url of a servlet mounted on /context/*") {
test("Url of a servlet mounted on /servlet-path/*") {
get("/servlet-path/foo") {
body should equal ("/servlet-path/foo")
}
}

test("Url of a filter does not duplicate the servlet path") {
get("/filtered/foo") {
body should equal ("/filtered/foo")
}
}
}

class UrlGeneratorNonRootContextSupportTest extends ScalatraFunSuite {
tester.setContextPath("/context")

addServlet(new UrlGeneratorContextTestServlet, "/*")
addServlet(new UrlGeneratorContextTestServlet, "/servlet-path/*")
addServlet(new UrlGeneratorContextTestServlet, "/filtered/*")
addFilter(new UrlGeneratorContextTestFilter, "/*")

test("Url of a servlet mounted on /*") {
get("/context/foo") {
body should equal ("/context/foo")
}
}

test("Url of a servlet mounted on /servlet-path/*") {
get("/context/servlet-path/foo") {
body should equal ("/context/servlet-path/foo")
}
}

test("Url of a filter does not duplicate the servlet path") {
get("/context/filtered/foo") {
body should equal ("/context/filtered/foo")
}
}
}


1 change: 1 addition & 0 deletions notes/2.0.1.markdown
@@ -1,2 +1,3 @@
### scalatra
* `redirect` halts the action immediately. [(GH-132)](http://github.com/scalatra/scalatra/issues/132)
* Correct base path of reverse routes in non-root contexts. [(GH-130)](http://github.com/scalatra/scalatra/issues/130)

0 comments on commit 162797b

Please sign in to comment.