blob: 5e6db173cc9de976e972fee77a5701fa8c01c6bb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.softwaremill.sttp
import java.nio.ByteBuffer
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.softwaremill.sttp.asynchttpclient.monix.MonixAsyncHttpClientHandler
import com.typesafe.scalalogging.StrictLogging
import monix.reactive.Observable
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
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()
monixStreamingTests()
val body = "streaming test"
def akkaStreamingTests(): Unit = {
implicit val handler = AkkaHttpSttpHandler.usingActorSystem(actorSystem)
"Akka HTTP" should "stream request body" in {
val response = sttp
.post(uri"$endpoint/echo")
.streamBody(Source.single(ByteString(body)))
.send()
.futureValue
response.body should be(body)
}
it should "receive a stream" in {
val response = sttp
.post(uri"$endpoint/echo")
.body(body)
.response(asStream[Source[ByteString, Any]])
.send()
.futureValue
val responseBody = response.body.runReduce(_ ++ _).futureValue.utf8String
responseBody should be(body)
}
}
def monixStreamingTests(): Unit = {
implicit val handler = MonixAsyncHttpClientHandler()
import monix.execution.Scheduler.Implicits.global
val body = "streaming test"
"Monix Async Http Client" should "stream request body" in {
val source = Observable.fromIterable(
body.getBytes("utf-8").map(b => ByteBuffer.wrap(Array(b))))
val response = sttp
.post(uri"$endpoint/echo")
.streamBody(source)
.send()
.runAsync
.futureValue
response.body should be(body)
}
it should "receive a stream" in {
val response = sttp
.post(uri"$endpoint/echo")
.body(body)
.response(asStream[Observable[ByteBuffer]])
.send()
.runAsync
.futureValue
val bytes = response.body
.flatMap(bb => Observable.fromIterable(bb.array()))
.toListL
.runAsync
.futureValue
.toArray
new String(bytes, "utf-8") should be(body)
}
}
}
|