Skip to content

Commit

Permalink
remove request from extractors that extract other things
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hamblen committed Nov 28, 2010
1 parent e6f5011 commit 29912f1
Show file tree
Hide file tree
Showing 28 changed files with 75 additions and 74 deletions.
8 changes: 4 additions & 4 deletions filter/src/test/scala/MixedServerSpec.scala
Expand Up @@ -27,10 +27,10 @@ object MixedServerSpec extends Specification with spec.jetty.Served with spec.Se
})

def setup = { _.filter(unfiltered.filter.Planify {
case GET(UFPath("/", r)) => ResponseString("public") ~> Ok
case HTTP(GET(UFPath("/https_only", r))) => Redirect("https://localhost/https_only")
case HTTPS(GET(UFPath("/https_only", r))) => ResponseString("secret") ~> Ok
case HTTPS(GET(UFPath("/tryme", r))) => ResponseString("secret") ~> Ok
case GET(UFPath("/")) => ResponseString("public") ~> Ok
case HTTP(GET(UFPath("/https_only"))) => Redirect("https://localhost/https_only")
case HTTPS(GET(UFPath("/https_only"))) => ResponseString("secret") ~> Ok
case HTTPS(GET(UFPath("/tryme"))) => ResponseString("secret") ~> Ok
})}

"A Mixed Secure Server" should {
Expand Down
8 changes: 4 additions & 4 deletions filter/src/test/scala/PassAndThenSpec.scala
Expand Up @@ -11,17 +11,17 @@ object PassAndThenSpec extends Specification with unfiltered.spec.jetty.Served {
import dispatch._

class Around extends Planify({
case GET(UFPath("/h", _)) => PassAndThen after {
case GET(UFPath("/h")) => PassAndThen after {
case _ => new HeaderName("x-test")("passed")
}
case GET(UFPath("/b", _)) => PassAndThen after {
case GET(UFPath("/b")) => PassAndThen after {
case _ => ResponseString("""[{"msg":"howdy partner"}]""")
}
})

class TestPlan extends Planify({
case GET(UFPath("/h", _)) => JsonContent ~> ResponseString("""[{"msg":"howdy partner"}]""")
case GET(UFPath("/b", _)) => JsonContent
case GET(UFPath("/h")) => JsonContent ~> ResponseString("""[{"msg":"howdy partner"}]""")
case GET(UFPath("/b")) => JsonContent
})

def setup = { _.filter(new Around).filter(new TestPlan) }
Expand Down
22 changes: 11 additions & 11 deletions filter/src/test/scala/PlanSpec.scala
Expand Up @@ -12,47 +12,47 @@ object PlanSpec extends Specification with unfiltered.spec.jetty.Served {

def setup = {
_.filter(Planify {
case GET(UFPath("/filter", _)) => Pass
case GET(UFPath("/filter")) => Pass
case _ => println("nonmatching first"); Pass
}).filter(Planify {
case GET(UFPath("/filter", _)) => ResponseString("test") ~> Ok
case GET(UFPath("/filter")) => ResponseString("test") ~> Ok
}).context("/filter2") {
_.filter(Planify {
case GET(UFPath("/test2", _)) => ResponseString("test2") ~> Ok
case GET(UFPath("/test2")) => ResponseString("test2") ~> Ok
})
}.context("/filter3") {
_.filter(new Plan { def intent = {
case GET(UFPath(Seg("aplan" :: Nil), r)) =>
case GET(UFPath(Seg("aplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan A")
}})
.filter(new Plan { def intent = {
case GET(UFPath(Seg("bplan" :: Nil), r)) =>
case GET(UFPath(Seg("bplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan B")
}})
}.context("/filter4") {
_.filter(new unfiltered.filter.Planify({
case GET(UFPath(Seg("aplan" :: Nil), r)) =>
case GET(UFPath(Seg("aplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan A")
}))
.filter(new unfiltered.filter.Planify({
case GET(UFPath(Seg("bplan" :: Nil), r)) =>
case GET(UFPath(Seg("bplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan B")
}))
.filter(new unfiltered.filter.Planify({
case GET(UFPath(Seg("cplan" :: Nil), r)) =>
case GET(UFPath(Seg("cplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan C")
}))
}.context("/filter5") {
_.filter(unfiltered.filter.Planify {
case GET(UFPath(Seg("aplan" :: Nil), r)) =>
case GET(UFPath(Seg("aplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan A")
})
.filter(unfiltered.filter.Planify {
case GET(UFPath(Seg("bplan" :: Nil), r)) =>
case GET(UFPath(Seg("bplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan B")
})
.filter(unfiltered.filter.Planify {
case GET(UFPath(Seg("cplan" :: Nil), r)) =>
case GET(UFPath(Seg("cplan" :: Nil))) =>
Ok ~> ContentType("text/html") ~> ResponseString("Plan C")
})
}
Expand Down
6 changes: 3 additions & 3 deletions filter/src/test/scala/ServerSpec.scala
Expand Up @@ -10,9 +10,9 @@ object ServerSpec extends Specification with unfiltered.spec.jetty.Served {
import dispatch._

def setup = _.filter(unfiltered.filter.Planify {
case GET(UFPath("/", _)) => ResponseString("test") ~> Ok
case GET(UFPath("/addr", r)) => ResponseString(r.remoteAddr) ~> Ok
case GET(UFPath("/addr_extractor", RemoteAddr(addr, _))) => ResponseString(addr) ~> Ok
case GET(UFPath("/")) => ResponseString("test") ~> Ok
case r @ GET(UFPath("/addr")) => ResponseString(r.remoteAddr) ~> Ok
case GET(UFPath("/addr_extractor") & RemoteAddr(addr)) => ResponseString(addr) ~> Ok
})

"A Server" should {
Expand Down
2 changes: 1 addition & 1 deletion filter/src/test/scala/SslServerSpec.scala
Expand Up @@ -26,7 +26,7 @@ object SslServerSpec extends Specification with spec.jetty.Served with spec.Secu
})

def setup = { _.filter(unfiltered.filter.Planify {
case GET(UFPath("/", _)) => ResponseString("secret") ~> Ok
case GET(UFPath("/")) => ResponseString("secret") ~> Ok
})}

"A Secure Server" should {
Expand Down
4 changes: 2 additions & 2 deletions json/src/main/scala/json.scala
Expand Up @@ -33,7 +33,7 @@ object Jsonp {
is provided else (emptywrapper, req) tuple is no callback param is provided */
object Optional {
def unapply[T](r: HttpRequest[T]) = r match {
case Accepts.Jsonp(Params(p, req)) =>
case Accepts.Jsonp(Params(p)) =>
Some(p match {
case Callback(cb, _) => new CallbackWrapper(cb)
case _ => EmptyWrapper
Expand All @@ -45,7 +45,7 @@ object Jsonp {
/** @return (callbackwrapper, req) tuple if request accepts json and a callback
param is provided */
def unapply[T](r: HttpRequest[T]) = r match {
case Accepts.Jsonp(Params(Callback(cb, _), _)) => Some(new CallbackWrapper(cb), r)
case Accepts.Jsonp(Params(Callback(cb, _))) => Some(new CallbackWrapper(cb), r)
case _ => None
}
}
2 changes: 1 addition & 1 deletion json/src/test/scala/JsonBodySpec.scala
Expand Up @@ -10,7 +10,7 @@ object JsonBodySpec extends Specification with unfiltered.spec.jetty.Served {
import dispatch._

class TestPlan extends unfiltered.filter.Planify({
case POST(UFPath("/", r)) => ResponseString(JsonBody(r) match {
case r @ POST(UFPath("/")) => ResponseString(JsonBody(r) match {
case Some(net.liftweb.json.JsonAST.JArray(a :: b :: Nil)) => "array of 2"
case _ => "expected json array of 2"
})
Expand Down
4 changes: 2 additions & 2 deletions json/src/test/scala/JsonpSpec.scala
Expand Up @@ -10,8 +10,8 @@ object JsonpSpec extends Specification with unfiltered.spec.jetty.Served {
import dispatch._

class TestPlan extends unfiltered.filter.Planify({
case GET(UFPath("/jsonp/with-callback", Jsonp(cb, _))) => ResponseString(cb.wrap("[42]"))
case GET(UFPath("/jsonp/with-optional-callback", Jsonp.Optional(cb, _))) => ResponseString(cb.wrap("[42]"))
case GET(UFPath("/jsonp/with-callback") & Jsonp(cb, _)) => ResponseString(cb.wrap("[42]"))
case GET(UFPath("/jsonp/with-optional-callback") & Jsonp.Optional(cb, _)) => ResponseString(cb.wrap("[42]"))
case _ => ResponseString("bad req")
})

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/scala/request/accepts.scala
Expand Up @@ -11,7 +11,7 @@ object Accepts {
def unapply[T](r: HttpRequest[T]) = {
val pathSuffix = r.requestURI.substring(r.contextPath.length).split("[.]").lastOption
r match {
case Accept(values, _) =>
case Accept(values) =>
if(values.exists { _.equalsIgnoreCase(contentType) })
Some(r)
else if (values.exists { _ == "*/*" } && pathSuffix.exists { ext == _ })
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/scala/request/auths.scala
Expand Up @@ -5,14 +5,14 @@ object BasicAuth {
import org.apache.commons.codec.binary.Base64.{decodeBase64}
import unfiltered.request.{HttpRequest => Req}

/** @return Some(((user, pass), req)) or None */
/** @return Some(user, pass) or None */
def unapply[T](r: Req[T]) = r match {
case Authorization(Seq(auth), _) => {
case Authorization(Seq(auth)) => {
val tok = new java.util.StringTokenizer(auth)
tok.nextToken match {
case "Basic" =>
new String(decodeBase64(tok.nextToken getBytes("utf8"))) split(":") match {
case Array(u, p) => Some(((u, p), r))
case Array(u, p) => Some(u, p)
case _ => None
}
case _ => None
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/scala/request/cookies.scala
Expand Up @@ -5,7 +5,7 @@ import unfiltered.Cookie
object Cookies {
def unapply[T](r: HttpRequest[T]) =
Some((((Map.empty[String, Option[Cookie]] /: r.cookies)(
(m,c) => m + (c.name -> Some(c)))).withDefaultValue(None), r))
(m,c) => m + (c.name -> Some(c)))).withDefaultValue(None)))

def apply[T](r: HttpRequest[T]): Map[String, Option[Cookie]] = Cookies.unapply(r).get._1
}
def apply[T](r: HttpRequest[T]): Map[String, Option[Cookie]] = Cookies.unapply(r).get
}
2 changes: 1 addition & 1 deletion library/src/main/scala/request/headers.scala
Expand Up @@ -12,7 +12,7 @@ class RequestHeader(val name: String) {

headers(req.headers(name)) match {
case Nil => None
case hs => Some(hs, req)
case hs => Some(hs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/scala/request/params.scala
Expand Up @@ -14,7 +14,7 @@ object Params {
val names = req.parameterNames
Some(((Map.empty[String, Seq[String]] /: names) ((m, n) =>
m + (n -> req.parameterValues(n))
)).withDefaultValue(Nil), req)
)).withDefaultValue(Nil))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/scala/request/paths.scala
@@ -1,7 +1,7 @@
package unfiltered.request

object Path {
def unapply[T](req: HttpRequest[T]) = Some((req.requestURI.substring(req.contextPath.length), req))
def unapply[T](req: HttpRequest[T]) = Some(req.requestURI.substring(req.contextPath.length))
}
object Seg {
def unapply(path: String): Option[List[String]] = path.split("/").toList match {
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/scala/request/readers.scala
@@ -1,18 +1,18 @@
package unfiltered.request

object InStream {
def unapply[T](req: HttpRequest[T]) = Some(req.inputStream, req)
def unapply[T](req: HttpRequest[T]) = Some(req.inputStream)
}

object Read {
def unapply[T](req: HttpRequest[T]) = Some(req.reader, req)
def unapply[T](req: HttpRequest[T]) = Some(req.reader)
}

object Bytes {
/** This extractor has a side effect and will go away soon */
@deprecated
def unapply[T](req: HttpRequest[T]) = {
val InStream(in, _) = req
val InStream(in) = req
val bos = new java.io.ByteArrayOutputStream
val ba = new Array[Byte](4096)
/* @scala.annotation.tailrec */ def read {
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/scala/request/remotes.scala
Expand Up @@ -9,12 +9,12 @@ object RemoteAddr {
* private IP 192.168.x.x
*/
val TrustedProxies = """(^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|3[0-1])|192\.168)\.\S+)""".r
def unapply[T](req: HttpRequest[T]) = Some((req match {
case XForwardedFor(forwarded, _) =>
def unapply[T](req: HttpRequest[T]) = Some(req match {
case XForwardedFor(forwarded) =>
forwarded.filter(!TrustedProxies.findFirstMatchIn(_).isDefined) match {
case addr :: _ => addr
case _ => req.remoteAddr
}
case _ => req.remoteAddr
}, req))
}
})
}
3 changes: 3 additions & 0 deletions library/src/main/scala/request/utils.scala
@@ -0,0 +1,3 @@
package unfiltered.request

object & { def unapply[A](a: A) = Some(a, a) }
8 changes: 4 additions & 4 deletions library/src/test/scala/AcceptsSpec.scala
Expand Up @@ -18,10 +18,10 @@ trait AcceptsSpec extends unfiltered.spec.Hosted {
import dispatch._

def intent[A,B]: unfiltered.Cycle.Intent[A,B] = {
case GET(UFPath(Seg(ext :: Nil), Accepts.Json(_))) => ResponseString("json")
case GET(UFPath(Seg(ext :: Nil), Accepts.Xml(_))) => ResponseString("xml")
case GET(UFPath(Seg(ext :: Nil), Accepts.Csv(_))) => ResponseString("csv")
case GET(UFPath(Seg(ext :: Nil), Accepts.Html(_))) => ResponseString("html")
case GET(UFPath(Seg(ext :: Nil)) & Accepts.Json(_)) => ResponseString("json")
case GET(UFPath(Seg(ext :: Nil)) & Accepts.Xml(_)) => ResponseString("xml")
case GET(UFPath(Seg(ext :: Nil)) & Accepts.Csv(_)) => ResponseString("csv")
case GET(UFPath(Seg(ext :: Nil)) & Accepts.Html(_)) => ResponseString("html")
}

"Accepts should" should {
Expand Down
2 changes: 1 addition & 1 deletion library/src/test/scala/BasicAuthSpec.scala
Expand Up @@ -18,7 +18,7 @@ trait BasicAuthSpec extends unfiltered.spec.Hosted {
import dispatch._

def intent[A,B]: unfiltered.Cycle.Intent[A,B] = {
case GET(UFPath("/secret", BasicAuth(creds, _))) => creds match {
case GET(UFPath("/secret") & BasicAuth(name, pass)) => (name, pass) match {
case ("test", "secret") => ResponseString("pass")
case _ => ResponseString("fail")
}
Expand Down
12 changes: 6 additions & 6 deletions library/src/test/scala/ParamsSpec.scala
Expand Up @@ -21,20 +21,20 @@ trait ParamsSpec extends unfiltered.spec.Hosted {
object Number extends Params.Extract("number", Params.first ~> Params.int)

def intent[A,B]: unfiltered.Cycle.Intent[A,B] = {
case UFPath("/basic", Params(params, _)) => params("foo") match {
case UFPath("/basic") & Params(params) => params("foo") match {
case Seq(foo) => ResponseString("foo is %s" format foo)
case _ => ResponseString("what's foo?")
}

case POST(UFPath("/extract", Params(Number(num, _), _))) =>
case POST(UFPath("/extract") & Params(Number(num, _))) =>
ResponseString(num.toString)

case POST(UFPath("/extract",_)) =>
case POST(UFPath("/extract")) =>
ResponseString("passed")

// QParam test paths:

case GET(UFPath("/int", Params(params, _))) =>
case GET(UFPath("/int") & Params(params)) =>
val expected = for {
even <- lookup("number") is(int(_ => ()))
} yield ResponseString(even.get.toString)
Expand All @@ -44,7 +44,7 @@ trait ParamsSpec extends unfiltered.spec.Hosted {
)
}

case GET(UFPath("/even", Params(params, _))) =>
case GET(UFPath("/even") & Params(params)) =>
val expected = for {
even <- lookup("number") is(int(in => "%s is not a number".format(in))) is
(pred { (_: Int) % 2 == 0} { i => "%d is odd".format(i) }) is(required("missing"))
Expand All @@ -56,7 +56,7 @@ trait ParamsSpec extends unfiltered.spec.Hosted {
)
}

case GET(UFPath("/str", Params(params, _))) =>
case GET(UFPath("/str") & Params(params)) =>
val expected = for {
str <- lookup("param") is(int(_ => 400)) is(optional[Int,Int]) // note: 2.8 can infer type on optional
req <- lookup("req") is(required(400))
Expand Down
2 changes: 1 addition & 1 deletion netty/src/main/scala/bindings.scala
Expand Up @@ -29,7 +29,7 @@ private [netty] class RequestBinding(msg: ReceivedMessage) extends HttpRequest(m
case _ => Map.empty[String,Seq[String]]
}
def postParams = this match {
case POST(RequestContentType(ct, _)) if ct.contains("application/x-www-form-urlencoded") =>
case POST(RequestContentType(ct)) if ct.contains("application/x-www-form-urlencoded") =>
URLParser.urldecode(req.getContent.toString(JNIOCharset.forName(charset)))
case _ => Map.empty[String,Seq[String]]
}
Expand Down
6 changes: 3 additions & 3 deletions netty/src/test/scala/ServerSpec.scala
Expand Up @@ -9,9 +9,9 @@ object ServerSpec extends unfiltered.spec.netty.Served {
import dispatch._

def setup = NHttp(_).handler(cycle.Planify({
case GET(UFPath("/", _)) => ResponseString("test") ~> Ok
case GET(UFPath("/addr", r)) => ResponseString(r.remoteAddr) ~> Ok
case GET(UFPath("/addr_extractor", RemoteAddr(addr, _))) => ResponseString(addr) ~> Ok
case GET(UFPath("/")) => ResponseString("test") ~> Ok
case r @ GET(UFPath("/addr")) => ResponseString(r.remoteAddr) ~> Ok
case GET(UFPath("/addr_extractor") & RemoteAddr(addr)) => ResponseString(addr) ~> Ok
}))

"A Server" should {
Expand Down
2 changes: 1 addition & 1 deletion netty/src/test/scala/SslServerSpec.scala
Expand Up @@ -32,7 +32,7 @@ object SslServerSpec extends Specification with spec.netty.Served with spec.Secu
val securePlan = new unfiltered.netty.cycle.Plan with Secured {
import org.jboss.netty.channel.{ChannelHandlerContext, ExceptionEvent}

def intent = { case GET(UFPath("/", _)) => ResponseString("secret") ~> Ok }
def intent = { case GET(UFPath("/")) => ResponseString("secret") ~> Ok }

override def exceptionCaught(ctx: ChannelHandlerContext, e: ExceptionEvent) =
ctx.getChannel.close
Expand Down
2 changes: 1 addition & 1 deletion netty/src/test/scala/spec.scala
Expand Up @@ -44,7 +44,7 @@ class RequestSpec extends Specification {
req.requestURI must_== "/seg1/seg2"
}
"be able to get the Path extracted correctly" in {
val unfiltered.request.Path(path, _) = req
val unfiltered.request.Path(path) = req
path must_== "/seg1/seg2"
}
}
Expand Down
6 changes: 3 additions & 3 deletions oauth/src/main/scala/oauth.scala
Expand Up @@ -47,7 +47,7 @@ trait OAuthed extends OAuthProvider with unfiltered.filter.Plan {
import OAuth._

def intent = {
case POST(Path("/request_token", Authorization(OAuth.Header(headers), Params(params, request)))) =>
case POST(Path("/request_token") & Authorization(OAuth.Header(headers)) & Params(params)) & request =>
val expected = for {
consumer_key <- lookup(ConsumerKey) is
nonempty(blankMsg(ConsumerKey)) is required(requiredMsg(ConsumerKey))
Expand Down Expand Up @@ -77,7 +77,7 @@ trait OAuthed extends OAuthProvider with unfiltered.filter.Plan {
BadRequest ~> ResponseString(fails.map { _.error } mkString(". "))
}

case Path("/authorize", Params(params, request)) =>
case Path("/authorize") & Params(params) & request =>
val expected = for {
token <- lookup(TokenKey) is
nonempty(blankMsg(TokenKey)) is required(requiredMsg(TokenKey))
Expand All @@ -96,7 +96,7 @@ trait OAuthed extends OAuthProvider with unfiltered.filter.Plan {
BadRequest ~> ResponseString(fails.map { _.error } mkString(". "))
}

case POST(Path("/access_token", Authorization(OAuth.Header(headers), Params(params, request)))) =>
case request @ POST(Path("/access_token") & Authorization(OAuth.Header(headers)) & Params(params)) =>
val expected = for {
consumer_key <- lookup(ConsumerKey) is
nonempty(blankMsg(ConsumerKey)) is required(requiredMsg(ConsumerKey))
Expand Down

0 comments on commit 29912f1

Please sign in to comment.