forked from leanovate/play-mockws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.scala
118 lines (90 loc) · 3.03 KB
/
Example.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
package mockws
import mockws.MockWSHelpers._
import org.scalatest.{FreeSpec, Matchers, OptionValues}
import play.api.libs.ws.WSClient
import play.api.mvc.Results._
import play.api.test.Helpers._
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.Future
import scala.util.Try
object ImplementationToTest {
// GatewayToTest simulates an gateway implementation we want to test
object GatewayToTest {
val userServiceUrl = "http://userservice"
}
/** @param ws [[WSClient]] as dependency injection */
class GatewayToTest(ws: WSClient) {
import GatewayToTest.userServiceUrl
/** @return age of the user if known */
def age(userId: Long): Future[Option[Int]] =
ws.url(s"$userServiceUrl/users/$userId/age").get().map {
case response if response.status == 200 => Try(response.body.toInt).toOption
case _ => None
}
}
}
trait TestScope {
import ImplementationToTest._
val userServiceUrl = GatewayToTest.userServiceUrl
def userRoute: Route
def ageResponse(userId: Long) = {
// we initialize a mock WS with the defined route
val ws = MockWS(userRoute)
// we inject the MockWS into GatewayToTest
val testedGateway = new GatewayToTest(ws)
try await(testedGateway.age(userId))
finally ws.close()
}
}
class Example extends FreeSpec with Matchers with OptionValues {
// and we can test the implementation of GatewayToTest
"GatewayToTest.age should" - {
"return None" - {
"when the user service does not know the user" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/23/age" => Action {
NotFound("user 23 not known")
}
}
ageResponse(userId = 23) shouldEqual None
}
"when the user service response is not an Integer" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/27/age" => Action {
Ok("crap")
}
}
ageResponse(userId = 27) shouldEqual None
}
}
"return the age of the user" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/5/age" => Action {
Ok("67")
}
}
ageResponse(userId = 5).value shouldEqual 67
}
}
}
/**
* Just a demonstration that you can also import the MockWSHelpers and do not have to mix it in as trait.
*
* {{{
* import mockws.MockWSHelpers._
* }}}
*/
class ExampleWithImportMockWSHelpers extends FreeSpec with Matchers with OptionValues {
// Just a simple import and Actions work, no mixins
import mockws.MockWSHelpers._
"GatewayToTest.age should" - {
"work properly when we import the MockWSHelpers (and do not use it as trait)" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/23/age" => Action {
NotFound("user 23 not known")
}
}
ageResponse(userId = 23) shouldEqual None
}
}
}