Permalink
Browse files

fixing encoding, now ResultsSpec passes again

  • Loading branch information...
1 parent 7ec1381 commit 2c0a96b2aa7b30220a05796d8ad32fb123a1b66b @pk11 pk11 committed Oct 29, 2011
@@ -16,9 +16,9 @@ object Crypto {
}
def sign(message: String): String = {
- Play.maybeApplication.get.configuration.get("application.secret") match {
+ Play.maybeApplication.map(_.configuration.get("application.secret") match {
case Some(Config(key, value, file)) => sign(message, value.getBytes)
case _ => throw PlayException("Configuration error", "Missing application.secret")
- }
+ }).getOrElse(throw PlayException("Configuration error", "Application does not exist"))
}
-}
+}
@@ -165,23 +165,28 @@ package play.api.mvc {
* Encode the data as String.
*/
def encode(data: Map[String, String]): String = {
- java.net.URLEncoder.encode(data.filterNot(_._1.contains(":")).map(d => d._1 + ":" + d._2).mkString("\u0000"))
+ val encoded = java.net.URLEncoder.encode(data.filterNot(_._1.contains(":")).map(d => d._1 + ":" + d._2).mkString("\u0000"))
+ if (isSigned)
+ Crypto.sign(encoded) + "-" + encoded
+ else
+ encoded
}
/**
- * Decode a from an encoded String.
+ * Decode from an encoded String.
*/
def decode(data: String): Map[String, String] = {
+
+ def urldecode(data: String) = java.net.URLDecoder.decode(data).split("\u0000").map(_.split(":")).map(p => p(0) -> p.drop(1).mkString(":")).toMap
+
try {
- Option(data.trim).filterNot(_.isEmpty).flatMap(data =>
- if (isSigned) {
- val splitted = data.split("-")
- if (splitted(0) == Crypto.sign(splitted(1)))
- Some(splitted(1))
- else None
- } else Some(data)).map { data =>
- java.net.URLDecoder.decode(data).split("\u0000").map(_.split(":")).map(p => p(0) -> p.drop(1).mkString(":")).toMap
- }.getOrElse(Map.empty[String, String])
+ if (isSigned) {
+ val splitted = data.split("-")
+ if (splitted(0) == Crypto.sign(splitted(1)))
+ return urldecode(splitted(1))
+ else
+ return Map.empty[String, String]
+ } else urldecode(data)
} catch {
// fail gracefully is the session cookie is corrupted
case _ => Map.empty[String, String]
@@ -193,9 +198,7 @@ package play.api.mvc {
*/
def encodeAsCookie(data: T): Cookie = {
val cookie = encode(serialize(data))
- Cookie(COOKIE_NAME, if (isSigned) {
- Crypto.sign(cookie) + "-" + cookie
- } else cookie)
+ Cookie(COOKIE_NAME, cookie)
}
/**
@@ -0,0 +1,17 @@
+package play.api.cache
+
+import org.specs2.mutable._
+
+object CacheSpec extends Specification {
+
+ "A cache" should {
+
+ "get/set items the scala way" in {
+ val c = new Cache
+ c.set("peter", "soooo")
+ c.get[String]("peter").get must equalTo("soooo")
+ }
+
+ }
+
+}
@@ -61,17 +61,26 @@ object ResultsSpec extends Specification {
"support session helper" in {
Session.decode(" ").isEmpty must be_==(true)
+ import java.io.File
+ import play.api._
+ import play.core._
+ val cl = new ApplicationClassLoader(Thread.currentThread().getContextClassLoader())
+
+ implicit val app: Application = new Application(new File("."), cl, None, Play.Mode.Dev) {
+ override lazy val configuration = new Configuration(Map("application.secret" -> Configuration.Config("application.secret", "pass", new File("."))), "")
+ }
+ Play.start(app)
val data = Map("user" -> "kiki", "bad:key" -> "yop", "langs" -> "fr:en:de")
- val encodedSession = Session.encode(Session(data))
+ val encodedSession = Session.encode(data)
val decodedSession = Session.decode(encodedSession)
- decodedSession.data.size must be_==(2)
- decodedSession.data must havePair("user" -> "kiki")
- decodedSession.data must havePair("langs" -> "fr:en:de")
-
+ decodedSession.size must be_==(2)
+ decodedSession must havePair("user" -> "kiki")
+ decodedSession must havePair("langs" -> "fr:en:de")
val SimpleResult(ResponseHeader(_, headers), _) =
Ok("hello").as("text/html")
+ .withSession("user" -> "kiki", "langs" -> "fr:en:de")
.withCookies(Cookie("session", "items"), Cookie("preferences", "blue"))
.discardingCookies("logged")
.withSession("user" -> "kiki", "langs" -> "fr:en:de")
@@ -83,8 +92,7 @@ object ResultsSpec extends Specification {
setCookies("preferences").value must be_==("blue")
setCookies("lang").value must be_==("fr")
setCookies("logged").maxAge must be_==(0)
-
- val playSession = Session.decodeFromCookie(setCookies.get(Session.SESSION_COOKIE_NAME))
+ val playSession = Session.decodeFromCookie(setCookies.get(Session.COOKIE_NAME))
playSession.data.size must be_==(2)
playSession.data must havePair("user" -> "kiki")
playSession.data must havePair("langs" -> "fr:en:de")
@@ -0,0 +1,31 @@
+<configuration>
+
+ <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
+
+ <appender name="FILE" class="ch.qos.logback.core.FileAppender">
+ <file>${application.home}/logs/application.log</file>
+ <encoder>
+ <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
+ </encoder>
+ </appender>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="play" level="INFO" />
+ <logger name="application" level="INFO" />
+
+ <!-- Off these ones as they are annoying, and anyway we manage configuration ourself -->
+ <logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
+ <logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
+ <logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />
+
+ <root level="ERROR">
+ <appender-ref ref="STDOUT" />
+ <appender-ref ref="FILE" />
+ </root>
+
+</configuration>
@@ -0,0 +1,7 @@
+# Default messages
+
+# --- Constraints
+constraint.required=Required field
+
+# --- Formats
+format.date=Date (''{0}'')

0 comments on commit 2c0a96b

Please sign in to comment.