pytest-grpc icon indicating copy to clipboard operation
pytest-grpc copied to clipboard

Passing metadata

Open Arrekin opened this issue 6 years ago • 2 comments

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.

Arrekin avatar Jun 07 '19 07:06 Arrekin

Same problem here, any updates?

hellocoldworld avatar Dec 02 '19 16:12 hellocoldworld

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

coyotwill avatar Dec 02 '20 03:12 coyotwill