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

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

This example Play Framework source code file (JsonBodyParserSpec.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, foo, int, json, jsonbodyparserspec, lib, library, none, option, play, play framework, playspecification, some, string, test, withapplication

The JsonBodyParserSpec.scala Play Framework example source code

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

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

object JsonBodyParserSpec extends PlaySpecification {

  private case class Foo(a: Int, b: String)
  private implicit val fooFormat = Json.format[Foo]

  "The JSON body parser" should {

    def parse[A](json: String, contentType: Option[String], encoding: String, bodyParser: BodyParser[A] = BodyParsers.parse.tolerantJson) = {
      await(Enumerator(json.getBytes(encoding)) |>>>
        bodyParser(FakeRequest().withHeaders(contentType.map(CONTENT_TYPE -> _).toSeq:_*)))
    }

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

    "automatically detect the charset" in new WithApplication() {
      parse("""{"foo":"bär"}""", Some("application/json"), "utf-8") must beRight.like {
        case json => (json \ "foo").as[String] must_== "bär"
      }
      parse("""{"foo":"bär"}""", Some("application/json"), "utf-16") must beRight.like {
        case json => (json \ "foo").as[String] must_== "bär"
      }
      parse("""{"foo":"bär"}""", Some("application/json"), "utf-32") must beRight.like {
        case json => (json \ "foo").as[String] must_== "bär"
      }
    }

    "ignore the supplied charset" in new WithApplication() {
      parse("""{"foo":"bär"}""", Some("application/json; charset=iso-8859-1"), "utf-16") must beRight.like {
        case json => (json \ "foo").as[String] must_== "bär"
      }
    }

    "accept all common json content types" in new WithApplication() {
      parse("""{"foo":"bar"}""", Some("application/json"), "utf-8", BodyParsers.parse.json) must beRight.like {
        case json => (json \ "foo").as[String] must_== "bar"
      }
      parse("""{"foo":"bar"}""", Some("text/json"), "utf-8", BodyParsers.parse.json) must beRight.like {
        case json => (json \ "foo").as[String] must_== "bar"
      }
    }

    "reject non json content types" in new WithApplication() {
      parse("""{"foo":"bar"}""", Some("application/xml"), "utf-8", BodyParsers.parse.json) must beLeft
      parse("""{"foo":"bar"}""", None, "utf-8", BodyParsers.parse.json) must beLeft
    }

    "gracefully handle invalid json" in new WithApplication() {
      parse("""{"foo:}""", Some("application/json"), "utf-8", BodyParsers.parse.json) must beLeft
    }

    "validate json content using .validate" in new WithApplication() {
      import scala.concurrent.ExecutionContext.Implicits.global

      val fooParser = BodyParsers.parse.json.validate {
        _.validate[Foo].asEither.left.map(e => BadRequest(JsError.toFlatJson(e)))
      }
      parse("""{"a":1,"b":"bar"}""", Some("application/json"), "utf-8", fooParser) must beRight
      parse("""{"foo":"bar"}""", Some("application/json"), "utf-8", fooParser) must beLeft
      parse("""{"a":1}""", Some("application/json"), "utf-8", fooParser) must beLeft
    }

    "validate json content using implicit reads" in new WithApplication() {
      parse("""{"a":1,"b":"bar"}""", Some("application/json"), "utf-8", BodyParsers.parse.json[Foo]) must beRight.like {
        case foo => foo must_== Foo(1, "bar")
      }
      parse("""{"foo":"bar"}""", Some("application/json"), "utf-8", BodyParsers.parse.json[Foo]) must beLeft
      parse("""{"a":1}""", Some("application/json"), "utf-8", BodyParsers.parse.json[Foo]) must beLeft
      parse("""{"foo:}""", Some("application/json"), "utf-8", BodyParsers.parse.json[Foo]) must beLeft
    }

  }

}

Other Play Framework source code examples

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