From 407f6956098a4a46aae830d7c34a4c37c4e68936 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Fri, 4 Jul 2014 20:48:22 -0500 Subject: [PATCH] Don't join None values in utils.urljoin This was seen on resources that don't set an id attribute, ending up with request URLs such as /v1/blargablarga/None instead of /v1/blargablarga/. Change-Id: Ib6fa52c6d4b76f6c9d30418277fdd927afb5d102 --- openstack/tests/test_resource.py | 15 +++++++++++++++ openstack/tests/test_utils.py | 32 ++++++++++++++++++++++++++++++++ openstack/utils.py | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 openstack/tests/test_utils.py diff --git a/openstack/tests/test_resource.py b/openstack/tests/test_resource.py index f72e1062c..dd373a208 100644 --- a/openstack/tests/test_resource.py +++ b/openstack/tests/test_resource.py @@ -60,6 +60,21 @@ class ResourceTests(base.TestTransportBase): self.auth = fakes.FakeAuthenticator() self.session = session.Session(self.transport, self.auth) + @httpretty.activate + def test_empty_id(self): + self.stub_url(httpretty.GET, path=[fake_path], json=fake_body) + obj = FakeResource.new() + obj.get(self.session) + + self.assertEqual(fake_id, obj.id) + self.assertEqual(fake_name, obj['name']) + self.assertEqual(fake_attr1, obj['attr1']) + self.assertEqual(fake_attr2, obj['attr2']) + + self.assertEqual(fake_name, obj.name) + self.assertEqual(fake_attr1, obj.first) + self.assertEqual(fake_attr2, obj.second) + @httpretty.activate def test_create(self): self.stub_url(httpretty.POST, path=fake_path, json=fake_body) diff --git a/openstack/tests/test_utils.py b/openstack/tests/test_utils.py new file mode 100644 index 000000000..4031d9c8e --- /dev/null +++ b/openstack/tests/test_utils.py @@ -0,0 +1,32 @@ +# 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. + +import unittest + +from openstack import utils + + +class Test_urljoin(unittest.TestCase): + + def test_strings(self): + root = "http://www.example.com" + leaves = "foo", "bar" + + result = utils.urljoin(root, *leaves) + self.assertEqual(result, "http://www.example.com/foo/bar") + + def test_with_none(self): + root = "http://www.example.com" + leaves = "foo", None + + result = utils.urljoin(root, *leaves) + self.assertEqual(result, "http://www.example.com/foo/") diff --git a/openstack/utils.py b/openstack/utils.py index 26f1fa12d..ae7342e0b 100644 --- a/openstack/utils.py +++ b/openstack/utils.py @@ -18,4 +18,4 @@ def urljoin(*args): like /path this should be joined to http://host/path as it is an anchored link. We generally won't care about that in client. """ - return '/'.join(str(a).strip('/') for a in args) + return '/'.join(str(a or '').strip('/') for a in args)