Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http/https schema correctly #242

Merged
merged 5 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _open_binary_stream(uri, mode, **kw):
#
host = kw.pop('host', None)
if host is not None:
kw['endpoint_url'] = 'http://' + host
kw['endpoint_url'] = _add_scheme_to_host(host)
return smart_open_s3.open(uri.bucket.name, uri.name, mode, **kw), uri.name
elif hasattr(uri, 'read'):
# simply pass-through if already a file-like
Expand All @@ -395,7 +395,7 @@ def _s3_open_uri(parsed_uri, mode, **kwargs):
# Get an S3 host. It is required for sigv4 operations.
host = kwargs.pop('host', None)
if host is not None:
kwargs['endpoint_url'] = 'http://' + host
kwargs['endpoint_url'] = _add_scheme_to_host(host)

return smart_open_s3.open(parsed_uri.bucket_id, parsed_uri.key_id, mode, **kwargs)

Expand Down Expand Up @@ -615,3 +615,8 @@ def _encoding_wrapper(fileobj, mode, encoding=None, errors=DEFAULT_ERRORS):
else:
decoder = codecs.getwriter(encoding)
return decoder(fileobj, errors=errors)

def _add_scheme_to_host(host):
if host.startswith('http://') or host.startswith('https://'):
return host
return 'http://' + host
25 changes: 25 additions & 0 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,31 @@ def test_write_text_gzip(self):
actual = fin.read()
self.assertEqual(text, actual)

class HostNameTest(unittest.TestCase):

def test_host_name_with_http(self):
host = 'http://a.com/b'
expected = 'http://a.com/b'
res = smart_open_lib._add_scheme_to_host(host)
self.assertEqual(expected, res)

def test_host_name_without_http(self):
host = 'a.com/b'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of these should be a separate test case. Putting multiple checks into a single test case is bad practice, because one failure can mask subsequent ones.

expected = 'http://a.com/b'
res = smart_open_lib._add_scheme_to_host(host)
self.assertEqual(expected, res)

def test_host_name_with_https(self):
host = 'https://a.com/b'
expected = 'https://a.com/b'
res = smart_open_lib._add_scheme_to_host(host)
self.assertEqual(expected, res)

def test_host_name_without_http_prefix(self):
host = 'httpa.com/b'
expected = 'http://httpa.com/b'
res = smart_open_lib._add_scheme_to_host(host)
self.assertEqual(expected, res)

if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
Expand Down