| 1 |
# Copyright 2018 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
"""Tests 'utf-8' encoded error message.""" |
| 15 |
|
| 16 |
import unittest |
| 17 |
import weakref |
| 18 |
|
| 19 |
import grpc |
| 20 |
|
| 21 |
from tests.unit import test_common |
| 22 |
from tests.unit.framework.common import test_constants |
| 23 |
|
| 24 |
_UNICODE_ERROR_MESSAGES = [ |
| 25 |
b'\xe2\x80\x9d'.decode('utf-8'), |
| 26 |
b'abc\x80\xd0\xaf'.decode('latin-1'), |
| 27 |
b'\xc3\xa9'.decode('utf-8'), |
| 28 |
] |
| 29 |
|
| 30 |
_REQUEST = b'\x00\x00\x00' |
| 31 |
_RESPONSE = b'\x00\x00\x00' |
| 32 |
|
| 33 |
_UNARY_UNARY = '/test/UnaryUnary' |
| 34 |
|
| 35 |
|
| 36 |
class _MethodHandler(grpc.RpcMethodHandler): |
| 37 |
|
| 38 |
def __init__(self, request_streaming=None, response_streaming=None): |
| 39 |
self.request_streaming = request_streaming |
| 40 |
self.response_streaming = response_streaming |
| 41 |
self.request_deserializer = None |
| 42 |
self.response_serializer = None |
| 43 |
self.unary_stream = None |
| 44 |
self.stream_unary = None |
| 45 |
self.stream_stream = None |
| 46 |
|
| 47 |
def unary_unary(self, request, servicer_context): |
| 48 |
servicer_context.set_code(grpc.StatusCode.UNKNOWN) |
| 49 |
servicer_context.set_details(request.decode('utf-8')) |
| 50 |
return _RESPONSE |
| 51 |
|
| 52 |
|
| 53 |
class _GenericHandler(grpc.GenericRpcHandler): |
| 54 |
|
| 55 |
def __init__(self, test): |
| 56 |
self._test = test |
| 57 |
|
| 58 |
def service(self, handler_call_details): |
| 59 |
return _MethodHandler() |
| 60 |
|
| 61 |
|
| 62 |
class ErrorMessageEncodingTest(unittest.TestCase): |
| 63 |
|
| 64 |
def setUp(self): |
| 65 |
self._server = test_common.test_server() |
| 66 |
self._server.add_generic_rpc_handlers( |
| 67 |
(_GenericHandler(weakref.proxy(self)),)) |
| 68 |
port = self._server.add_insecure_port('[::]:0') |
| 69 |
self._server.start() |
| 70 |
self._channel = grpc.insecure_channel('localhost:%d' % port) |
| 71 |
|
| 72 |
def tearDown(self): |
| 73 |
self._server.stop(0) |
| 74 |
self._channel.close() |
| 75 |
|
| 76 |
def testMessageEncoding(self): |
| 77 |
for message in _UNICODE_ERROR_MESSAGES: |
| 78 |
multi_callable = self._channel.unary_unary(_UNARY_UNARY) |
| 79 |
with self.assertRaises(grpc.RpcError) as cm: |
| 80 |
multi_callable(message.encode('utf-8')) |
| 81 |
|
| 82 |
self.assertEqual(cm.exception.code(), grpc.StatusCode.UNKNOWN) |
| 83 |
self.assertEqual(cm.exception.details(), message) |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == '__main__': |
| 87 |
unittest.main(verbosity=2) |