Merge "Remove spaces around data in transport debug print"

This commit is contained in:
Jenkins 2015-08-19 16:18:59 +00:00 committed by Gerrit Code Review
commit cdec1281ad
2 changed files with 32 additions and 7 deletions

View File

@ -1,3 +1,4 @@
# -*- encoding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
# a copy of the License at # a copy of the License at
@ -573,3 +574,30 @@ class TestTransportRedirects(TestTransportBase):
self.assertEqual("Wot?", xport._parse_error_response(resp)) self.assertEqual("Wot?", xport._parse_error_response(resp))
resp.json.return_value = {"QuantumError": "Network error"} resp.json.return_value = {"QuantumError": "Network error"}
self.assertEqual("Network error", xport._parse_error_response(resp)) self.assertEqual("Network error", xport._parse_error_response(resp))
class TestLogging(base.TestCase):
METHOD = 'PUT'
URL = 'http://example.com/'
def setUp(self):
super(TestLogging, self).setUp()
self.xport = transport.Transport()
mock_logger = mock.Mock()
mock_logger.isEnabledFor = mock.Mock()
mock_logger.isEnabledFor.return_value = True
self.mock_debug = mock.Mock()
mock_logger.debug = self.mock_debug
transport._logger = mock_logger
self.expected = (u"REQ: curl -i -X '%s' '%s'" %
(self.METHOD, self.URL))
def test_data(self):
self.xport._log_request(self.METHOD, self.URL, data="payload",
headers={})
self.mock_debug.assert_called_with(self.expected + " --data 'payload'")
def test_unicode(self):
self.xport._log_request(self.METHOD, self.URL, data=u'拱心石',
headers={})
self.mock_debug.assert_called_with(self.expected + u" --data '拱心石'")

View File

@ -363,7 +363,7 @@ class Transport(requests.Session):
string_parts.append(header) string_parts.append(header)
if 'data' in kwargs and kwargs['data'] is not None: if 'data' in kwargs and kwargs['data'] is not None:
string_parts.append("--data '") string_parts.append("--data")
data = kwargs['data'] data = kwargs['data']
# Only log text strings and byte strings that can be decoded # Only log text strings and byte strings that can be decoded
@ -376,13 +376,10 @@ class Transport(requests.Session):
# to ascii. If it works, log it, otherwise put in a # to ascii. If it works, log it, otherwise put in a
# placeholder to specify that it's a blob of binary data. # placeholder to specify that it's a blob of binary data.
try: try:
string_parts.append(data.decode("ascii")) data = data.decode("ascii")
except UnicodeDecodeError: except UnicodeDecodeError:
string_parts.append("<binary data>") data = "<binary data>"
else: string_parts.append("'" + data + "'")
string_parts.append(data)
string_parts.append("'")
_logger.debug("REQ: %s" % " ".join(string_parts)) _logger.debug("REQ: %s" % " ".join(string_parts))