Skip to content

Commit 47d1d99

Browse files
committed
fix: Strip trailing slash for request url
1 parent 5354c55 commit 47d1d99

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(self,
6969
service_url: str = None,
7070
authenticator: Authenticator = None,
7171
disable_ssl_verification: bool = False) -> None:
72-
self.service_url = service_url
72+
self.set_service_url(service_url)
7373
self.http_config = {}
7474
self.jar = CookieJar()
7575
self.authenticator = authenticator
@@ -164,7 +164,7 @@ def set_service_url(self, service_url: str) -> None:
164164
'The service url shouldn\'t start or end with curly brackets or quotes. '
165165
'Be sure to remove any {} and \" characters surrounding your service url'
166166
)
167-
self.service_url = service_url
167+
self.service_url = service_url.rstrip('/') # remove trailing slash
168168

169169
def get_authenticator(self) -> Authenticator:
170170
"""Get the authenticator currently used by the service.
@@ -272,7 +272,7 @@ def prepare_request(self,
272272
# validate the service url is set
273273
if not self.service_url:
274274
raise ValueError('The service_url is required')
275-
request['url'] = self.service_url + url
275+
request['url'] = self.service_url + url.rstrip('/') # strip trailing slash
276276

277277
headers = remove_null_values(headers) if headers else {}
278278
headers = cleanup_values(headers)

test/test_base_service.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,37 @@ def test_json():
518518
req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world'})
519519
assert req.get('data') == "{\"hello\": \"world\"}"
520520

521+
def test_trailing_slash():
522+
service = AnyServiceV1('2018-11-20', service_url='https://trailingSlash.com/', authenticator=NoAuthAuthenticator())
523+
assert service.service_url == 'https://trailingSlash.com'
524+
service.set_service_url('https://trailingSlash.com/')
525+
assert service.service_url == 'https://trailingSlash.com'
526+
req = service.prepare_request('POST',
527+
url='/trailingSlashPath/',
528+
headers={'X-opt-out': True},
529+
data={'hello': 'world'})
530+
assert req.get('url') == 'https://trailingSlash.com/trailingSlashPath'
531+
532+
service = AnyServiceV1('2018-11-20', service_url='https://trailingSlash.com/', authenticator=NoAuthAuthenticator())
533+
assert service.service_url == 'https://trailingSlash.com'
534+
service.set_service_url('https://trailingSlash.com/')
535+
assert service.service_url == 'https://trailingSlash.com'
536+
req = service.prepare_request('POST',
537+
url='/',
538+
headers={'X-opt-out': True},
539+
data={'hello': 'world'})
540+
assert req.get('url') == 'https://trailingSlash.com'
541+
542+
service = AnyServiceV1('2018-11-20', service_url='/', authenticator=NoAuthAuthenticator())
543+
assert service.service_url == ''
544+
service.set_service_url('/')
545+
assert service.service_url == ''
546+
with pytest.raises(ValueError): # ValueError thrown because service_url is '' and falsey
547+
req = service.prepare_request('POST',
548+
url='/',
549+
headers={'X-opt-out': True},
550+
data={'hello': 'world'})
551+
521552
def test_service_url_not_set():
522553
service = BaseService(service_url='', authenticator=NoAuthAuthenticator())
523554
with pytest.raises(ValueError) as err:

0 commit comments

Comments
 (0)