-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix router using actions template and add tests (#296)
* fix router using actions template and add tests * fix headers and formatting
- Loading branch information
Showing
5 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// must mirror the scala-interop-test one since that is shown in the docs | ||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "example.myapp.helloworld.grpc.actions"; | ||
option java_outer_classname = "HelloWorldProto"; | ||
|
||
package helloworld; | ||
|
||
service GreeterService { | ||
rpc SayHello (HelloRequest) returns (HelloReply) {} | ||
} | ||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
message HelloReply { | ||
string message = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
play-scalatest/src/test/scala/play/grpc/scalatest/PlayActionsScalaTestSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.grpc.scalatest | ||
|
||
import org.scalatest.concurrent.IntegrationPatience | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatestplus.play.PlaySpec | ||
import org.scalatestplus.play.guice.GuiceOneServerPerTest | ||
import play.api.Application | ||
import play.api.inject.bind | ||
import play.api.inject.guice.GuiceApplicationBuilder | ||
import play.api.libs.ws.WSClient | ||
import play.api.routing.Router | ||
import io.grpc.Status | ||
import example.myapp.helloworld.grpc.actions.helloworld._ | ||
|
||
import akka.grpc.internal.GrpcProtocolNative | ||
|
||
/** | ||
* Test for the Play gRPC ScalaTest APIs | ||
*/ | ||
class PlayActionsScalaTestSpec | ||
extends PlaySpec | ||
with GuiceOneServerPerTest | ||
with ServerGrpcClient | ||
with ScalaFutures | ||
with IntegrationPatience { | ||
|
||
override def fakeApplication(): Application = { | ||
GuiceApplicationBuilder() | ||
.overrides(bind[Router].to[GreeterServiceWithActionsImpl]) | ||
.build() | ||
} | ||
|
||
implicit def ws: WSClient = app.injector.instanceOf(classOf[WSClient]) | ||
|
||
"A Play server bound to a gRPC router using actions" must { | ||
"give a 404 when routing a non-gRPC request" in { | ||
val result = wsUrl("/").get.futureValue | ||
result.status must be(404) // Maybe should be a 426, see #396 | ||
} | ||
// this test results in a 500 | ||
// "give a 415 error when not using a gRPC content-type" in { | ||
// val result = wsUrl(s"/${GreeterService.name}/FooBar").get.futureValue | ||
// result.status must be(415) | ||
// } | ||
// this test results in a 500 error | ||
// "give a grpc 'unimplemented' error when routing a non-existent gRPC method" in { | ||
// val result = wsUrl(s"/${GreeterService.name}/FooBar") | ||
// .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString) | ||
// .get | ||
// .futureValue | ||
// result.status must be(200) // Maybe should be a 426, see #396 | ||
// result.header("grpc-status") mustEqual Some(Status.Code.UNIMPLEMENTED.value().toString) | ||
// } | ||
"give a grpc 'invalid argument' error when routing an empty request to a gRPC method" in { | ||
val result = wsUrl(s"/${GreeterService.name}/SayHello") | ||
.addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString) | ||
.get | ||
.futureValue | ||
result.status must be(200) | ||
result.header("grpc-status") mustEqual Some(Status.Code.INVALID_ARGUMENT.value().toString) | ||
} | ||
"work with a gRPC client" in withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient => | ||
val reply = client.sayHello(HelloRequest("Alice")).futureValue | ||
reply.message must be("Hello, Alice!") | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...cala/example/myapp/helloworld/grpc/actions/helloworld/GreeterServiceWithActionsImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (C) Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package example.myapp.helloworld.grpc.actions.helloworld | ||
|
||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import akka.stream.Materializer | ||
import akka.actor.ActorSystem | ||
import play.api.mvc.PlayBodyParsers | ||
import play.api.mvc.DefaultActionBuilder | ||
|
||
import scala.concurrent.Future | ||
|
||
@Singleton | ||
class GreeterServiceWithActionsImpl @Inject() ( | ||
implicit | ||
mat: Materializer, | ||
actorSystem: ActorSystem, | ||
parsers: PlayBodyParsers, | ||
actionBuilder: DefaultActionBuilder, | ||
) extends AbstractGreeterServiceRouter( | ||
mat, | ||
actorSystem, | ||
parsers, | ||
actionBuilder, | ||
) { | ||
|
||
override def sayHello(in: HelloRequest): Future[HelloReply] = { | ||
actorSystem.log.error("Saying hello!") | ||
Future.successful(HelloReply(s"Hello, ${in.name}!")) | ||
} | ||
|
||
} |