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

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

This example Play Framework source code file (HttpSpec.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

a, api, cookie, foo, headers, http, httponly, httpspec, mvc, path, play, play framework, seq, some

The HttpSpec.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 HttpSpec extends Specification {
  "HTTP" title

  val headers = new Headers {
    val data = Seq(("a", Seq("a1", "a2")), ("b", Seq("b1", "b2")))
  }

  "Headers" should {
    "return the header value associated with a" in {
      headers.get("a") must beSome("a1")
    }

    "return the header values associated with b" in {
      headers.getAll("b") must be_==(Seq("b1", "b2"))
    }

    "not return an empty sequence of values associated with an unknown key" in {
      headers.getAll("z") must beEmpty
    }

    "should return all keys" in {
      headers.keys must be_==(Set("a", "b"))
    }

    "should return a simple map" in {
      headers.toSimpleMap must be_==(Map("a" -> "a1", "b" -> "b1"))
    }

    "return the value from a map by case insensitive" in {
      (headers.toMap.get("A") must_== Some(Seq("a1", "a2"))) and 
        (headers.toMap.get("b") must_== Some(Seq("b1", "b2")))
    }

    "return the value from a simple map by case insensitive" in {
      (headers.toSimpleMap.get("A") must beSome("a1")) and
       (headers.toSimpleMap.get("b") must beSome("b1"))
    }
  }

  "Cookies" should {
    "merge two cookies" in {
      val cookies = Seq(
        Cookie("foo", "bar"),
        Cookie("bar", "qux"))

      Cookies.merge("", cookies) must ===("foo=bar; Path=/; HTTPOnly; bar=qux; Path=/; HTTPOnly")
    }
    "merge and remove duplicates" in {
      val cookies = Seq(
        Cookie("foo", "bar"),
        Cookie("foo", "baz"),
        Cookie("foo", "bar", domain=Some("Foo")),
        Cookie("foo", "baz", domain=Some("FoO")),
        Cookie("foo", "baz", secure=true),
        Cookie("foo", "baz", httpOnly=false),
        Cookie("foo", "bar", path="/blah"),
        Cookie("foo", "baz", path="/blah"))


      Cookies.merge("", cookies) must ===(
        "foo=baz; Path=/; Domain=FoO; HTTPOnly" + "; " + // Cookie("foo", "baz", domain=Some("FoO"))
        "foo=baz; Path=/"                       + "; " + // Cookie("foo", "baz", httpOnly=false)
        "foo=baz; Path=/blah; HTTPOnly"                  // Cookie("foo", "baz", path="/blah")
        )
    }
  }
}

Other Play Framework source code examples

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