11package io .udash
22package rest
33
4+ import cats .implicits .catsSyntaxTuple2Semigroupal
45import com .avsystem .commons .*
56import com .avsystem .commons .misc .ScalaDurationExtensions .durationIntOps
67import io .udash .rest .raw .RawRest
78import io .udash .rest .raw .RawRest .HandleRequest
9+ import io .udash .testing .AsyncUdashSharedTest
810import monix .eval .Task
911import monix .execution .Scheduler
1012import org .scalactic .source .Position
11- import org .scalatest .BeforeAndAfterEach
12- import org .scalatest .concurrent .ScalaFutures
13- import org .scalatest .funsuite .AnyFunSuite
1413import org .scalatest .time .{Millis , Seconds , Span }
14+ import org .scalatest .{Assertion , BeforeAndAfterEach }
1515
1616import scala .concurrent .duration .FiniteDuration
1717
18- abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndAfterEach {
18+ abstract class RestApiTest extends AsyncUdashSharedTest with BeforeAndAfterEach {
1919 implicit def scheduler : Scheduler = Scheduler .global
2020
2121 protected final val MaxConnections : Int = 1 // to timeout quickly
@@ -38,11 +38,10 @@ abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndA
3838 lazy val proxy : RestTestApi =
3939 RawRest .fromHandleRequest[RestTestApi ](clientHandle)
4040
41- def testCall [T ](call : RestTestApi => Future [T ])(implicit pos : Position ): Unit =
42- assert(
43- call(proxy).wrapToTry.futureValue.map(mkDeep) ==
44- call(impl).catchFailures.wrapToTry.futureValue.map(mkDeep)
45- )
41+ def testCall [T ](call : RestTestApi => Future [T ])(implicit pos : Position ): Future [Assertion ] =
42+ (call(proxy).wrapToTry, call(impl).catchFailures.wrapToTry).mapN { (proxyResult, implResult) =>
43+ assert(proxyResult.map(mkDeep) == implResult.map(mkDeep))
44+ }
4645
4746 def mkDeep (value : Any ): Any = value match {
4847 case arr : Array [_] => IArraySeq .empty[AnyRef ] ++ arr.iterator.map(mkDeep)
@@ -53,72 +52,71 @@ abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndA
5352trait RestApiTestScenarios extends RestApiTest {
5453 override implicit val patienceConfig : PatienceConfig = PatienceConfig (scaled(Span (10 , Seconds )), scaled(Span (50 , Millis )))
5554
56- test( " trivial GET" ) {
55+ " trivial GET" in {
5756 testCall(_.trivialGet)
5857 }
5958
60- test( " failing GET" ) {
59+ " failing GET" in {
6160 testCall(_.failingGet)
6261 }
6362
64- test( " JSON failing GET" ) {
63+ " JSON failing GET" in {
6564 testCall(_.jsonFailingGet)
6665 }
6766
68- test( " more failing GET" ) {
67+ " more failing GET" in {
6968 testCall(_.moreFailingGet)
7069 }
7170
72- test( " complex GET" ) {
71+ " complex GET" in {
7372 testCall(_.complexGet(0 , " a/ +&" , 1 , " b/ +&" , 2 , " ć/ +&" , Opt (3 ), 4 , " ó /&f" ))
7473 testCall(_.complexGet(0 , " a/ +&" , 1 , " b/ +&" , 2 , " ć/ +&" , Opt .Empty , 3 , " ó /&f" ))
7574 }
7675
77- test( " multi-param body POST" ) {
76+ " multi-param body POST" in {
7877 testCall(_.multiParamPost(0 , " a/ +&" , 1 , " b/ +&" , 2 , " ć/ +&" , 3 , " l\" l" ))
7978 }
8079
81- test( " single body PUT" ) {
80+ " single body PUT" in {
8281 testCall(_.singleBodyPut(RestEntity (RestEntityId (" id" ), " señor" )))
8382 }
8483
85- test( " form POST" ) {
84+ " form POST" in {
8685 testCall(_.formPost(" ó" , " ą=ę" , 42 ))
8786 }
8887
89- test( " prefixed GET" ) {
88+ " prefixed GET" in {
9089 testCall(_.prefix(" p0" , " h0" , " q0" ).subget(0 , 1 , 2 ))
9190 }
9291
93- test( " transparent prefix GET" ) {
92+ " transparent prefix GET" in {
9493 testCall(_.transparentPrefix.subget(0 , 1 , 2 ))
9594 }
9695
97- test( " custom response with headers" ) {
96+ " custom response with headers" in {
9897 testCall(_.customResponse(" walue" ))
9998 }
10099
101- test( " binary request and response" ) {
100+ " binary request and response" in {
102101 testCall(_.binaryEcho(Array .fill[Byte ](5 )(5 )))
103102 }
104103
105- test( " large binary request and response" ) {
104+ " large binary request and response" in {
106105 testCall(_.binaryEcho(Array .fill[Byte ](1024 * 1024 )(5 )))
107106 }
108107
109- test( " body using third party type" ) {
108+ " body using third party type" in {
110109 testCall(_.thirdPartyBody(HasThirdParty (ThirdParty (5 ))))
111110 }
112111
113- test( " close connection on monix task timeout" ) {
112+ " close connection on monix task timeout" in {
114113 Task
115114 .traverse(List .range(0 , Connections ))(_ => Task .deferFuture(proxy.neverGet).timeout(CallTimeout ).failed)
116115 .map(_ => assertResult(expected = Connections )(actual = impl.counterValue())) // neverGet should be called Connections times
117116 .runToFuture
118- .futureValue
119117 }
120118
121- test( " close connection on monix task cancellation" ) {
119+ " close connection on monix task cancellation" in {
122120 Task
123121 .traverse(List .range(0 , Connections )) { i =>
124122 val cancelable = Task .deferFuture(proxy.neverGet).runAsync(_ => ())
@@ -128,7 +126,6 @@ trait RestApiTestScenarios extends RestApiTest {
128126 }
129127 .map(_ => assertResult(expected = Connections )(actual = impl.counterValue())) // neverGet should be called Connections times
130128 .runToFuture
131- .futureValue
132129 }
133130}
134131
0 commit comments