9
9
)
10
10
import grpclib
11
11
import grpclib .metadata
12
+ import grpclib .server
12
13
from grpclib .testing import ChannelFor
13
14
import pytest
14
15
from betterproto .grpc .util .async_channel import AsyncChannel
@@ -32,12 +33,59 @@ def server_side_test(stream):
32
33
return server_side_test
33
34
34
35
36
+ @pytest .fixture
37
+ def handler_trailer_only_unauthenticated ():
38
+ async def handler (stream : grpclib .server .Stream ):
39
+ await stream .recv_message ()
40
+ await stream .send_initial_metadata ()
41
+ await stream .send_trailing_metadata (status = grpclib .Status .UNAUTHENTICATED )
42
+
43
+ return handler
44
+
45
+
35
46
@pytest .mark .asyncio
36
47
async def test_simple_service_call ():
37
48
async with ChannelFor ([ThingService ()]) as channel :
38
49
await _test_client (ThingServiceClient (channel ))
39
50
40
51
52
+ @pytest .mark .asyncio
53
+ async def test_trailer_only_error_unary_unary (
54
+ mocker , handler_trailer_only_unauthenticated
55
+ ):
56
+ service = ThingService ()
57
+ mocker .patch .object (
58
+ service ,
59
+ "do_thing" ,
60
+ side_effect = handler_trailer_only_unauthenticated ,
61
+ autospec = True ,
62
+ )
63
+ async with ChannelFor ([service ]) as channel :
64
+ with pytest .raises (grpclib .exceptions .GRPCError ) as e :
65
+ await ThingServiceClient (channel ).do_thing (name = "something" )
66
+ assert e .value .status == grpclib .Status .UNAUTHENTICATED
67
+
68
+
69
+ @pytest .mark .asyncio
70
+ async def test_trailer_only_error_stream_unary (
71
+ mocker , handler_trailer_only_unauthenticated
72
+ ):
73
+ service = ThingService ()
74
+ mocker .patch .object (
75
+ service ,
76
+ "do_many_things" ,
77
+ side_effect = handler_trailer_only_unauthenticated ,
78
+ autospec = True ,
79
+ )
80
+ async with ChannelFor ([service ]) as channel :
81
+ with pytest .raises (grpclib .exceptions .GRPCError ) as e :
82
+ await ThingServiceClient (channel ).do_many_things (
83
+ request_iterator = [DoThingRequest (name = "something" )]
84
+ )
85
+ await _test_client (ThingServiceClient (channel ))
86
+ assert e .value .status == grpclib .Status .UNAUTHENTICATED
87
+
88
+
41
89
@pytest .mark .asyncio
42
90
@pytest .mark .skipif (
43
91
sys .version_info < (3 , 8 ), reason = "async mock spy does works for python3.8+"
0 commit comments