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

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

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

anycontentasjson, anycontentasraw, api, array, get, head, lib, library, none, play, play framework, post, put, some, test, withapplication

The AnyContentBodyParserSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package play.it.http.parsing

import play.api.mvc._
import play.api.libs.iteratee.Enumerator
import play.api.libs.json.{Json, JsError}
import play.api.test._

object AnyContentBodyParserSpec extends PlaySpecification {

  "The anyContent body parser" should {

    def parse(method: String, contentType: Option[String], body: Array[Byte]) = {
      val request = FakeRequest(method, "/x").withHeaders(contentType.map(CONTENT_TYPE -> _).toSeq:_*)
      await(Enumerator(body) |>>> BodyParsers.parse.anyContent(request))
    }

    "ignore empty bodies for GET requests" in new WithApplication() {
      parse("GET", None, Array[Byte]()) must beRight(AnyContentAsEmpty)
    }

    "ignore text bodies for GET requests" in new WithApplication() {
      parse("GET", Some("text/plain"), "bar".getBytes("utf-8")) must beRight(AnyContentAsEmpty)
    }

    "ignore empty bodies for HEAD requests" in new WithApplication() {
      parse("HEAD", None, Array[Byte]()) must beRight(AnyContentAsEmpty)
    }

    "ignore text bodies for HEAD requests" in new WithApplication() {
      parse("HEAD", None, "bar".getBytes("utf-8")) must beRight(AnyContentAsEmpty)
    }

    "parse text bodies for POST requests" in new WithApplication() {
      parse("POST", Some("text/plain"), "bar".getBytes("utf-8")) must beRight(AnyContentAsText("bar"))
    }

    "parse JSON bodies for PUT requests" in new WithApplication() {
      parse("PUT", Some("application/json"), """{"foo":"bar"}""".getBytes("utf-8")) must beRight.like {
        case AnyContentAsJson(json) => (json \ "foo").as[String] must_== "bar"
      }
    }

    "parse unknown bodies as raw for PUT requests" in new WithApplication() {
      val inBytes = Array[Byte](0)
      parse("PUT", None, inBytes) must beRight.like {
        case AnyContentAsRaw(rawBuffer) => rawBuffer.asBytes() must beSome.like {
          case outBytes => outBytes.to[Vector] must_== inBytes.to[Vector]
        }
      }
    }

  }
}

Other Play Framework source code examples

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