Skip to content

Commit

Permalink
Updated HttpClientXLightWebSpec with combinator style
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Simpson committed Dec 1, 2010
1 parent 5eb3b36 commit 64df442
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 79 deletions.
1 change: 0 additions & 1 deletion .ensime
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
( (
:project-package "blueeyes" :project-package "blueeyes"
:use-sbt t :use-sbt t
:target "target/scala_2.8.0"
:sbt-compile-on-save nil :sbt-compile-on-save nil
:formatting-prefs (:alignParameters nil) :formatting-prefs (:alignParameters nil)
) )
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ sealed trait HttpClientXLightWebEngines[T] extends HttpClient[T]{


// Execute the HTTP request // Execute the HTTP request
xlRequest.foreach(httpClient.send(_, responseHandler)) xlRequest.foreach(httpClient.send(_, responseHandler))
httpClient.close
} }


def createXLRequest(request: HttpRequest[T], url: String): Option[XLHttpRequest] = { def createXLRequest(request: HttpRequest[T], url: String): Option[XLHttpRequest] = {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,82 +3,95 @@ package blueeyes.core.service.engines
import org.specs.Specification import org.specs.Specification
import org.specs.util._ import org.specs.util._
import blueeyes.core.http._ import blueeyes.core.http._
import blueeyes.core.http.HttpHeaders._
import blueeyes.core.http.HttpHeaderImplicits
import blueeyes.core.http.MimeTypes._
import blueeyes.core.service._ import blueeyes.core.service._
import blueeyes.util.Future import blueeyes.util.Future
import blueeyes.util.FutureImplicits import blueeyes.util.FutureImplicits


class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerCombinators { class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerCombinators with FutureImplicits {
import HttpHeaderImplicits._

val duration = 250 val duration = 250
val retries = 10 val retries = 10
val skip = true val skip = true


private val httpClient = new HttpClientXLightWebEnginesString {
}

def skipper(): () => Unit = skip match { def skipper(): () => Unit = skip match {
case true => skip("Will use Skalatra") case true => skip("Will use Skalatra")
case _ => () => Unit case _ => () => Unit
} }


"Support GET requests with status OK" in { private val httpClient = new HttpClientXLightWebEnginesString { }
val h = protocol("http") {
host("www.google.com") {
port(80) {
path[String, String]("/") {
get[String, String] { (res: HttpResponse[String]) =>
Future[String](res.content.getOrElse[String](""))
}
}
}
}
}


val f = h(httpClient)
f.value must eventually(beSomething)
}

/*
"Support GET requests with status OK" in { "Support GET requests with status OK" in {
skipper()() skipper()()
val f = protocol("http") {
println("RUNNING") host("localhost") {
val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php")) path[String, HttpResponse[String]]("/test/echo.php") {
f.deliverTo((res: HttpResponse[String]) => {}) get[String, HttpResponse[String]] { r => r }
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must eventually(be(HttpStatusCodes.OK)) f.value.get.status.code must eventually(be(HttpStatusCodes.OK))
} }

"Support GET requests with status Not Found" in { "Support GET requests with status Not Found" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/bogus")) val f = protocol("http") {
f.deliverTo((res: HttpResponse[String]) => {}) host("localhost") {
f.value must eventually(retries, new Duration(duration))(beSomething) path[String, HttpResponse[String]]("/test/bogus") {
f.value.get.status.code must be(HttpStatusCodes.NotFound) get[String, HttpResponse[String]]{ r => r}
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.NotFound)
} }


"Support GET requests with query params" in { "Support GET requests with query params" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php?param1=a&param2=b")) val f = protocol("http") {
f.deliverTo((res: HttpResponse[String]) => {}) host("localhost") {
path[String, HttpResponse[String]]("/test/echo.php?param1=a&param2=b") {
get[String, HttpResponse[String]] { (res: HttpResponse[String]) =>
res
}
}
}
}(httpClient)

f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b")) f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support POST requests with query params" in { "Support POST requests with query params" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php?param1=a&param2=b")) val f = protocol("http") {
f.deliverTo((res: HttpResponse[String]) => {}) host("localhost") {
path[String, HttpResponse[String]]("/test/echo.php?param1=a&param2=b") {
post[String, HttpResponse[String]]("") { r => r }
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b")) f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support POST requests with request params" in { "Support POST requests with request params" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", val f = protocol("http") {
parameters=Map('param1 -> "a", 'param2 -> "b"))) host("localhost") {
f.deliverTo((res: HttpResponse[String]) => {}) path[String, HttpResponse[String]]("/test/echo.php") {
parameters('param1 -> "a", 'param2 -> "b") {
post[String, HttpResponse[String]]("") { r => r }
}
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b")) f.value.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
Expand All @@ -87,8 +100,13 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with body" in { "Support POST requests with body" in {
skipper()() skipper()()
val content = "Hello, world" val content = "Hello, world"
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", content=Some(content))) val f = protocol("http") {
f.deliverTo((res: HttpResponse[String]) => {}) host("localhost") {
path[String, HttpResponse[String]]("/test/echo.php") {
post[String, HttpResponse[String]](content) { r => r }
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must eventually(equalIgnoreSpace(content)) f.value.get.content.get.trim must eventually(equalIgnoreSpace(content))
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
Expand All @@ -97,10 +115,15 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with body and request params" in { "Support POST requests with body and request params" in {
skipper()() skipper()()
val content = "Hello, world" val content = "Hello, world"
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", val f = protocol("http") {
content=Some(content), host("localhost") {
parameters=Map('param1 -> "a", 'param2 -> "b"))) path[String, HttpResponse[String]]("/test/echo.php") {
f.deliverTo((res: HttpResponse[String]) => {}) parameters('param1 -> "a", 'param2 -> "b") {
post[String, HttpResponse[String]](content) { r => r }
}
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must equalIgnoreSpace("param1=a&param2=b" + content) f.value.get.content.get.trim must equalIgnoreSpace("param1=a&param2=b" + content)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
Expand All @@ -109,19 +132,31 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support PUT requests with body" in { "Support PUT requests with body" in {
skipper()() skipper()()
val content = "Hello, world" val content = "Hello, world"
val f = apply(HttpRequest[String](HttpMethods.PUT, "http://localhost/test/echo.php", val f = protocol("http") {
content=Some(content), host("localhost") {
headers=Map(`Content-Length`(100)))) path[String, HttpResponse[String]]("/test/echo.php") {
headers(`Content-Length`(100)) {
put[String, HttpResponse[String]](content) { r => r }
}
}
}
}(httpClient)
f.deliverTo((res: HttpResponse[String]) => {}) f.deliverTo((res: HttpResponse[String]) => {})
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support GET requests with header" in { "Support GET requests with header" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php?headers=true", val f = protocol("http") {
headers=Map("Fooblahblah" -> "washere", "param2" -> "1"))) host("localhost") {
f.deliverTo((res: HttpResponse[String]) => {}) path[String, HttpResponse[String]]("/test/echo.php?headers=true") {
headers("Fooblahblah" -> "washere", "param2" -> "1") {
get[String, HttpResponse[String]] { r => r }
}
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must include("Fooblahblah: washere") f.value.get.content.get.trim must include("Fooblahblah: washere")
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
Expand All @@ -130,10 +165,15 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with Content-Type: text/html & Content-Length: 100" in { "Support POST requests with Content-Type: text/html & Content-Length: 100" in {
skipper()() skipper()()
val content = "<html></html>" val content = "<html></html>"
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", val f = protocol("http") {
content=Some(content), host("localhost") {
headers=Map(`Content-Type`(text/html), `Content-Length`(100)))) path[String, HttpResponse[String]]("/test/echo.php") {
f.deliverTo((res: HttpResponse[String]) => {}) headers(`Content-Type`(text/html), `Content-Length`(100)) {
post[String, HttpResponse[String]](content) { r => r }
}
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must beEqual(content) f.value.get.content.get.trim must beEqual(content)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
Expand All @@ -142,50 +182,51 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with large payload" in { "Support POST requests with large payload" in {
skipper()() skipper()()
val content = Array.fill(1024*1000)(0).toList.mkString("") val content = Array.fill(1024*1000)(0).toList.mkString("")
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", val f = protocol("http") {
content=Some(content))) host("localhost") {
f.deliverTo((res: HttpResponse[String]) => {}) path[String, HttpResponse[String]]("/test/echo.php") {
post[String, HttpResponse[String]](content) { r => r }
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.content.get.trim must beEqual(content) f.value.get.content.get.trim must beEqual(content)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support HEAD requests" in { "Support HEAD requests" in {
skipper()() skipper()()
val f = apply(HttpRequest[String](HttpMethods.HEAD, "http://localhost/test/echo.php")) val f = protocol("http") {
f.deliverTo((res: HttpResponse[String]) => {}) host("localhost") {
path[String, HttpResponse[String]]("/test/echo.php") {
head[String, HttpResponse[String]]() { r => r }
}
}
}(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support CONNECT requests" in { "Support CONNECT requests" in {
skip("CONNECT method TBD") skip("CONNECT method TBD")
val f = apply(HttpRequest[String](HttpMethods.CONNECT, "http://localhost/test/echo.php?headers=true",
headers=Map("Fooblahblah" -> "washere")))
f.deliverTo((res: HttpResponse[String]) => {})
f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK)
}
"Support POST requests with empty body" in {
skipper()()
val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php"))
f.deliverTo((res: HttpResponse[String]) => {})
f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK)
} }


"Support GET requests of 1000 requests" in { "Support GET requests of 1000 requests" in {
skipper()() skipper()()
val total = 1000 val total = 1000
val duration = 1000 val duration = 1000
val futures = (0 until total).map { i => val futures = (0 until total).map { i =>
apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php?test=true")) protocol("http") {
host("localhost") {
path[String, HttpResponse[String]]("/test/echo.php?test=true") {
get[String, HttpResponse[String]] { r => r }
}
}
}(httpClient)
} }


val responses = futures.foldLeft(0) { val responses = futures.foldLeft(0) {
(acc, f) => { (acc, f) => {
f.deliverTo((res: HttpResponse[String]) => {})
f.value must eventually(retries, new Duration(duration))(beSomething) f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK) f.value.get.status.code must be(HttpStatusCodes.OK)
acc + 1 acc + 1
Expand All @@ -194,7 +235,4 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb


responses must beEqual(total) responses must beEqual(total)
} }
*/


} }

0 comments on commit 64df442

Please sign in to comment.