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

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

This example Play Framework source code file (BadClientHandlingSpec.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, badclienthandlingspec, essentialaction, exception, fakeapplication, http, io, mvc, play, play framework, playspecification, port, server, t, test, utilities

The BadClientHandlingSpec.scala Play Framework example source code

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

import play.api.mvc._
import play.api.test._
import play.api.test.TestServer
import scala.util.Random
import scala.io.Source
import java.io.InputStream

object BadClientHandlingSpec extends PlaySpecification {

  "Play" should {

    def withServer[T](action: EssentialAction)(block: Port => T) = {
      val port = testServerPort
      running(TestServer(port, FakeApplication(
        withRoutes = {
          case _ => action
        }
      ))) {
        block(port)
      }
    }

    "gracefully handle long urls and return 414" in withServer(Action(Results.Ok)) { port =>
      val url = new String(Random.alphanumeric.take(5 * 1024).toArray)

      val conn = new java.net.URL("http://localhost:" + port + "/" + url).openConnection()
      try {
        Source.fromInputStream(conn.getContent.asInstanceOf[InputStream]).getLines()
        failure // must not reach this point
      } catch {
        case e: Exception =>
          e.getMessage must contain("Server returned HTTP response code: 414")
          success
      }
    }

  }
}

Other Play Framework source code examples

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