Skip to content

Commit

Permalink
Lifts request and response into kernel proxy, allows access to header…
Browse files Browse the repository at this point in the history
…s etc
  • Loading branch information
casualjim committed Aug 21, 2010
1 parent 5166d9b commit 520cb93
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Expand Up @@ -15,6 +15,8 @@ class ScalatraKernelProxy {
private var _session: () => HttpSession = _
private var _params: () => collection.Map[String, String] = _
private var _redirect: String => Unit = _
private var _request: () => HttpServletRequest = _
private var _response: () => HttpServletResponse = _

def session = _session()
private[auth] def session_=(sess: => HttpSession) = {
Expand All @@ -29,14 +31,22 @@ class ScalatraKernelProxy {
def redirect(uri: String) = _redirect(uri)
private[auth] def redirect_=(redirectFunction: String => Unit) = _redirect = redirectFunction

def request = _request()
def response = _response()
def request_=(req: => HttpServletRequest) = _request = () => req
def response_=(res: => HttpServletResponse) = _response = () => res

}

object ScalatraKernelProxy {
def apply(session: => HttpSession, params: => collection.Map[String, String], redirect: String => Unit) ={
def apply(session: => HttpSession, params: => collection.Map[String, String], redirect: String => Unit,
request: => HttpServletRequest, response: => HttpServletResponse ) ={
val ctxt = new ScalatraKernelProxy
ctxt.session = session
ctxt.params = params
ctxt.redirect_=(redirect)
ctxt.response = response
ctxt.request = request
ctxt
}

Expand Down
Expand Up @@ -31,7 +31,7 @@ trait ScentrySupport[TypeForUser <: AnyRef] extends Handler with Initializable {
}

abstract override def handle(servletRequest: HttpServletRequest, servletResponse: HttpServletResponse) = {
val app = ScalatraKernelProxy(session, params, redirect _)
val app = ScalatraKernelProxy(session, params, redirect _, request, response)
_scentry.withValue(new Scentry[UserType](app, toSession, fromSession)) {
registerStrategiesFromConfig
registerAuthStrategies
Expand Down
11 changes: 7 additions & 4 deletions scentry/src/test/scala/org/scalatra/auth/ScentrySpec.scala
Expand Up @@ -4,9 +4,7 @@ import org.specs._
import mock.Mockito
import org.mockito.Matchers._
import runner.{ScalaTest, JUnit}
import javax.servlet.http.HttpSession


import javax.servlet.http.{HttpServletResponse, HttpServletRequest, HttpSession}

object ScentrySpec extends Specification with Mockito with JUnit with ScalaTest {
detailedDiffs
Expand All @@ -18,7 +16,12 @@ object ScentrySpec extends Specification with Mockito with JUnit with ScalaTest
session.getAttribute(Scentry.scentryAuthKey) returns "6789"
var invalidateCalled = false
session.invalidate answers { _ => invalidateCalled = true }
val context = ScalatraKernelProxy(session, smartMock[scala.collection.Map[String, String]], s => "redirected to: " + s)
val context = ScalatraKernelProxy(
session,
smartMock[scala.collection.Map[String, String]],
s => "redirected to: " + s,
smartMock[HttpServletRequest],
smartMock[HttpServletResponse])
val theScentry = new Scentry[User](context, { case User(id) => id }, { case s: String => User(s)})
var beforeFetchCalled = false
var afterFetchCalled = false
Expand Down

0 comments on commit 520cb93

Please sign in to comment.