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

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

This example Play Framework source code file (HttpPipeliningSpec.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, basicrequest, concurrent, done, fakeapplication, get, lib, library, map, play, play framework, play's, playspecification, port, string, t, test

The HttpPipeliningSpec.scala Play Framework example source code

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

import play.api.mvc.{Results, EssentialAction}
import play.api.test._
import play.api.test.TestServer
import play.api.libs.concurrent.Promise
import play.api.libs.iteratee._
import java.util.concurrent.TimeUnit
import scala.concurrent.Future

import scala.concurrent.ExecutionContext.Implicits.global

object HttpPipeliningSpec extends PlaySpecification {

  "Play's http pipelining support" should {

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

    "wait for the first response to return before returning the second" in withServer(EssentialAction { req =>
      req.path match {
        case "/long" => Iteratee.flatten(Promise.timeout(Done(Results.Ok("long")), 100, TimeUnit.MILLISECONDS))
        case "/short" => Done(Results.Ok("short"))
        case _ => Done(Results.NotFound)
      }
    }) { port =>
      val responses = BasicHttpClient.pipelineRequests(port,
        BasicRequest("GET", "/long", "HTTP/1.1", Map(), ""),
        BasicRequest("GET", "/short", "HTTP/1.1", Map(), "")
      )
      responses(0).status must_== 200
      responses(0).body must beLeft("long")
      responses(1).status must_== 200
      responses(1).body must beLeft("short")
    }

    "wait for the first response body to return before returning the second" in withServer(EssentialAction { req =>
      req.path match {
        case "/long" => Done(
          Results.Ok.chunked(Enumerator.unfoldM[Int, String](0) { chunk =>
            if (chunk < 3) {
              Promise.timeout(Some((chunk + 1, chunk.toString)), 50, TimeUnit.MILLISECONDS)
            } else {
              Future.successful(None)
            }
          })
        )
        case "/short" => Done(Results.Ok("short"))
        case _ => Done(Results.NotFound)
      }
    }) { port =>
      val responses = BasicHttpClient.pipelineRequests(port,
        BasicRequest("GET", "/long", "HTTP/1.1", Map(), ""),
        BasicRequest("GET", "/short", "HTTP/1.1", Map(), "")
      )
      responses(0).status must_== 200
      responses(0).body must beRight
      responses(0).body.right.get._1 must containAllOf(Seq("0", "1", "2")).inOrder
      responses(1).status must_== 200
      responses(1).body must beLeft("short")
    }

  }
}

Other Play Framework source code examples

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