From 18d0740f6fa7715e4120eba3c30ec95e43996076 Mon Sep 17 00:00:00 2001 From: TerryHowe Date: Tue, 18 Aug 2015 17:03:28 -0600 Subject: [PATCH] Remove spaces around data in transport debug print The debug transport curl print has spaces around --data ' data ' because of the join at the end. Also, this adds a test for it. Change-Id: I2e3ac7bc1cba463f7b7de14c50af5afda4c10234 --- openstack/tests/unit/test_transport.py | 28 ++++++++++++++++++++++++++ openstack/transport.py | 11 ++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/openstack/tests/unit/test_transport.py b/openstack/tests/unit/test_transport.py index e1aa32405..0d412a9c6 100644 --- a/openstack/tests/unit/test_transport.py +++ b/openstack/tests/unit/test_transport.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- # 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 @@ -550,3 +551,30 @@ class TestTransportRedirects(base.TestTransportBase): self.assertEqual("Wot?", xport._parse_error_response(resp)) resp.json.return_value = {"QuantumError": "Network error"} 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 '拱心石'") diff --git a/openstack/transport.py b/openstack/transport.py index ce01533ae..a7cdaaf18 100644 --- a/openstack/transport.py +++ b/openstack/transport.py @@ -363,7 +363,7 @@ class Transport(requests.Session): string_parts.append(header) if 'data' in kwargs and kwargs['data'] is not None: - string_parts.append("--data '") + string_parts.append("--data") data = kwargs['data'] # 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 # placeholder to specify that it's a blob of binary data. try: - string_parts.append(data.decode("ascii")) + data = data.decode("ascii") except UnicodeDecodeError: - string_parts.append("") - else: - string_parts.append(data) - - string_parts.append("'") + data = "" + string_parts.append("'" + data + "'") _logger.debug("REQ: %s" % " ".join(string_parts))