Skip to content

Commit

Permalink
fix: Strip trailing slash for request url
Browse files Browse the repository at this point in the history
  • Loading branch information
rmkeezer committed May 22, 2020
1 parent 5354c55 commit 47d1d99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self,
service_url: str = None,
authenticator: Authenticator = None,
disable_ssl_verification: bool = False) -> None:
self.service_url = service_url
self.set_service_url(service_url)
self.http_config = {}
self.jar = CookieJar()
self.authenticator = authenticator
Expand Down Expand Up @@ -164,7 +164,7 @@ def set_service_url(self, service_url: str) -> None:
'The service url shouldn\'t start or end with curly brackets or quotes. '
'Be sure to remove any {} and \" characters surrounding your service url'
)
self.service_url = service_url
self.service_url = service_url.rstrip('/') # remove trailing slash

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

headers = remove_null_values(headers) if headers else {}
headers = cleanup_values(headers)
Expand Down
31 changes: 31 additions & 0 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,37 @@ def test_json():
req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world'})
assert req.get('data') == "{\"hello\": \"world\"}"

def test_trailing_slash():
service = AnyServiceV1('2018-11-20', service_url='https://trailingSlash.com/', authenticator=NoAuthAuthenticator())
assert service.service_url == 'https://trailingSlash.com'
service.set_service_url('https://trailingSlash.com/')
assert service.service_url == 'https://trailingSlash.com'
req = service.prepare_request('POST',
url='/trailingSlashPath/',
headers={'X-opt-out': True},
data={'hello': 'world'})
assert req.get('url') == 'https://trailingSlash.com/trailingSlashPath'

service = AnyServiceV1('2018-11-20', service_url='https://trailingSlash.com/', authenticator=NoAuthAuthenticator())
assert service.service_url == 'https://trailingSlash.com'
service.set_service_url('https://trailingSlash.com/')
assert service.service_url == 'https://trailingSlash.com'
req = service.prepare_request('POST',
url='/',
headers={'X-opt-out': True},
data={'hello': 'world'})
assert req.get('url') == 'https://trailingSlash.com'

service = AnyServiceV1('2018-11-20', service_url='/', authenticator=NoAuthAuthenticator())
assert service.service_url == ''
service.set_service_url('/')
assert service.service_url == ''
with pytest.raises(ValueError): # ValueError thrown because service_url is '' and falsey
req = service.prepare_request('POST',
url='/',
headers={'X-opt-out': True},
data={'hello': 'world'})

def test_service_url_not_set():
service = BaseService(service_url='', authenticator=NoAuthAuthenticator())
with pytest.raises(ValueError) as err:
Expand Down

0 comments on commit 47d1d99

Please sign in to comment.