Permalink
Browse files

Updated HttpClientXLightWebSpec with combinator style

  • Loading branch information...
1 parent 5eb3b36 commit 64df442302a53e31758078926738a831bf1ad184 Jeff Simpson committed Dec 1, 2010
View
@@ -3,7 +3,6 @@
(
:project-package "blueeyes"
:use-sbt t
-:target "target/scala_2.8.0"
:sbt-compile-on-save nil
:formatting-prefs (:alignParameters nil)
)
@@ -64,6 +64,7 @@ sealed trait HttpClientXLightWebEngines[T] extends HttpClient[T]{
// Execute the HTTP request
xlRequest.foreach(httpClient.send(_, responseHandler))
+ httpClient.close
}
def createXLRequest(request: HttpRequest[T], url: String): Option[XLHttpRequest] = {
@@ -3,82 +3,95 @@ package blueeyes.core.service.engines
import org.specs.Specification
import org.specs.util._
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.util.Future
import blueeyes.util.FutureImplicits
-class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerCombinators {
+class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerCombinators with FutureImplicits {
+ import HttpHeaderImplicits._
+
val duration = 250
val retries = 10
val skip = true
- private val httpClient = new HttpClientXLightWebEnginesString {
- }
-
def skipper(): () => Unit = skip match {
case true => skip("Will use Skalatra")
case _ => () => Unit
}
- "Support GET requests with status OK" in {
- 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](""))
- }
- }
- }
- }
- }
+ private val httpClient = new HttpClientXLightWebEnginesString { }
- val f = h(httpClient)
- f.value must eventually(beSomething)
- }
-
-/*
"Support GET requests with status OK" in {
skipper()()
-
- println("RUNNING")
- val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php"))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ host("localhost") {
+ path[String, HttpResponse[String]]("/test/echo.php") {
+ get[String, HttpResponse[String]] { r => r }
+ }
+ }
+ }(httpClient)
f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must eventually(be(HttpStatusCodes.OK))
}
-
- "Support GET requests with status Not Found" in {
- skipper()()
- val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/bogus"))
- f.deliverTo((res: HttpResponse[String]) => {})
- f.value must eventually(retries, new Duration(duration))(beSomething)
- f.value.get.status.code must be(HttpStatusCodes.NotFound)
+
+ "Support GET requests with status Not Found" in {
+ skipper()()
+ val f = protocol("http") {
+ host("localhost") {
+ path[String, HttpResponse[String]]("/test/bogus") {
+ 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 {
skipper()()
- val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php?param1=a&param2=b"))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ 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.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK)
}
"Support POST requests with query params" in {
skipper()()
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php?param1=a&param2=b"))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ 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.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK)
}
"Support POST requests with request params" in {
skipper()()
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php",
- parameters=Map('param1 -> "a", 'param2 -> "b")))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ host("localhost") {
+ 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.get.content.get.trim must eventually(equalIgnoreSpace("param1=a&param2=b"))
f.value.get.status.code must be(HttpStatusCodes.OK)
@@ -87,8 +100,13 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with body" in {
skipper()()
val content = "Hello, world"
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php", content=Some(content)))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ 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.get.content.get.trim must eventually(equalIgnoreSpace(content))
f.value.get.status.code must be(HttpStatusCodes.OK)
@@ -97,10 +115,15 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with body and request params" in {
skipper()()
val content = "Hello, world"
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php",
- content=Some(content),
- parameters=Map('param1 -> "a", 'param2 -> "b")))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ host("localhost") {
+ path[String, HttpResponse[String]]("/test/echo.php") {
+ parameters('param1 -> "a", 'param2 -> "b") {
+ post[String, HttpResponse[String]](content) { r => r }
+ }
+ }
+ }
+ }(httpClient)
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.status.code must be(HttpStatusCodes.OK)
@@ -109,19 +132,31 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support PUT requests with body" in {
skipper()()
val content = "Hello, world"
- val f = apply(HttpRequest[String](HttpMethods.PUT, "http://localhost/test/echo.php",
- content=Some(content),
- headers=Map(`Content-Length`(100))))
+ val f = protocol("http") {
+ host("localhost") {
+ 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.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK)
}
"Support GET requests with header" in {
skipper()()
- val f = apply(HttpRequest[String](HttpMethods.GET, "http://localhost/test/echo.php?headers=true",
- headers=Map("Fooblahblah" -> "washere", "param2" -> "1")))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ host("localhost") {
+ 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.get.content.get.trim must include("Fooblahblah: washere")
f.value.get.status.code must be(HttpStatusCodes.OK)
@@ -130,10 +165,15 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with Content-Type: text/html & Content-Length: 100" in {
skipper()()
val content = "<html></html>"
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php",
- content=Some(content),
- headers=Map(`Content-Type`(text/html), `Content-Length`(100))))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ host("localhost") {
+ path[String, HttpResponse[String]]("/test/echo.php") {
+ 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.get.content.get.trim must beEqual(content)
f.value.get.status.code must be(HttpStatusCodes.OK)
@@ -142,50 +182,51 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
"Support POST requests with large payload" in {
skipper()()
val content = Array.fill(1024*1000)(0).toList.mkString("")
- val f = apply(HttpRequest[String](HttpMethods.POST, "http://localhost/test/echo.php",
- content=Some(content)))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ 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.get.content.get.trim must beEqual(content)
f.value.get.status.code must be(HttpStatusCodes.OK)
}
"Support HEAD requests" in {
skipper()()
- val f = apply(HttpRequest[String](HttpMethods.HEAD, "http://localhost/test/echo.php"))
- f.deliverTo((res: HttpResponse[String]) => {})
+ val f = protocol("http") {
+ 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.get.status.code must be(HttpStatusCodes.OK)
}
"Support CONNECT requests" in {
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 {
skipper()()
val total = 1000
val duration = 1000
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) {
(acc, f) => {
- f.deliverTo((res: HttpResponse[String]) => {})
f.value must eventually(retries, new Duration(duration))(beSomething)
f.value.get.status.code must be(HttpStatusCodes.OK)
acc + 1
@@ -194,7 +235,4 @@ class HttpClientXLightWebSpec extends Specification with HttpResponseHandlerComb
responses must beEqual(total)
}
-*/
-
-
}

0 comments on commit 64df442

Please sign in to comment.