alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (FlashCookieSpec.scala)

This example Play Framework source code file (FlashCookieSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, cookie, flash, flashcookiespec, illegalargumentexception, map, mvc, play, play framework, specification, string, unicode, utf-8

The FlashCookieSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.mvc

import org.specs2.mutable._
import java.net.URLEncoder

object FlashCookieSpec extends Specification {

  def oldEncoder(data: Map[String, String]): String = {
    URLEncoder.encode(
      data.map(d => d._1 + ":" + d._2).mkString("\u0000"),
      "UTF-8"
    )
  }

  "Flash cookies" should {
    "bake in a header and value" in {
      val es = Flash.encode(Map("a" -> "b"))
      val m = Flash.decode(es)
      m.size must_== 1
      m("a") must_== "b"
    }
    "bake in multiple headers and values" in {
      val es = Flash.encode(Map("a" -> "b", "c" -> "d"))
      val m = Flash.decode(es)
      m.size must_== 2
      m("a") must_== "b"
      m("c") must_== "d"
    }
    "bake in a header an empty value" in {
      val es = Flash.encode(Map("a" -> ""))
      val m = Flash.decode(es)
      m.size must_== 1
      m("a") must_== ""
    }
    "bake in a header a Unicode value" in {
      val es = Flash.encode(Map("a" -> "\u0000"))
      val m = Flash.decode(es)
      m.size must_== 1
      m("a") must_== "\u0000"
    }
    "bake in an empty map" in {
      val es = Flash.encode(Map.empty)
      val m = Flash.decode(es)
      m.size must_== 0
    }
    "encode values such that no extra keys can be created" in {
      val es = Flash.encode(Map("a" -> "b&c=d"))
      val m = Flash.decode(es)
      m.size must_== 1
      m("a") must_== "b&c=d"
    }
    "specifically exclude control chars" in {
      for (i <- 0 until 32) {
        val s = Character.toChars(i).toString
        val es = Flash.encode(Map("a" -> s))
        es must not contain s

        val m = Flash.decode(es)
        m.size must_== 1
        m("a") must_== s
      }
      success
    }
    "specifically exclude special cookie chars" in {
      val es = Flash.encode(Map("a" -> " \",;\\"))
      es must not contain " "
      es must not contain "\""
      es must not contain ","
      es must not contain ";"
      es must not contain "\\"

      val m = Flash.decode(es)
      m.size must_== 1
      m("a") must_== " \",;\\"
    }
    "decode values of the previously supported format" in {
      val es = oldEncoder(Map("a" -> "b", "c" -> "d"))
      val m = Flash.decode(es)
      m.size must_== 0
    }
    "decode values of the previously supported format with the new delimiters in them" in {
      val es = oldEncoder(Map("a" -> "b&="))
      val m = Flash.decode(es)
      m.size must_== 0
    }
    "decode values with gibberish in them" in {
      val es = "asfjdlkasjdflk"
      val m = Flash.decode(es)
      m.size must_== 0
    }
    "put disallows null values" in {
      val c = Flash(Map("foo" -> "bar"))
      c + (("x", null)) must throwA(new IllegalArgumentException("requirement failed: Cookie values cannot be null"))
    }
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework FlashCookieSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.