Skip to content

Commit

Permalink
Remove trailing slash from bucket_host
Browse files Browse the repository at this point in the history
The `fastenv.cloud.object_storage.ObjectStorageConfig` class is used to
configure fastenv for connecting to object storage buckets. The class
accepts a `bucket_host` in "virtual-hosted-style," like
`<BUCKET_NAME>.s3.<REGION>.amazonaws.com` for AWS S3 or
`<BUCKET_NAME>.s3.<REGION>.backblazeb2.com` for Backblaze B2.

It is common to append trailing slashes ("/") to URLs. These should be
removed during configuration because some of the configuration logic
uses the Python string method `string.endswith()` assuming there is no
slash at the end.

This commit will update
`fastenv.cloud.object_storage.ObjectStorageConfig`
to remove trailing slashes from `bucket_host`.

#8
https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html
  • Loading branch information
br3ndonland committed Jan 15, 2024
1 parent 69bdf05 commit 8492656
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions fastenv/cloud/object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(
"`<BUCKET_NAME>.s3.<REGION>.backblazeb2.com` for Backblaze B2."
)
elif bucket_host and not bucket_name:
bucket_host = bucket_host.rstrip("/")
scheme = (
"http://"
if bucket_host.startswith("http://")
Expand Down
16 changes: 16 additions & 0 deletions tests/cloud/test_object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ def test_config_if_scheme_in_bucket_host(
assert self.config_is_correct(config, expected_bucket_host=expected_bucket_host)
assert scheme not in config.bucket_host

def test_config_if_trailing_slash_in_bucket_host(
self, mocker: MockerFixture
) -> None:
"""Assert that trailing slash ("/") is removed."""
mocker.patch.dict(os.environ, clear=True)
bucket_host = f"{self.example_bucket_host}/"
expected_bucket_host = self.example_bucket_host
config = fastenv.cloud.object_storage.ObjectStorageConfig(
access_key=self.example_access_key,
secret_key=self.example_secret_key,
bucket_host=bucket_host,
bucket_region=self.example_bucket_region,
)
assert self.config_is_correct(config, expected_bucket_host=expected_bucket_host)
assert not config.bucket_host.endswith("/")


class TestObjectStorageClientUnit:
"""Test `class ObjectStorageClient` and its methods.
Expand Down

0 comments on commit 8492656

Please sign in to comment.