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

fix get_client #32

Merged
merged 4 commits into from
Jan 5, 2023
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
6 changes: 3 additions & 3 deletions dvc_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ async def get_client(
# data blobs. We remove the total timeout, and only limit the time
# that is spent when connecting to the remote server and waiting
# for new data portions.
connect_timeout = kwargs.get("connect_timeout", self.REQUEST_TIMEOUT)
connect_timeout = kwargs.pop("connect_timeout", self.REQUEST_TIMEOUT)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use pop everywhere to avoid passing kwargs to client

kwargs["timeout"] = aiohttp.ClientTimeout(
total=None,
connect=connect_timeout,
sock_connect=connect_timeout,
sock_read=kwargs.get("read_timeout", self.REQUEST_TIMEOUT),
sock_read=kwargs.pop("read_timeout", self.REQUEST_TIMEOUT),
)

kwargs["connector"] = aiohttp.TCPConnector(
# Force cleanup of closed SSL transports.
# See https://github.com/iterative/dvc/issues/7414
enable_cleanup_closed=True,
ssl=make_context(kwargs.get("ssl_verify")),
ssl=make_context(kwargs.pop("ssl_verify", None)),
)

return ReadOnlyRetryClient(**kwargs)
Expand Down
96 changes: 96 additions & 0 deletions dvc_http/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import ssl

from dvc.fs import HTTPFileSystem
from fsspec.asyn import get_loop as get_fsspec_loop
from fsspec.asyn import sync


def test_public_auth_method():
config = {
"url": "http://example.com/",
"path": "file.html",
"user": "",
"password": "",
}

fs = HTTPFileSystem(**config)

assert "auth" not in fs.fs_args["client_kwargs"]
assert "headers" not in fs.fs_args


def test_basic_auth_method():
user = "username"
password = "password"
config = {
"url": "http://example.com/",
"path": "file.html",
"auth": "basic",
"user": user,
"password": password,
}

fs = HTTPFileSystem(**config)

assert fs.fs_args["client_kwargs"]["auth"].login == user
assert fs.fs_args["client_kwargs"]["auth"].password == password


def test_custom_auth_method():
header = "Custom-Header"
password = "password"
config = {
"url": "http://example.com/",
"path": "file.html",
"auth": "custom",
"custom_auth_header": header,
"password": password,
}

fs = HTTPFileSystem(**config)

headers = fs.fs_args["headers"]
assert header in headers
assert headers[header] == password


def test_ssl_verify_disable():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified test_ssl_verify_{disable,custom_cert} to account for changes in #28

config = {
"url": "http://example.com/",
"path": "file.html",
"ssl_verify": False,
}

fs = HTTPFileSystem(**config)
session = sync(get_fsspec_loop(), fs.fs.set_session)

# pylint: disable-next=protected-access
assert not session._client._connector._ssl


def test_ssl_verify_custom_cert(mocker):
mocker.patch("ssl.SSLContext.load_verify_locations")
config = {
"url": "http://example.com/",
"path": "file.html",
"ssl_verify": "/path/to/custom/cabundle.pem",
}

fs = HTTPFileSystem(**config)
session = sync(get_fsspec_loop(), fs.fs.set_session)

# pylint: disable-next=protected-access
assert isinstance(session._client.connector._ssl, ssl.SSLContext)


def test_http_method():
config = {
"url": "http://example.com/",
"path": "file.html",
}

fs = HTTPFileSystem(**config, method="PUT")
assert fs.upload_method == "PUT"

fs = HTTPFileSystem(**config, method="POST")
assert fs.upload_method == "POST"