def test_single_noop_interceptor(self): calls = [] def interceptor(request, context, details, cont): calls.append((request, details)) return cont(request, context, details) s = TestServicer() app = self.make_test_server_app(s, [interceptor]) resp = self.call_echo(app, 123, headers={'Authorization': 'x'}) self.assertEqual( resp, {u'response': [u'hello!', u'123']}, ) self.assertEqual(s.echoed.r.m, 123) # Interceptor called and saw relevant metadata. self.assertEqual(len(calls), 1) req, details = calls[0] self.assertEqual(req, test_pb2.EchoRequest(r=test_pb2.GiveRequest(m=123))) self.assertEqual(details.method, 'test.Test.Echo') self.assertEqual( dict(details.invocation_metadata)['authorization'], 'x')
def test_context(self): calls = [] def rpc_callback(_request, context): calls.append({ 'peer': context.peer(), 'is_active': context.is_active(), 'time_remaining': context.time_remaining(), }) self.service.give_callback = rpc_callback headers = self.make_headers(encoding.Encoding.BINARY) req = test_pb2.GiveRequest(m=3333) raw_resp = self.app.post( '/prpc/test.Test/Give', req.SerializeToString(), headers, ).body self.assertEqual(len(raw_resp), 0) self.assertEqual(calls, [ { 'is_active': True, 'peer': 'ipv6:[::ffff:127.0.0.1]', 'time_remaining': None, }, ])
def test_bad_app(self): """Make sure the server handles a bad servicer implementation.""" req = test_pb2.GiveRequest(m=825800) resp = self.bad_app.post( '/prpc/test.Test/Give', req.SerializeToString(), self.make_headers(encoding.Encoding.BINARY), expect_errors=True, ) self.assertEqual(resp.status_int, httplib.INTERNAL_SERVER_ERROR) self.check_headers(resp.headers, server.StatusCode.INTERNAL) req = empty_pb2.Empty() resp = self.bad_app.post( '/prpc/test.Test/Take', req.SerializeToString(), self.make_headers(encoding.Encoding.BINARY), expect_errors=True, ) self.assertEqual(resp.status_int, httplib.INTERNAL_SERVER_ERROR) self.check_headers(resp.headers, server.StatusCode.INTERNAL) req = test_pb2.EchoRequest() resp = self.bad_app.post( '/prpc/test.Test/Echo', req.SerializeToString(), self.make_headers(encoding.Encoding.BINARY), expect_errors=True, ) self.assertEqual(resp.status_int, httplib.INTERNAL_SERVER_ERROR) self.check_headers(resp.headers, server.StatusCode.INTERNAL)
def test_request(self): client_metadata = {'Jennys-Number-Bin': '867-5309'} server_metadata = {} # To be written to. with self.mocked_request_async(): req = test_pb2.GiveRequest(m=1) self.make_test_client().GiveAsync( req, metadata=client_metadata, response_metadata=server_metadata).get_result() net.request_async.assert_called_with( url='https://example.com/prpc/test.Test/Give', method='POST', payload=req.SerializeToString(), headers={ 'Content-Type': 'application/prpc; encoding=binary', 'Accept': 'application/prpc; encoding=binary', 'X-Prpc-Timeout': '10S', 'Jennys-Number-Bin': 'ODY3LTUzMDk=', # '867-5309' }, scopes=None, service_account_key=None, delegation_token=None, deadline=10, max_attempts=4, response_headers=server_metadata, project_id=None, ) self.assertEqual(server_metadata['Some-Bytes-Bin'], '1234')
def test_bad_method(self): """Make sure the server handles an unknown method.""" req = test_pb2.GiveRequest(m=825800) resp = self.app.post( '/prpc/test.Test/IDontExist', req.SerializeToString(), self.make_headers(encoding.Encoding.BINARY), expect_errors=True, ) self.assertEqual(resp.status_int, httplib.NOT_IMPLEMENTED) self.check_headers(resp.headers, server.StatusCode.UNIMPLEMENTED)
def test_bad_headers(self): """Make sure the server gives a reasonable response for bad headers.""" req = test_pb2.GiveRequest(m=825800) resp = self.app.post( '/prpc/test.Test/Give', req.SerializeToString(), {}, expect_errors=True, ) self.assertEqual(resp.status_int, httplib.BAD_REQUEST) self.check_headers(resp.headers, server.StatusCode.INVALID_ARGUMENT)
def test_request(self): with self.mocked_request_async(): req = test_pb2.GiveRequest(m=1) self.make_test_client().GiveAsync(req).get_result() net.request_async.assert_called_with( url='https://example.com/prpc/test.Test/Give', method='POST', payload=req.SerializeToString(), headers={ 'Content-Type': 'application/prpc; encoding=binary', 'Accept': 'application/prpc; encoding=binary', 'X-Prpc-Timeout': '10S', }, scopes=None, service_account_key=None, delegation_token=None, deadline=10, max_attempts=4, )
def test_servicer_persistence(self): """Basic test which ensures the servicer state persists.""" headers = self.make_headers(encoding.Encoding.BINARY) req = test_pb2.GiveRequest(m=3333) raw_resp = self.app.post( '/prpc/test.Test/Give', req.SerializeToString(), headers, ).body self.assertEqual(len(raw_resp), 0) req = empty_pb2.Empty() raw_resp = self.app.post( '/prpc/test.Test/Take', req.SerializeToString(), headers, ).body resp = test_pb2.TakeResponse() test_pb2.TakeResponse.ParseFromString(resp, raw_resp) self.assertEqual(resp.k, 3333)
def give_creds(self, creds): self.make_test_client().Give(test_pb2.GiveRequest(), credentials=creds)
def test_request_timeout(self): with self.mocked_request_async(): self.make_test_client().Give(test_pb2.GiveRequest(), timeout=20) _, kwargs = net.request_async.call_args self.assertEqual(kwargs['deadline'], 20) self.assertEqual(kwargs['headers']['X-Prpc-Timeout'], '20S')