-
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.
testing: Add a test for the grpc-web-proxy
We create a standalone service and front it with the grpc-web-proxy. Since the proxy must not rely on the payload to make decisions we just implemented a simple test proto just for this case.
- Loading branch information
Showing
9 changed files
with
291 additions
and
26 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
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,17 @@ | ||
// Just a small grpc definition to test the grpcweb implementation. | ||
|
||
syntax = "proto3"; | ||
|
||
package gltesting; | ||
|
||
service Greeter { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import purerpc | ||
import gltesting.test_pb2 as gltesting_dot_test__pb2 | ||
|
||
|
||
class GreeterServicer(purerpc.Servicer): | ||
async def SayHello(self, input_message): | ||
raise NotImplementedError() | ||
|
||
@property | ||
def service(self) -> purerpc.Service: | ||
service_obj = purerpc.Service( | ||
"gltesting.Greeter" | ||
) | ||
service_obj.add_method( | ||
"SayHello", | ||
self.SayHello, | ||
purerpc.RPCSignature( | ||
purerpc.Cardinality.UNARY_UNARY, | ||
gltesting_dot_test__pb2.HelloRequest, | ||
gltesting_dot_test__pb2.HelloReply, | ||
) | ||
) | ||
return service_obj | ||
|
||
|
||
class GreeterStub: | ||
def __init__(self, channel): | ||
self._client = purerpc.Client( | ||
"gltesting.Greeter", | ||
channel | ||
) | ||
self.SayHello = self._client.get_method_stub( | ||
"SayHello", | ||
purerpc.RPCSignature( | ||
purerpc.Cardinality.UNARY_UNARY, | ||
gltesting_dot_test__pb2.HelloRequest, | ||
gltesting_dot_test__pb2.HelloReply, | ||
) | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
# Tests that use a grpc-web client, without a client certificate, but | ||
# payload signing for authentication. | ||
|
||
from gltesting.fixtures import * | ||
from gltesting.test_pb2_grpc import GreeterStub | ||
from gltesting.test_pb2 import HelloRequest | ||
import sonora.client | ||
|
||
def test_start(grpc_web_proxy): | ||
with sonora.client.insecure_web_channel( | ||
f"http://localhost:{grpc_web_proxy.web_port}" | ||
) as channel: | ||
stub = GreeterStub(channel) | ||
req = HelloRequest(name="greenlight") | ||
print(stub.SayHello(req)) | ||
|
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,36 @@ | ||
# This is a simple grpc server serving the `gltesting/test.proto` | ||
# protocol. It is used to test whether the grpc-web to grpc/h2 | ||
# proxying is working. | ||
|
||
from gltesting.test_pb2 import HelloRequest, HelloReply | ||
from gltesting.test_grpc import GreeterServicer | ||
from ephemeral_port_reserve import reserve | ||
import purerpc | ||
from threading import Thread | ||
import anyio | ||
|
||
|
||
|
||
class Server(GreeterServicer): | ||
def __init__(self, *args, **kwargs): | ||
GreeterServicer.__init__(self, *args, **kwargs) | ||
self.grpc_port = reserve() | ||
self.inner = purerpc.Server(self.grpc_port) | ||
self.thread: Thread | None = None | ||
self.inner.add_service(self.service) | ||
|
||
async def SayHello(self, message): | ||
return HelloReply(message="Hello, " + message.name) | ||
|
||
def start(self): | ||
def target(): | ||
try: | ||
anyio.run(self.inner.serve_async) | ||
except Exception as e: | ||
print("Error starting the grpc backend") | ||
|
||
self.thread = Thread(target=target, daemon=True) | ||
self.thread.start() | ||
|
||
def stop(self): | ||
self.inner.aclose |