-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayAPISpec.scala
115 lines (91 loc) · 3.43 KB
/
PlayAPISpec.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
package controllers.common
import org.scalatest.concurrent.ScalaFutures
import org.scalatestplus.play.PlaySpec
import org.slf4j.LoggerFactory
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.Result
import play.api.test.FakeRequest
import play.api.test.Helpers.*
import play.api.{Application, Mode}
import java.net.URLEncoder
import scala.concurrent.Future
/** A PlayAPISpec allow us to write tests for the API calls.
*/
trait PlayAPISpec extends PlaySpec with ScalaFutures {
protected def defaultGuiceApplicationBuilder: GuiceApplicationBuilder =
GuiceApplicationBuilder()
.in(Mode.Test)
private val JsonHeader = CONTENT_TYPE -> "application/json"
private val EmptyJson = "{}"
protected val logger = LoggerFactory.getLogger(this.getClass)
def log[T](request: FakeRequest[T], response: Future[Result]): Unit = {
logger.info(
s"REQUEST > $request, headers = ${request.headers}; RESPONSE < status = ${status(response)}, body = ${contentAsString(response)}"
)
}
/** Syntactic sugar for calling APIs * */
def GET(url: String, extraHeaders: (String, String)*)(implicit application: Application): Future[Result] = {
val headers = JsonHeader :: extraHeaders.toList
val request = FakeRequest("GET", url)
.withHeaders(headers: _*)
val response = route(application, request).value
log(request, response)
response
}
def POST(url: String, extraHeaders: (String, String)*)(implicit application: Application): Future[Result] = {
POST(url, EmptyJson, extraHeaders: _*)
}
def POST(url: String, jsonBody: String, extraHeaders: (String, String)*)(implicit
application: Application
): Future[Result] = {
val headers = JsonHeader :: extraHeaders.toList
val request = FakeRequest("POST", url)
.withHeaders(headers: _*)
.withBody(jsonBody)
val response = route(application, request).value
log(request, response)
response
}
def PUT(url: String, extraHeaders: (String, String)*)(implicit application: Application): Future[Result] = {
PUT(url, EmptyJson, extraHeaders: _*)
}
def PUT(url: String, jsonBody: String, extraHeaders: (String, String)*)(implicit
application: Application
): Future[Result] = {
val headers = JsonHeader :: extraHeaders.toList
val request = FakeRequest("PUT", url)
.withHeaders(headers: _*)
.withBody(jsonBody)
val response = route(application, request).value
log(request, response)
response
}
def DELETE(url: String, extraHeaders: (String, String)*)(implicit application: Application): Future[Result] = {
val headers = JsonHeader :: extraHeaders.toList
val request = FakeRequest("DELETE", url)
.withHeaders(headers: _*)
val response = route(application, request).value
log(request, response)
response
}
}
object PlayAPISpec {
object Implicits {
implicit class HttpExt(val params: List[(String, String)]) extends AnyVal {
def toQueryString: String = {
params
.map { case (key, value) =>
val encodedKey = URLEncoder.encode(key, "UTF-8")
val encodedValue = URLEncoder.encode(value, "UTF-8")
List(encodedKey, encodedValue).mkString("=")
}
.mkString("&")
}
}
implicit class StringUrlExt(val url: String) extends AnyVal {
def withQueryParams(params: (String, String)*): String = {
List(url, params.toList.toQueryString).mkString("?")
}
}
}
}