Skip to content

Commit a406fcb

Browse files
committed
test: add ensure_ascii setting to AuthServiceProxy
Add a setting ensure_ascii to AuthServiceProxy. This setting, defaulting to True (backwards compatible), is passed through to json.dumps. If set to False, non-ASCII characters >0x80 are not escaped. This is useful for testing server input processing, as well as slightly more bandwidth friendly in case of heavy unicode usage.
1 parent 6315152 commit a406fcb

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

qa/rpc-tests/test_framework/authproxy.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ def EncodeDecimal(o):
6767
class AuthServiceProxy(object):
6868
__id_count = 0
6969

70-
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
70+
# ensure_ascii: escape unicode as \uXXXX, passed to json.dumps
71+
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None, ensure_ascii=True):
7172
self.__service_url = service_url
7273
self._service_name = service_name
74+
self.ensure_ascii = ensure_ascii # can be toggled on the fly by tests
7375
self.__url = urlparse.urlparse(service_url)
7476
if self.__url.port is None:
7577
port = 80
@@ -134,12 +136,12 @@ def __call__(self, *args):
134136
AuthServiceProxy.__id_count += 1
135137

136138
log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self._service_name,
137-
json.dumps(args, default=EncodeDecimal)))
139+
json.dumps(args, default=EncodeDecimal, ensure_ascii=self.ensure_ascii)))
138140
postdata = json.dumps({'version': '1.1',
139141
'method': self._service_name,
140142
'params': args,
141-
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
142-
response = self._request('POST', self.__url.path, postdata)
143+
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal, ensure_ascii=self.ensure_ascii)
144+
response = self._request('POST', self.__url.path, postdata.encode('utf-8'))
143145
if response['error'] is not None:
144146
raise JSONRPCException(response['error'])
145147
elif 'result' not in response:
@@ -149,9 +151,9 @@ def __call__(self, *args):
149151
return response['result']
150152

151153
def _batch(self, rpc_call_list):
152-
postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal)
154+
postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal, ensure_ascii=self.ensure_ascii)
153155
log.debug("--> "+postdata)
154-
return self._request('POST', self.__url.path, postdata)
156+
return self._request('POST', self.__url.path, postdata.encode('utf-8'))
155157

156158
def _get_response(self):
157159
http_response = self.__conn.getresponse()
@@ -167,7 +169,7 @@ def _get_response(self):
167169
responsedata = http_response.read().decode('utf8')
168170
response = json.loads(responsedata, parse_float=decimal.Decimal)
169171
if "error" in response and response["error"] is None:
170-
log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal)))
172+
log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal, ensure_ascii=self.ensure_ascii)))
171173
else:
172174
log.debug("<-- "+responsedata)
173175
return response

0 commit comments

Comments
 (0)