aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio_tests/tests/unit/_error_message_encoding_test.py
blob: 81de1dae1d168e64a8c5a6c297bb890786d5436b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests 'utf-8' encoded error message."""

import unittest
import weakref

import grpc

from tests.unit import test_common
from tests.unit.framework.common import test_constants

_UNICODE_ERROR_MESSAGES = [
    b'\xe2\x80\x9d'.decode('utf-8'),
    b'abc\x80\xd0\xaf'.decode('latin-1'),
    b'\xc3\xa9'.decode('utf-8'),
]

_REQUEST = b'\x00\x00\x00'
_RESPONSE = b'\x00\x00\x00'

_UNARY_UNARY = '/test/UnaryUnary'


class _MethodHandler(grpc.RpcMethodHandler):

    def __init__(self, request_streaming=None, response_streaming=None):
        self.request_streaming = request_streaming
        self.response_streaming = response_streaming
        self.request_deserializer = None
        self.response_serializer = None
        self.unary_stream = None
        self.stream_unary = None
        self.stream_stream = None

    def unary_unary(self, request, servicer_context):
        servicer_context.set_code(grpc.StatusCode.UNKNOWN)
        servicer_context.set_details(request.decode('utf-8'))
        return _RESPONSE


class _GenericHandler(grpc.GenericRpcHandler):

    def __init__(self, test):
        self._test = test

    def service(self, handler_call_details):
        return _MethodHandler()


class ErrorMessageEncodingTest(unittest.TestCase):

    def setUp(self):
        self._server = test_common.test_server()
        self._server.add_generic_rpc_handlers((_GenericHandler(
            weakref.proxy(self)),))
        port = self._server.add_insecure_port('[::]:0')
        self._server.start()
        self._channel = grpc.insecure_channel('localhost:%d' % port)

    def tearDown(self):
        self._server.stop(0)
        self._channel.close()

    def testMessageEncoding(self):
        for message in _UNICODE_ERROR_MESSAGES:
            multi_callable = self._channel.unary_unary(_UNARY_UNARY)
            with self.assertRaises(grpc.RpcError) as cm:
                multi_callable(message.encode('utf-8'))

            self.assertEqual(cm.exception.code(), grpc.StatusCode.UNKNOWN)
            self.assertEqual(cm.exception.details(), message)


if __name__ == '__main__':
    unittest.main(verbosity=2)