async def test_deadline(loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), (':path', '/package.Service/Method'), ('te', 'trailers'), ('content-type', 'application/grpc'), ('grpc-timeout', '10m'), ] async def _method(stream_): await asyncio.sleep(1) methods = {'/package.Service/Method': Handler( _method, Cardinality.UNARY_UNARY, DummyRequest, DummyReply, )} task = loop.create_task( request_handler(methods, stream, headers, ProtoCodec(), release_stream) ) await asyncio.wait_for(task, 0.1, loop=loop) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '200'), ('grpc-status', '4'), # DEADLINE_EXCEEDED ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_invalid_method(loop): stream = H2StreamStub(loop=loop) headers = [(':method', 'GET')] await request_handler({}, stream, headers, ProtoCodec(), release_stream) assert stream.__events__ == [ SendHeaders(headers=[(':status', '405')], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_invalid_method(): stream = H2StreamStub() headers = [(':method', 'GET')] await call_handler({}, stream, headers) assert stream.__events__ == [ SendHeaders(headers=[(':status', '405')], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_missing_content_type(loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), ] await request_handler({}, stream, headers, ProtoCodec(), release_stream) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '415'), ('grpc-status', '2'), # UNKNOWN ('grpc-message', 'Missing content-type header'), ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_missing_te_header(loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), ('content-type', 'application/grpc'), ] await request_handler({}, stream, headers, ProtoCodec(), release_stream) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '400'), ('grpc-status', '2'), # UNKNOWN ('grpc-message', 'Required "te: trailers" header is missing'), ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_invalid_content_type(content_type, loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), ('content-type', content_type), ] await call_handler({}, stream, headers) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '415'), ('grpc-status', '2'), # UNKNOWN ('grpc-message', 'Unacceptable content-type header'), ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_missing_method(loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), (':path', '/missing.Service/MissingMethod'), ('te', 'trailers'), ('content-type', 'application/grpc'), ] await request_handler({}, stream, headers, ProtoCodec(), release_stream) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '200'), ('grpc-status', '12'), # UNIMPLEMENTED ('grpc-message', 'Method not found'), ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_invalid_grpc_timeout(loop): stream = H2StreamStub(loop=loop) headers = [ (':method', 'POST'), (':path', '/package.Service/Method'), ('te', 'trailers'), ('content-type', 'application/grpc'), ('grpc-timeout', 'invalid'), ] methods = {'/package.Service/Method': object()} await call_handler(methods, stream, headers) assert stream.__events__ == [ SendHeaders(headers=[ (':status', '200'), ('grpc-status', '2'), # UNKNOWN ('grpc-message', 'Invalid grpc-timeout header'), ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ]
async def test_deadline(loop, caplog, handler, level, msg, exc_type, exc_text): caplog.set_level(logging.INFO) stream = H2StreamStub() headers = [ (':method', 'POST'), (':path', '/package.Service/Method'), ('te', 'trailers'), ('content-type', 'application/grpc'), ('grpc-timeout', '10m'), ] methods = { '/package.Service/Method': Handler( handler, Cardinality.UNARY_UNARY, DummyRequest, DummyReply, ) } task = loop.create_task(call_handler(methods, stream, headers)) await asyncio.wait_for(task, 0.1) assert stream.__events__ == [ SendHeaders( headers=[ (':status', '200'), ('content-type', 'application/grpc+proto'), ('grpc-status', '4'), # DEADLINE_EXCEEDED ], end_stream=True), Reset(ErrorCodes.NO_ERROR), ] record, = caplog.records assert record.name == 'grpclib.server' assert record.levelname == level assert msg in record.getMessage() if exc_type is not None: assert record.exc_info[0] is exc_type if exc_text is not None: assert exc_text in record.exc_text