pytest-grpc
pytest-grpc copied to clipboard
Passing metadata
Is there a way to pass metadata?
Normally I can invoke it like this:
grpc_stub.SomeGrpcMethod(SomeRequest(), metadata=some_metadata)
But I get:
TypeError: fake_handler() got an unexpected keyword argument 'metadata'
I can see fake_handler takes only request as argument.
Same problem here, any updates?
Hackish, but does the job:
from pytest_grpc.plugin import FakeContext
some_metadata = dict(a='b', c='d...')
if 'FakeChannel' in repr(grpc_stub.SomeGrpcMethod):
# pytest grpc operating in 'fake server mode' (--grpc-fake-server flag provided)
# Circumvent bug: https://github.com/kataev/pytest-grpc/issues/6
# Patch the getter function to return our metadata.
monkeypatch.setattr(FakeContext, 'invocation_metadata', lambda _self: some_metadata.items())
return grpc_stub.SomeGrpcMethod(SomeRequest())
else:
# default
return grpc_stub.SomeGrpcMethod(SomeRequest(), metadata=some_metadata.items())
Put this in your test function or a function-scoped fixture, and add monkeypatch to the argument list