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

URL::build(): Accept host-only URLs #217

Merged
merged 1 commit into from
Aug 11, 2018
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
20 changes: 16 additions & 4 deletions tests/test_url_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ def test_build_simple():
assert str(u) == 'http://127.0.0.1'


def test_build_scheme_and_host():
with pytest.raises(ValueError):
URL.build(host='127.0.0.1')

def test_build_with_scheme():
with pytest.raises(ValueError):
URL.build(scheme='http')


def test_build_with_host():
u = URL.build(host='127.0.0.1')
assert str(u) == '//127.0.0.1'
assert u == URL('//127.0.0.1')


def test_build_with_scheme_and_host():
u = URL.build(scheme='http', host='127.0.0.1')
assert str(u) == 'http://127.0.0.1'
assert u == URL('http://127.0.0.1')


def test_build_with_port():
with pytest.raises(ValueError):
URL.build(port=8000)

u = URL.build(scheme='http', host='127.0.0.1', port=8000)
assert str(u) == 'http://127.0.0.1:8000'

Expand Down
6 changes: 3 additions & 3 deletions yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ def build(cls, *, scheme='', user='', password='', host='', port=None,
encoded=False):
"""Creates and returns a new URL"""

if host and not scheme:
raise ValueError(
'Can\'t build URL with "host" but without "scheme".')
if not host and scheme:
raise ValueError(
'Can\'t build URL with "scheme" but without "host".')
if port and not host:
raise ValueError(
'Can\'t build URL with "port" but without "host".')
if query and query_string:
raise ValueError(
"Only one of \"query\" or \"query_string\" should be passed")
Expand Down