Skip to content

Commit 407f695

Browse files
author
Brian Curtin
committed
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
1 parent fe3e996 commit 407f695

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

openstack/tests/test_resource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def setUp(self):
6060
self.auth = fakes.FakeAuthenticator()
6161
self.session = session.Session(self.transport, self.auth)
6262

63+
@httpretty.activate
64+
def test_empty_id(self):
65+
self.stub_url(httpretty.GET, path=[fake_path], json=fake_body)
66+
obj = FakeResource.new()
67+
obj.get(self.session)
68+
69+
self.assertEqual(fake_id, obj.id)
70+
self.assertEqual(fake_name, obj['name'])
71+
self.assertEqual(fake_attr1, obj['attr1'])
72+
self.assertEqual(fake_attr2, obj['attr2'])
73+
74+
self.assertEqual(fake_name, obj.name)
75+
self.assertEqual(fake_attr1, obj.first)
76+
self.assertEqual(fake_attr2, obj.second)
77+
6378
@httpretty.activate
6479
def test_create(self):
6580
self.stub_url(httpretty.POST, path=fake_path, json=fake_body)

openstack/tests/test_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import unittest
14+
15+
from openstack import utils
16+
17+
18+
class Test_urljoin(unittest.TestCase):
19+
20+
def test_strings(self):
21+
root = "http://www.example.com"
22+
leaves = "foo", "bar"
23+
24+
result = utils.urljoin(root, *leaves)
25+
self.assertEqual(result, "http://www.example.com/foo/bar")
26+
27+
def test_with_none(self):
28+
root = "http://www.example.com"
29+
leaves = "foo", None
30+
31+
result = utils.urljoin(root, *leaves)
32+
self.assertEqual(result, "http://www.example.com/foo/")

openstack/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def urljoin(*args):
1818
like /path this should be joined to http://host/path as it is an anchored
1919
link. We generally won't care about that in client.
2020
"""
21-
return '/'.join(str(a).strip('/') for a in args)
21+
return '/'.join(str(a or '').strip('/') for a in args)

0 commit comments

Comments
 (0)