forked from leanovate/play-mockws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockWS.scala
63 lines (54 loc) · 1.73 KB
/
MockWS.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
package mockws
import java.util.UUID
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.{WSClient, WSRequest}
import play.api.mvc.EssentialAction
/**
* Mock implementation for the [[play.api.libs.ws.WSClient]].
* Usage:
* {{{
* val ws = MockWS {
* case ("GET", "/") => Action { Ok("index") }
* case ("GET", "/hi") => Action { Ok("world") }
* }
* }}}
*
* MockWS.Routes is a partial function.
* It is also possible to combine routes together:
* {{{
* val index = MockWS.Routes {
* case ("GET", "/") => Action { Ok("index") }
* }
* val hiWorld = MockWS.Routes {
* case ("GET", "/hi") => Action { Ok("world") }
* }
* val ws = MockWS(index orElse hiWorld)
* }}}
*
* @param routes routes defining the mock calls
*/
class MockWS(routes: MockWS.Routes, shutdownHook: () ⇒ Unit)(implicit val materializer: ActorMaterializer) extends WSClient {
require(routes != null)
override def underlying[T]: T = this.asInstanceOf[T]
override def close(): Unit = {
shutdownHook()
}
override def url(url: String): WSRequest = FakeWSRequestHolder(routes, url)
}
object MockWS {
type Routes = PartialFunction[(String, String), EssentialAction]
/**
* @param routes simulation of the external web resource
*/
def apply(routes: Routes) = {
implicit val system = ActorSystem("mock-ws-" + UUID.randomUUID().toString)
implicit val materializer = ActorMaterializer()
new MockWS(routes, () ⇒ system.terminate())
}
/**
* @param routes simulation of the external web resource
* @param materializer user-defined materializer
*/
def apply(routes: Routes, materializer: ActorMaterializer) = new MockWS(routes, () ⇒ Unit)(materializer)
}