Ensure url sent to proxy don't have redundant /

When a proxy is set, the url requested to the proxy have a double // after
the domain, but for ceilometer this kind of url is not valid.

So, this patch ensures the url have only one /

Closes-bug: #1274981

Change-Id: Id6fc5cf7ab7a3866bc23af7102785a9cede593fe
This commit is contained in:
Mehdi Abaakouk
2014-01-31 17:16:30 +01:00
committed by Gerrit Code Review
parent 0eed59f4b5
commit 85c80f0fc5
2 changed files with 28 additions and 1 deletions

View File

@@ -143,7 +143,8 @@ class HTTPClient(object):
try:
if self.proxy_url:
conn_url = self.endpoint + self._make_connection_url(url)
conn_url = (self.endpoint.rstrip('/') +
self._make_connection_url(url))
else:
conn_url = self._make_connection_url(url)
conn.request(method, conn_url, **kwargs)

View File

@@ -13,6 +13,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import mock
from ceilometerclient.common import http
from ceilometerclient.tests import utils
@@ -44,6 +47,29 @@ class HttpClientTest(utils.BaseTestCase):
client = http.HTTPClient(self.url)
self.assertIsNotNone(client.get_connection())
def test_url_generation_with_proxy(self):
client = http.HTTPClient(self.url)
client.proxy_url = "http://localhost:3128/"
conn = mock.MagicMock()
with contextlib.nested(
mock.patch.object(client, 'get_connection'),
mock.patch.object(client, 'auth_token')
) as (get_conn, auth_token):
conn.request.side_effect = Exception("stop")
get_conn.return_value = conn
auth_token.return_value = "token"
try:
client._http_request('/v1/resources', 'GET')
except Exception:
pass
conn.request.assert_called_once_with('GET', (self.url.rstrip('/') +
'/v1/resources'),
headers=mock.ANY)
class HttpsClientTest(HttpClientTest):
url = 'https://localhost'
class HttpEndingSlashClientTest(HttpClientTest):
url = 'http://localhost/'