-
Notifications
You must be signed in to change notification settings - Fork 124
/
JGreeterServiceSpec.scala
72 lines (59 loc) · 2.54 KB
/
JGreeterServiceSpec.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
/*
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package example.myapp.helloworld
import scala.concurrent.Await
import org.scalatest.BeforeAndAfterAll
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.grpc.GrpcClientSettings
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.Span
import example.myapp.helloworld.grpc._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class JGreeterServiceSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll with ScalaFutures {
implicit val patience = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis))
implicit val serverSystem: ActorSystem = {
// important to enable HTTP/2 in server ActorSystem's config
val conf = ConfigFactory
.parseString("akka.http.server.preview.enable-http2 = on")
.withFallback(ConfigFactory.defaultApplication())
val sys = ActorSystem("GreeterServer", conf)
// make sure servers are bound before using client
GreeterServer.run(sys).toCompletableFuture.get
PowerGreeterServer.run(sys).toCompletableFuture.get
sys
}
val clientSystem = ActorSystem("GreeterClient")
implicit val mat = ActorMaterializer.create(clientSystem)
implicit val ec = clientSystem.dispatcher
val clients = Seq(8080, 8081).map { port =>
GreeterServiceClient.create(GrpcClientSettings.connectToServiceAt("127.0.0.1", port).withTls(false), mat, ec)
}
override def afterAll: Unit = {
Await.ready(clientSystem.terminate(), 5.seconds)
Await.ready(serverSystem.terminate(), 5.seconds)
}
"GreeterService" should {
"reply to single request" in {
val reply = clients.head.sayHello(HelloRequest.newBuilder.setName("Alice").build())
reply.toCompletableFuture.get should ===(HelloReply.newBuilder.setMessage("Hello, Alice").build())
}
}
"GreeterServicePowerApi" should {
Seq(("Authorization", "Hello, Alice (authenticated)"), ("WrongHeaderName", "Hello, Alice (not authenticated)")).zipWithIndex
.foreach {
case ((mdName, expResp), ix) =>
s"use metadata in replying to single request ($ix)" in {
val reply = clients.last
.sayHello()
.addHeader(mdName, "<some auth token>")
.invoke(HelloRequest.newBuilder.setName("Alice").build())
reply.toCompletableFuture.get should ===(HelloReply.newBuilder.setMessage(expResp).build())
}
}
}
}