diff --git a/src/rfc3986/builder.py b/src/rfc3986/builder.py index 85469ed..4cb6129 100644 --- a/src/rfc3986/builder.py +++ b/src/rfc3986/builder.py @@ -114,3 +114,23 @@ class URIBuilder(object): query=self.query, fragment=self.fragment, ) + + def add_host(self, host): + """Add hostname to the URI. + + .. code-block:: python + + >>> URIBuilder().add_host('google.com') + URIBuilder(scheme=None, userinfo=None, host='google.com', + port=None, path=None, query=None, fragment=None) + + """ + return URIBuilder( + scheme=self.scheme, + userinfo=self.userinfo, + host=normalizers.normalize_host(host), + port=self.port, + path=self.path, + query=self.query, + fragment=self.fragment, + ) diff --git a/tests/test_builder.py b/tests/test_builder.py index a2e8043..b10ffc2 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -65,3 +65,15 @@ def test_add_credentials_requires_username(): """Verify one needs a username to add credentials.""" with pytest.raises(ValueError): builder.URIBuilder().add_credentials(None, None) + + +@pytest.mark.parametrize('hostname', [ + 'google.com', + 'GOOGLE.COM', + 'gOOgLe.COM', + 'goOgLE.com', +]) +def test_add_host(hostname): + """Verify we normalize hostnames in add_host.""" + uribuilder = builder.URIBuilder().add_host(hostname) + assert uribuilder.host == 'google.com'