Created
April 3, 2015 07:37
-
-
Save msbaek/c35ffa73432042b88c5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import models.{DB, SdssRecord} | |
import org.joda.time.LocalDate | |
import org.specs2.mutable.Specification | |
import play.api.libs.ws.{WS, WSResponse} | |
import play.api.test.WithApplication | |
import spray.http.{HttpRequest, HttpResponse} | |
import akka.actor.ActorSystem | |
import spray.http.{HttpRequest, HttpResponse} | |
import spray.client.pipelining.{Get, sendReceive} | |
import scala.concurrent.Future | |
import scala.util.{Success, Failure} | |
import scala.concurrent.Future | |
import scala.util.matching.Regex.Match | |
class SprayClientSpec extends Specification { | |
var date = new LocalDate(2015, 3, 27) | |
trait WebClient { | |
def get(url: String): Future[String] | |
} | |
class SprayWebClient(implicit system: ActorSystem) extends WebClient { | |
import system.dispatcher | |
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive | |
def get(url: String): Future[String] = { | |
val futureResponse = pipeline(Get(url)) | |
futureResponse.map { | |
_.entity.asString | |
} | |
} | |
} | |
"record should be persistent" in new WithApplication { | |
implicit val system = ActorSystem() | |
val webClient = new SprayWebClient() | |
val url = "http://xxx.yy.zz" | |
val futureResponse = webClient.get(url) | |
futureResponse onComplete { | |
case Success(response) => println(s"\n\nresponse=[$response]\n\n") | |
case Failure(error) => println(s"\n\nAn error has occurred: [${error.getMessage}]\n\n") | |
} | |
while (!futureResponse.isCompleted) { | |
println("sleep 100ms") | |
Thread.sleep(1 * 100) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment