Skip to content
Draft
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
8 changes: 5 additions & 3 deletions sqlalchemy_libsql/libsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def _build_connection_url(url, query, secure):
else:
scheme = "http"

host = f"[{url.host}]" if ":" in url.host else url.host

if url.username and url.password:
netloc = f"{url.username}:{url.password}@{url.host}"
netloc = f"{url.username}:{url.password}@{host}"
elif url.username:
netloc = f"{url.username}@{url.host}"
netloc = f"{url.username}@{host}"
else:
netloc = url.host
netloc = host

if url.port:
netloc += f":{url.port}"
Expand Down
22 changes: 22 additions & 0 deletions tests/test_connection_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
from sqlalchemy import create_engine
from urllib.parse import urlsplit


@pytest.mark.parametrize("host", ["::1", "2001:db8::1", "127.0.0.1", "example.com"])
@pytest.mark.parametrize("secure", [False, True])
def test_remote_connection_url(host, secure):
authority = f"[{host}]" if ":" in host else host
engine = create_engine(
f"sqlite+libsql://{authority}:8080/database?secure={str(secure).lower()}"
)
try:
args, options = engine.dialect.create_connect_args(engine.url)
scheme = "https" if secure else "http"
assert args == [f"{scheme}://{authority}:8080/database"]
parsed = urlsplit(args[0])
assert parsed.hostname == host
assert parsed.port == 8080
assert options["uri"] is True
finally:
engine.dispose()