diff options
author | adamw <adam@warski.org> | 2017-07-16 09:59:09 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-16 09:59:09 +0200 |
commit | b1844f09d78e035d85302310e2ba55929cd5fc52 (patch) | |
tree | f1659e57ff863dd81f6935e422d9f9043fc66f40 /tests/src/test/scala | |
parent | 062ab1ac1bd4fa9f4602ae31419c60a7e6d991bf (diff) | |
download | sttp-b1844f09d78e035d85302310e2ba55929cd5fc52.tar.gz sttp-b1844f09d78e035d85302310e2ba55929cd5fc52.tar.bz2 sttp-b1844f09d78e035d85302310e2ba55929cd5fc52.zip |
Streaming tests, comments
Diffstat (limited to 'tests/src/test/scala')
3 files changed, 123 insertions, 34 deletions
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index def843a..ff6b113 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -4,13 +4,11 @@ import java.io.ByteArrayInputStream import java.nio.ByteBuffer import java.time.{ZoneId, ZonedDateTime} -import akka.stream.ActorMaterializer -import akka.actor.ActorSystem -import akka.http.scaladsl.Http import akka.http.scaladsl.model.{DateTime, FormData} import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.headers.CacheDirectives._ import akka.http.scaladsl.server.Directives._ +import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.Credentials import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler import com.typesafe.scalalogging.StrictLogging @@ -18,7 +16,6 @@ import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import better.files._ -import scala.concurrent.Future import scala.language.higherKinds class BasicTests @@ -27,11 +24,14 @@ class BasicTests with BeforeAndAfterAll with ScalaFutures with StrictLogging - with IntegrationPatience { + with IntegrationPatience + with TestHttpServer + with ForceWrapped { + private def paramsToString(m: Map[String, String]): String = m.toList.sortBy(_._1).map(p => s"${p._1}=${p._2}").mkString(" ") - private val serverRoutes = + override val serverRoutes: Route = pathPrefix("echo") { pathPrefix("form_params") { formFieldMap { params => @@ -105,37 +105,12 @@ class BasicTests } } - private implicit val actorSystem: ActorSystem = ActorSystem("sttp-test") - import actorSystem.dispatcher - - private implicit val materializer = ActorMaterializer() - private val endpoint = "http://localhost:51823" - - override protected def beforeAll(): Unit = { - Http().bindAndHandle(serverRoutes, "localhost", 51823).futureValue - } - - override protected def afterAll(): Unit = { - actorSystem.terminate().futureValue - } - - trait ForceWrappedValue[R[_]] { - def force[T](wrapped: R[T]): T - } - implicit class ForceDecorator[R[_], T](wrapped: R[T]) { - def force()(implicit fwv: ForceWrappedValue[R]): T = fwv.force(wrapped) - } + override def port = 51823 runTests("HttpURLConnection")(HttpConnectionSttpHandler, - new ForceWrappedValue[Id] { - override def force[T](wrapped: Id[T]): T = - wrapped - }) + ForceWrappedValue.id) runTests("Akka HTTP")(new AkkaHttpSttpHandler(actorSystem), - new ForceWrappedValue[Future] { - override def force[T](wrapped: Future[T]): T = - wrapped.futureValue - }) + ForceWrappedValue.future) def runTests[R[_]](name: String)( implicit handler: SttpHandler[R, Nothing], diff --git a/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala new file mode 100644 index 0000000..ab77753 --- /dev/null +++ b/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala @@ -0,0 +1,64 @@ +package com.softwaremill.sttp + +import akka.http.scaladsl.server.Directives._ +import akka.http.scaladsl.server.Route +import akka.stream.scaladsl.Source +import akka.util.ByteString +import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler +import com.typesafe.scalalogging.StrictLogging +import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} +import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} +import com.softwaremill.sttp.akkahttp._ + +class StreamingTests + extends FlatSpec + with Matchers + with BeforeAndAfterAll + with ScalaFutures + with StrictLogging + with IntegrationPatience + with TestHttpServer { + + override val serverRoutes: Route = + path("echo") { + post { + parameterMap { params => + entity(as[String]) { body: String => + complete(body) + } + } + } + } + + override def port = 51824 + + akkaStreamingTests() + + def akkaStreamingTests(): Unit = { + implicit val handler = new AkkaHttpSttpHandler(actorSystem) + + val body = "streaming test" + + "Akka HTTP" should "stream request body" in { + val response = sttp + .post(uri"$endpoint/echo") + .body(Source.single(ByteString(body))) + .send(responseAsString) + .futureValue + + response.body should be(body) + } + + it should "receive a stream" in { + val response = sttp + .post(uri"$endpoint/echo") + .body(body) + .send(responseAsStream[Source[ByteString, Any]]) + .futureValue + + val responseBody = response.body.runReduce(_ ++ _).futureValue.utf8String + + responseBody should be(body) + } + } +} diff --git a/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala new file mode 100644 index 0000000..bc7eccd --- /dev/null +++ b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala @@ -0,0 +1,50 @@ +package com.softwaremill.sttp + +import akka.actor.ActorSystem +import akka.http.scaladsl.Http +import akka.http.scaladsl.server.Route +import akka.stream.ActorMaterializer +import org.scalatest.{BeforeAndAfterAll, Suite} +import org.scalatest.concurrent.ScalaFutures + +import scala.concurrent.Future +import scala.language.higherKinds + +trait TestHttpServer extends BeforeAndAfterAll with ScalaFutures { + this: Suite => + protected implicit val actorSystem: ActorSystem = ActorSystem("sttp-test") + import actorSystem.dispatcher + + protected implicit val materializer = ActorMaterializer() + protected val endpoint = uri"http://localhost:$port" + + override protected def beforeAll(): Unit = { + Http().bindAndHandle(serverRoutes, "localhost", port).futureValue + } + + override protected def afterAll(): Unit = { + actorSystem.terminate().futureValue + } + + def serverRoutes: Route + def port: Int +} + +trait ForceWrapped extends ScalaFutures { this: Suite => + trait ForceWrappedValue[R[_]] { + def force[T](wrapped: R[T]): T + } + object ForceWrappedValue { + val id = new ForceWrappedValue[Id] { + override def force[T](wrapped: Id[T]): T = + wrapped + } + val future = new ForceWrappedValue[Future] { + override def force[T](wrapped: Future[T]): T = + wrapped.futureValue + } + } + implicit class ForceDecorator[R[_], T](wrapped: R[T]) { + def force()(implicit fwv: ForceWrappedValue[R]): T = fwv.force(wrapped) + } +} |