Skip to content

Commit

Permalink
Skip interceptor for methods that are not registered in the server (#48)
Browse files Browse the repository at this point in the history
* Skip interceptor for methods that are not registered in the server

Co-authored-by: Xander Johnson <xander@metasyn.pw>

* Bump version

---------

Co-authored-by: Xander Johnson <xander@metasyn.pw>
  • Loading branch information
anuraaga and metasyn authored Nov 16, 2023
1 parent af11a9b commit d866a1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "grpc-interceptor"
version = "0.15.3"
version = "0.15.4"
description = "Simplifies gRPC interceptors"
license = "MIT"
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions src/grpc_interceptor/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d866a1b

Please sign in to comment.