From a1329a4bfc93b2c75f749224926849932cd1f7bc Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Wed, 15 Nov 2023 16:38:17 +0900 Subject: [PATCH] Skip interceptor for methods that are not registered in the server Co-authored-by: Xander Johnson --- src/grpc_interceptor/server.py | 4 ++++ tests/test_server.py | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/grpc_interceptor/server.py b/src/grpc_interceptor/server.py index 6cd6ebc..6733d0f 100644 --- a/src/grpc_interceptor/server.py +++ b/src/grpc_interceptor/server.py @@ -121,6 +121,10 @@ async def intercept_service(self, continuation, handler_call_details): have a public name. Do not override it, unless you know what you're doing. """ next_handler = await continuation(handler_call_details) + # Returns None if the method isn't implemented. + if not next_handler: + return + handler_factory, next_handler_method = _get_factory_and_method(next_handler) if next_handler.response_streaming: diff --git a/tests/test_server.py b/tests/test_server.py index f7ca614..d0d9553 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -177,15 +177,19 @@ def test_aborting_interceptor(aio): assert e.value.details() == "oh no" -def test_method_not_found(): +@pytest.mark.parametrize("aio", [False, True]) +def test_method_not_found(aio): """Calling undefined endpoints should return Unimplemented. Interceptors are not invoked when the RPC call is not handled. """ - intr = CountingInterceptor() + intr_type = AsyncCountingInterceptor if aio else CountingInterceptor + intr = intr_type() interceptors = [intr] - with dummy_channel(special_cases={}, interceptors=interceptors) as channel: + with dummy_channel( + special_cases={}, interceptors=interceptors, aio_server=aio + ) as channel: with pytest.raises(grpc.RpcError) as e: channel.unary_unary( "/DummyService/Unimplemented",