aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala/com/softwaremill/sttp/testing/streaming/StreamingTest.scala
blob: aa0af0782b74fa62c50648bb6d381ba74bc5b364 (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
package com.softwaremill.sttp.testing.streaming

import com.softwaremill.sttp._
import com.softwaremill.sttp.testing.ForceWrapped
import org.scalatest.{AsyncFreeSpec, BeforeAndAfterAll, Matchers}

import scala.language.higherKinds

trait StreamingTest[R[_], S] extends AsyncFreeSpec with Matchers with BeforeAndAfterAll with ForceWrapped {

  private val endpoint = "localhost:51823"
  private val body = "streaming test"

  val testStreamingBackend: TestStreamingBackend[R, S]
  import testStreamingBackend._

  "stream request body" in {
    sttp
      .post(uri"$endpoint/streaming/echo")
      .streamBody(bodyProducer(body))
      .send()
      .toFuture()
      .map { response =>
        response.unsafeBody shouldBe body
      }
  }

  "receive a stream" in {
    sttp
      .post(uri"$endpoint/streaming/echo")
      .body(body)
      .response(asStream[S])
      .send()
      .toFuture()
      .flatMap { response =>
        bodyConsumer(response.unsafeBody).toFuture()
      }
      .map { responseBody =>
        responseBody shouldBe body
      }
  }

  "receive a stream from an https site" in {
    sttp
    // of course, you should never rely on the internet being available
    // in tests, but that's so much easier than setting up an https
    // testing server
      .get(uri"https://softwaremill.com")
      .response(asStream[S])
      .send()
      .toFuture()
      .flatMap { response =>
        bodyConsumer(response.unsafeBody).toFuture()
      }
      .map { responseBody =>
        responseBody should include("</div>")
      }
  }

  override protected def afterAll(): Unit = {
    testStreamingBackend.backend.close()
    super.afterAll()
  }

}