Skip to content

Commit ccfc100

Browse files
committed
Refactor RawRestTest to handle sjs limitations
1 parent be1bc63 commit ccfc100

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

rest/src/test/scala/io/udash/rest/raw/RawRestTest.scala

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ package raw
55
import com.avsystem.commons.*
66
import com.avsystem.commons.annotation.AnnotationAggregate
77
import com.avsystem.commons.serialization.{transientDefault, whenAbsent}
8+
import io.udash.rest.raw.StreamedBody.castOrFail
89
import io.udash.rest.util.WithHeaders
910
import monix.eval.Task
1011
import monix.execution.Scheduler
1112
import monix.reactive.Observable
1213
import org.scalactic.source.Position
13-
import org.scalatest.Assertion
1414
import org.scalatest.concurrent.ScalaFutures
1515
import org.scalatest.funsuite.AnyFunSuite
1616
import org.scalatest.matchers.should.Matchers
1717

18-
import scala.concurrent.duration.*
19-
2018
case class UserId(id: String) extends AnyVal {
2119
override def toString: String = id
2220
}
@@ -87,7 +85,6 @@ trait RootApi {
8785
}
8886
object RootApi extends DefaultRestApiCompanion[RootApi]
8987
class RawRestTest extends AnyFunSuite with ScalaFutures with Matchers {
90-
implicit override def patienceConfig: PatienceConfig = PatienceConfig(timeout = 5.seconds)
9188
implicit def scheduler: Scheduler = Scheduler.global
9289

9390
def repr(body: HttpBody, inNewLine: Boolean = true): String = body match {
@@ -244,23 +241,6 @@ class RawRestTest extends AnyFunSuite with ScalaFutures with Matchers {
244241
}
245242
}
246243

247-
def assertRawStreamingExchange(
248-
request: RestRequest,
249-
assertResponse: StreamedRestResponse => Future[Assertion]
250-
)(implicit pos: Position): Unit = {
251-
val futureResponse: Future[AbstractRestResponse] = serverHandleWithStreaming(request).runToFuture
252-
253-
val assertionFuture: Future[Assertion] = futureResponse.flatMap {
254-
case stream: StreamedRestResponse =>
255-
assertResponse(stream)
256-
case resp: RestResponse =>
257-
Future.successful(fail(s"Expected StreamedRestResponse but got RestResponse: $resp"))
258-
}
259-
whenReady(assertionFuture) { assertionResult =>
260-
assertionResult
261-
}
262-
}
263-
264244
test("simple GET") {
265245
testRestCall(_.self.user(UserId("ID")),
266246
"""-> GET /user?userId=ID
@@ -485,12 +465,12 @@ class RawRestTest extends AnyFunSuite with ScalaFutures with Matchers {
485465
|01020304
486466
|""".stripMargin
487467

488-
val realResultFuture = real.self.streamBinary(inputBytes).toListL.runToFuture.map(_.map(_.toList))
489-
val proxyResultFuture = realStreamingProxy.self.streamBinary(inputBytes).toListL.runToFuture.map(_.map(_.toList))
468+
val realResultFuture = real.self.streamBinary(inputBytes).toListL.runToFuture
469+
val proxyResultFuture = realStreamingProxy.self.streamBinary(inputBytes).toListL.runToFuture
490470

491471
whenReady(realResultFuture) { realResult =>
492472
whenReady(proxyResultFuture) { proxyResult =>
493-
proxyResult shouldBe realResult
473+
proxyResult.map(_.toList) shouldBe realResult.map(_.toList)
494474
trafficLog shouldBe expectedTraffic
495475
}
496476
}
@@ -524,17 +504,15 @@ class RawRestTest extends AnyFunSuite with ScalaFutures with Matchers {
524504
RestParameters(PlainValue.decodePath("streamNumbers"), query = Mapping(ISeq("count" -> PlainValue("4")))),
525505
HttpBody.Empty
526506
)
527-
528-
assertRawStreamingExchange(request, { response =>
529-
response.code shouldBe 200
530-
response.body match {
531-
case StreamedBody.JsonList(elements, _) =>
532-
elements.toListL.runToFuture.map { elementList =>
533-
elementList.map(_.value) shouldBe List("1", "2", "3", "4")
534-
}
535-
case other =>
536-
Future.successful(fail(s"Expected JsonList body, got $other"))
507+
whenReady(serverHandleWithStreaming(request).runToFuture) {
508+
case StreamedRestResponse(code, headers, body, batchSize) => {
509+
code shouldBe 200
510+
val elements = castOrFail[StreamedBody.JsonList](body).elements
511+
whenReady(elements.toListL.runToFuture) { e =>
512+
e shouldBe List(JsonValue("1"), JsonValue("2"), JsonValue("3"), JsonValue("4"))
513+
}
537514
}
538-
})
515+
case _ => fail()
516+
}
539517
}
540518
}

0 commit comments

Comments
 (0)