|
1 | 1 | # Note: these tests depend on the extension being installed and actual AWS credentials being configured, such
|
2 | 2 | # that the proxy can be started within the tests. They are designed to be mostly run in CI at this point.
|
3 | 3 | import gzip
|
| 4 | +import re |
| 5 | +from urllib.parse import urlparse |
4 | 6 |
|
5 | 7 | import boto3
|
6 | 8 | import pytest
|
@@ -41,20 +43,33 @@ def _start(config: dict = None):
|
41 | 43 |
|
42 | 44 |
|
43 | 45 | @pytest.mark.parametrize("metadata_gzip", [True, False])
|
44 |
| -@pytest.mark.parametrize("host_addressing", [True, False]) |
45 |
| -def test_s3_requests(start_aws_proxy, s3_create_bucket, metadata_gzip, host_addressing): |
| 46 | +@pytest.mark.parametrize("target_endpoint", ["local_domain", "aws_domain", "default"]) |
| 47 | +def test_s3_requests(start_aws_proxy, s3_create_bucket, metadata_gzip, target_endpoint): |
46 | 48 | # start proxy
|
47 | 49 | config = ProxyConfig(services={"s3": {"resources": ".*"}}, bind_host=PROXY_BIND_HOST)
|
48 | 50 | start_aws_proxy(config)
|
49 | 51 |
|
50 | 52 | # create clients
|
51 |
| - if host_addressing: |
| 53 | + if target_endpoint == "default": |
| 54 | + s3_client = connect_to().s3 |
| 55 | + else: |
52 | 56 | s3_client = connect_to(
|
53 | 57 | endpoint_url="http://s3.localhost.localstack.cloud:4566",
|
54 | 58 | config=Config(s3={"addressing_style": "virtual"}),
|
55 | 59 | ).s3
|
56 |
| - else: |
57 |
| - s3_client = connect_to().s3 |
| 60 | + |
| 61 | + if target_endpoint == "aws_domain": |
| 62 | + |
| 63 | + def _add_header(request, **kwargs): |
| 64 | + # instrument boto3 client to add custom `Host` header, mimicking a `*.s3.amazonaws.com` request |
| 65 | + url = urlparse(request.url) |
| 66 | + match = re.match(r"(.+)\.s3\.localhost\.localstack\.cloud", url.netloc) |
| 67 | + if match: |
| 68 | + request.headers.add_header("host", f"{match.group(1)}.s3.amazonaws.com") |
| 69 | + |
| 70 | + s3_client.meta.events.register_first("before-sign.*.*", _add_header) |
| 71 | + |
| 72 | + # define S3 client pointing to real AWS |
58 | 73 | s3_client_aws = boto3.client("s3")
|
59 | 74 |
|
60 | 75 | # list buckets to assert that proxy is up and running
|
|
0 commit comments