-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathHttpSpec.scala
131 lines (119 loc) · 4.05 KB
/
HttpSpec.scala
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.evolutiongaming.bootcamp.http
import cats.effect.IO
import cats.syntax.option._
import com.evolutiongaming.bootcamp.http.HttpServer.httpApp
import fs2.Stream
import org.http4s._
import org.http4s.headers._
import org.http4s.implicits._
import org.http4s.multipart.{Multipart, Part}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
class HttpSpec extends AnyFreeSpec with Matchers {
import cats.effect.unsafe.implicits.global
"HttpService should" - {
"validate timestamp" in {
val validResponseIO = httpApp.run(
Request(
method = Method.GET,
uri = uri"/params/validate"
.withQueryParam(key = "timestamp", value = "2020-11-04T14:19:54.736Z"),
)
)
check[String](
actualResponseIO = validResponseIO,
expectedStatus = Status.Ok,
expectedBody = "Timestamp is valid".some,
)
val invalidResponseIO = httpApp.run(
Request(
method = Method.GET,
uri = uri"/params/validate"
.withQueryParam(key = "timestamp", value = "2020-40-04T14:19:54.736Z"),
)
)
check[String](
actualResponseIO = invalidResponseIO,
expectedStatus = Status.BadRequest,
expectedBody = "Timestamp is invalid".some,
)
}
"increment counter cookie" in {
val incrementedCounterResponseIO = httpApp.run(
Request(method = Method.GET, uri = uri"/cookies").addCookie("counter", "9")
)
check[String](
actualResponseIO = incrementedCounterResponseIO,
expectedStatus = Status.Ok,
expectedBody = None,
expectedResponseCookie = ResponseCookie("counter", "10").some,
)
val newCounterResponseIO = httpApp.run(
Request(method = Method.GET, uri = uri"/cookies")
)
check[String](
actualResponseIO = newCounterResponseIO,
expectedStatus = Status.Ok,
expectedBody = None,
expectedResponseCookie = ResponseCookie("counter", "1").some,
)
}
"count character occurrences" in {
def multipartRequestWith(characterValue: String): Request[IO] = {
val multipart = Multipart[IO](
Vector(
Part.formData(name = "character", value = characterValue),
Part.fileData(
name = "file",
filename = "test.txt",
entityBody = Stream.emits(os = "Some test text here...".map(_.toByte)),
headers = `Content-Type`(MediaType.text.plain),
),
)
)
val multipartBody = EntityEncoder[IO, Multipart[IO]].toEntity(multipart).body
Request(
method = Method.POST,
uri = uri"/multipart",
headers = multipart.headers,
body = multipartBody,
)
}
val validResponseIO = httpApp.run {
multipartRequestWith(characterValue = "e")
}
check[String](
actualResponseIO = validResponseIO,
expectedStatus = Status.Ok,
expectedBody = "5".some,
)
val invalidResponseIO = httpApp.run {
multipartRequestWith(characterValue = "abc")
}
check[String](
actualResponseIO = invalidResponseIO,
expectedStatus = Status.BadRequest,
expectedBody = None,
)
}
}
private def check[A](
actualResponseIO: IO[Response[IO]],
expectedStatus: Status,
expectedBody: Option[A],
expectedResponseCookie: Option[ResponseCookie] = None,
)(implicit
decoder: EntityDecoder[IO, A]
): Unit = (for {
actualResponse <- actualResponseIO
_ <- IO(actualResponse.status shouldBe expectedStatus)
_ <- expectedBody match {
case None => actualResponse.body.compile.toVector.map(_ shouldBe empty)
case Some(body) => actualResponse.as[A].map(_ shouldBe body)
}
_ <- expectedResponseCookie match {
case None => IO.unit
case Some(responseCookie) => IO(actualResponse.cookies should contain(responseCookie))
}
} yield ()).unsafeRunSync()
}