diff options
Diffstat (limited to 'tests')
10 files changed, 52 insertions, 113 deletions
diff --git a/tests/src/test/resources/logback.xml b/tests/src/test/resources/logback.xml new file mode 100644 index 0000000..1470b51 --- /dev/null +++ b/tests/src/test/resources/logback.xml @@ -0,0 +1,12 @@ +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT"/> + </root> +</configuration> diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 392f6d8..55e21b8 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -19,8 +19,12 @@ import com.softwaremill.sttp.asynchttpclient.cats.AsyncHttpClientCatsBackend import com.softwaremill.sttp.asynchttpclient.future.AsyncHttpClientFutureBackend import com.softwaremill.sttp.asynchttpclient.monix.AsyncHttpClientMonixBackend import com.softwaremill.sttp.asynchttpclient.scalaz.AsyncHttpClientScalazBackend +import com.softwaremill.sttp.impl.cats.convertCatsIOToFuture +import com.softwaremill.sttp.impl.monix.convertMonixTaskToFuture +import com.softwaremill.sttp.impl.scalaz.convertScalazTaskToFuture import com.softwaremill.sttp.okhttp.monix.OkHttpMonixBackend import com.softwaremill.sttp.okhttp.{OkHttpFutureBackend, OkHttpSyncBackend} +import com.softwaremill.sttp.testing.streaming.ConvertToFuture import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} import org.scalatest.{path => _, _} @@ -180,19 +184,20 @@ class BasicTests var closeBackends: List[() => Unit] = Nil - runTests("HttpURLConnection")(HttpURLConnectionBackend(), ForceWrappedValue.id) - runTests("TryHttpURLConnection")(TryHttpURLConnectionBackend(), ForceWrappedValue.scalaTry) - runTests("Akka HTTP")(AkkaHttpBackend.usingActorSystem(actorSystem), ForceWrappedValue.future) - runTests("Async Http Client - Future")(AsyncHttpClientFutureBackend(), ForceWrappedValue.future) - runTests("Async Http Client - Scalaz")(AsyncHttpClientScalazBackend(), ForceWrappedValue.scalazTask) - runTests("Async Http Client - Monix")(AsyncHttpClientMonixBackend(), ForceWrappedValue.monixTask) - runTests("Async Http Client - Cats Effect")(AsyncHttpClientCatsBackend[cats.effect.IO](), ForceWrappedValue.catsIo) - runTests("OkHttpSyncClientHandler")(OkHttpSyncBackend(), ForceWrappedValue.id) - runTests("OkHttpAsyncClientHandler - Future")(OkHttpFutureBackend(), ForceWrappedValue.future) - runTests("OkHttpAsyncClientHandler - Monix")(OkHttpMonixBackend(), ForceWrappedValue.monixTask) - - def runTests[R[_]](name: String)(implicit backend: SttpBackend[R, Nothing], - forceResponse: ForceWrappedValue[R]): Unit = { + runTests("HttpURLConnection")(HttpURLConnectionBackend(), ConvertToFuture.id) + runTests("TryHttpURLConnection")(TryHttpURLConnectionBackend(), ConvertToFuture.scalaTry) + runTests("Akka HTTP")(AkkaHttpBackend.usingActorSystem(actorSystem), ConvertToFuture.future) + runTests("Async Http Client - Future")(AsyncHttpClientFutureBackend(), ConvertToFuture.future) + runTests("Async Http Client - Scalaz")(AsyncHttpClientScalazBackend(), convertScalazTaskToFuture) + runTests("Async Http Client - Monix")(AsyncHttpClientMonixBackend(), convertMonixTaskToFuture) + runTests("Async Http Client - Cats IO")(AsyncHttpClientCatsBackend[cats.effect.IO](), convertCatsIOToFuture) + runTests("OkHttpSyncClientHandler")(OkHttpSyncBackend(), ConvertToFuture.id) + runTests("OkHttpAsyncClientHandler - Future")(OkHttpFutureBackend(), ConvertToFuture.future) + runTests("OkHttpAsyncClientHandler - Monix")(OkHttpMonixBackend(), convertMonixTaskToFuture) + + def runTests[R[_]](name: String)(implicit + backend: SttpBackend[R, Nothing], + convertToFuture: ConvertToFuture[R]): Unit = { closeBackends = (() => backend.close()) :: closeBackends diff --git a/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala index e3dbe2c..b2b44a2 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/StreamingTests.scala @@ -3,6 +3,7 @@ package com.softwaremill.sttp import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.softwaremill.sttp.streaming._ +import com.softwaremill.sttp.testing.streaming.TestStreamingBackend import com.typesafe.scalalogging.StrictLogging import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/AkkaHttpStreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/AkkaHttpStreamingTests.scala index d8fbb82..691df81 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/AkkaHttpStreamingTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/streaming/AkkaHttpStreamingTests.scala @@ -5,8 +5,9 @@ import akka.actor.ActorSystem import akka.stream.Materializer import akka.stream.scaladsl.Source import akka.util.ByteString +import com.softwaremill.sttp.SttpBackend import com.softwaremill.sttp.akkahttp.AkkaHttpBackend -import com.softwaremill.sttp.{ForceWrappedValue, SttpBackend} +import com.softwaremill.sttp.testing.streaming.{ConvertToFuture, TestStreamingBackend} import scala.concurrent.Future @@ -16,8 +17,8 @@ class AkkaHttpStreamingTests(actorSystem: ActorSystem)(implicit materializer: Ma override implicit val backend: SttpBackend[Future, Source[ByteString, Any]] = AkkaHttpBackend.usingActorSystem(actorSystem) - override implicit val forceResponse: ForceWrappedValue[Future] = - ForceWrappedValue.future + override implicit val convertToFuture: ConvertToFuture[Future] = + ConvertToFuture.future override def bodyProducer(body: String): Source[ByteString, NotUsed] = Source.single(ByteString(body)) diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientFs2StreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientFs2StreamingTests.scala index 82f36be..8959ccc 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientFs2StreamingTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientFs2StreamingTests.scala @@ -3,18 +3,19 @@ package com.softwaremill.sttp.streaming import java.nio.ByteBuffer import cats.effect._ -import cats.implicits._ +import cats.instances.string._ +import com.softwaremill.sttp.SttpBackend import com.softwaremill.sttp.asynchttpclient.fs2.AsyncHttpClientFs2Backend -import com.softwaremill.sttp.{ForceWrappedValue, SttpBackend} -import fs2._ +import com.softwaremill.sttp.impl.cats.convertCatsIOToFuture +import com.softwaremill.sttp.testing.streaming.{ConvertToFuture, TestStreamingBackend} +import fs2.{Chunk, Stream, text} class AsyncHttpClientFs2StreamingTests extends TestStreamingBackend[IO, Stream[IO, ByteBuffer]] { override implicit val backend: SttpBackend[IO, Stream[IO, ByteBuffer]] = AsyncHttpClientFs2Backend[IO]() - override implicit val forceResponse: ForceWrappedValue[IO] = - ForceWrappedValue.catsIo + override implicit val convertToFuture: ConvertToFuture[IO] = convertCatsIOToFuture override def bodyProducer(body: String): Stream[IO, ByteBuffer] = Stream.emits(body.getBytes("utf-8").map(b => ByteBuffer.wrap(Array(b)))) diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientMonixStreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientMonixStreamingTests.scala index 0357668..faebf8b 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientMonixStreamingTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/streaming/AsyncHttpClientMonixStreamingTests.scala @@ -4,10 +4,11 @@ import java.nio.ByteBuffer import com.softwaremill.sttp.SttpBackend import com.softwaremill.sttp.asynchttpclient.monix.AsyncHttpClientMonixBackend +import com.softwaremill.sttp.impl.monix.MonixTestStreamingBackend import monix.eval.Task import monix.reactive.Observable -class AsyncHttpClientMonixStreamingTests extends MonixBaseBackend { +class AsyncHttpClientMonixStreamingTests extends MonixTestStreamingBackend { import monix.execution.Scheduler.Implicits.global diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/MonixBaseBackend.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/MonixBaseBackend.scala deleted file mode 100644 index acd67a7..0000000 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/MonixBaseBackend.scala +++ /dev/null @@ -1,23 +0,0 @@ -package com.softwaremill.sttp.streaming - -import java.nio.ByteBuffer - -import com.softwaremill.sttp.ForceWrappedValue -import monix.eval.Task -import monix.reactive.Observable - -trait MonixBaseBackend extends TestStreamingBackend[Task, Observable[ByteBuffer]] { - - override implicit def forceResponse: ForceWrappedValue[Task] = - ForceWrappedValue.monixTask - - override def bodyProducer(body: String): Observable[ByteBuffer] = - Observable.fromIterable(body.getBytes("utf-8").map(b => ByteBuffer.wrap(Array(b)))) - - override def bodyConsumer(stream: Observable[ByteBuffer]): Task[String] = - stream - .flatMap(bb => Observable.fromIterable(bb.array())) - .toListL - .map(bs => new String(bs.toArray, "utf8")) - -} diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/OkHttpMonixStreamingTests.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/OkHttpMonixStreamingTests.scala index 9bfcc26..27a4517 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/OkHttpMonixStreamingTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/streaming/OkHttpMonixStreamingTests.scala @@ -3,11 +3,12 @@ package com.softwaremill.sttp.streaming import java.nio.ByteBuffer import com.softwaremill.sttp.SttpBackend +import com.softwaremill.sttp.impl.monix.MonixTestStreamingBackend import com.softwaremill.sttp.okhttp.monix.OkHttpMonixBackend import monix.eval.Task import monix.reactive.Observable -class OkHttpMonixStreamingTests extends MonixBaseBackend { +class OkHttpMonixStreamingTests extends MonixTestStreamingBackend { import monix.execution.Scheduler.Implicits.global diff --git a/tests/src/test/scala/com/softwaremill/sttp/streaming/TestStreamingBackend.scala b/tests/src/test/scala/com/softwaremill/sttp/streaming/TestStreamingBackend.scala deleted file mode 100644 index 5fc1c57..0000000 --- a/tests/src/test/scala/com/softwaremill/sttp/streaming/TestStreamingBackend.scala +++ /dev/null @@ -1,15 +0,0 @@ -package com.softwaremill.sttp.streaming - -import com.softwaremill.sttp.{ForceWrappedValue, SttpBackend} - -import scala.language.higherKinds - -trait TestStreamingBackend[R[_], S] { - implicit def backend: SttpBackend[R, S] - - implicit def forceResponse: ForceWrappedValue[R] - - def bodyProducer(body: String): S - - def bodyConsumer(stream: S): R[String] -} diff --git a/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala index 1a57b7e..12fe770 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala @@ -12,11 +12,8 @@ import org.scalatest.exceptions.TestFailedException import org.scalatest.matchers.{MatchResult, Matcher} import org.scalatest.{BeforeAndAfterAll, Suite} -import scala.concurrent.Future import scala.concurrent.duration._ import scala.language.higherKinds -import scala.util.Try -import scalaz._ trait TestHttpServer extends BeforeAndAfterAll with ScalaFutures with TestingPatience { this: Suite => @@ -38,60 +35,18 @@ trait TestHttpServer extends BeforeAndAfterAll with ScalaFutures with TestingPat def port: Int } -trait ForceWrappedValue[R[_]] { - def force[T](wrapped: R[T]): T -} - -object ForceWrappedValue extends ScalaFutures with TestingPatience { - - val id = new ForceWrappedValue[Id] { - override def force[T](wrapped: Id[T]): T = - wrapped - } - - val scalaTry = new ForceWrappedValue[Try] { - override def force[T](wrapped: Try[T]): T = wrapped.get - } - - val future = new ForceWrappedValue[Future] { - - override def force[T](wrapped: Future[T]): T = - try { - wrapped.futureValue - } catch { - case e: TestFailedException if e.getCause != null => throw e.getCause - } - } - val scalazTask = new ForceWrappedValue[scalaz.concurrent.Task] { - override def force[T](wrapped: scalaz.concurrent.Task[T]): T = - wrapped.unsafePerformSyncAttempt match { - case -\/(error) => throw error - case \/-(value) => value - } - } - val monixTask = new ForceWrappedValue[monix.eval.Task] { - import monix.execution.Scheduler.Implicits.global +trait ForceWrapped extends ScalaFutures with TestingPatience { this: Suite => + type ConvertToFuture[R[_]] = + com.softwaremill.sttp.testing.streaming.ConvertToFuture[R] - override def force[T](wrapped: monix.eval.Task[T]): T = + implicit class ForceDecorator[R[_], T](wrapped: R[T]) { + def force()(implicit ctf: ConvertToFuture[R]): T = { try { - wrapped.runAsync.futureValue + ctf.toFuture(wrapped).futureValue } catch { case e: TestFailedException if e.getCause != null => throw e.getCause } - } - val catsIo = new ForceWrappedValue[cats.effect.IO] { - override def force[T](wrapped: cats.effect.IO[T]): T = - wrapped.unsafeRunSync - } -} - -trait ForceWrapped extends ScalaFutures with TestingPatience { this: Suite => - type ForceWrappedValue[R[_]] = com.softwaremill.sttp.ForceWrappedValue[R] - val ForceWrappedValue: com.softwaremill.sttp.ForceWrappedValue.type = - com.softwaremill.sttp.ForceWrappedValue - - implicit class ForceDecorator[R[_], T](wrapped: R[T]) { - def force()(implicit fwv: ForceWrappedValue[R]): T = fwv.force(wrapped) + } } } |