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

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

This example Play Framework source code file (CookiesSpec.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, cookies, cookiesspec, mvc, none, play, play framework, some, specification

The CookiesSpec.scala Play Framework example source code

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

import org.specs2.mutable._

object CookiesSpec extends Specification {

  "object Cookies#apply" should {

    "create new Cookies instance with cookies" in {
      val originalCookie = Cookie(name = "cookie", value="value")

      val headerString = Cookies.encode(Seq(originalCookie))
      val c = Cookies(header = Some(headerString))

      c must beAnInstanceOf[Cookies]
    }

    "should create an empty Cookies instance with no header" in {
      val c = Cookies(header = None)
      c must beAnInstanceOf[Cookies]
    }
  }

  "trait Cookies#get" should {
    val originalCookie = Cookie(name = "cookie", value="value")
    val headerString = Cookies.encode(Seq(originalCookie))
    val c : Cookies = Cookies(header = Some(headerString))

    "get a cookie" in {
      c.get("cookie") must beSome[Cookie].which { cookie =>
        cookie.name must be_==("cookie")
      }
    }

    "return none if no cookie" in {
      c.get("no-cookie") must beNone
    }
  }

  "trait Cookies#apply" should {
    val originalCookie = Cookie(name = "cookie", value="value")
    val headerString = Cookies.encode(Seq(originalCookie))
    val c : Cookies = Cookies(header = Some(headerString))

    "apply for a cookie" in {
      val cookie = c("cookie")
      cookie.name must be_==("cookie")
    }

    "throw error if no cookie" in {
      {
        c("no-cookie")
      }.must(throwA[RuntimeException](message = "Cookie doesn't exist"))
    }
  }

  "trait Cookies#traversable" should {
    val cookie1 = Cookie(name = "cookie1", value="value2")
    val cookie2 = Cookie(name = "cookie2", value="value2")

    "be empty for no cookies" in {
      val c = Cookies(header = None)
      c must be empty
    }

    "contain elements for some cookies" in {
      val headerString = Cookies.encode(Seq(cookie1, cookie2))
      val c : Cookies = Cookies(header = Some(headerString))
      c must contain(allOf(cookie1,cookie2))
    }

    // technically the same as above
    "run a foreach for a cookie" in {
      val headerString = Cookies.encode(Seq(cookie1))
      val c : Cookies = Cookies(header = Some(headerString))

      var myCookie : Cookie = null
      c.foreach { cookie =>
        myCookie = cookie
      }
      myCookie must beEqualTo(cookie1)
    }
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework CookiesSpec.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.