-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_s3_parse_url.py
185 lines (149 loc) · 5.53 KB
/
test_s3_parse_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from s3_endpoint_parse import parse
bucket = "my-bucket"
key = "path/to/object"
region = "xyz-to-nowhere"
def test_invalid_empty():
result = parse("")
assert result["match"] is False
def test_invalid_random():
result = parse("http://www.google.com/bucket/path")
assert result["match"] is False
def test_invalid_proto():
result = parse("//s3.amazonaws.com/bucket/key")
assert result["match"] is False
def virtual_hosted_style():
host = f"{bucket}.s3.{region}.amazonaws.com"
host_fips = f"{bucket}.s3-fips.{region}.amazonaws.com"
host_dual = f"{bucket}.s3.dualstack.{region}.amazonaws.com"
host_both = f"{bucket}.s3-fips.dualstack.{region}.amazonaws.com"
host_cn = f"{bucket}.s3.{region}.amazonaws.com.cn"
fixtures = [
# (url, path, region)
(f"https://{host}/{key}", key, region),
(f"http://{host}/", "", region),
(f"s3://{host}", "", region),
(f"https://{host_fips}/{key}", key, region),
(f"http://{host_fips}/", "", region),
(f"s3://{host_fips}", "", region),
(f"https://{host_dual}/{key}", key, region),
(f"http://{host_dual}/", "", region),
(f"s3://{host_dual}", "", region),
(f"https://{host_both}/{key}", key, region),
(f"http://{host_both}/", "", region),
(f"s3://{host_both}", "", region),
(f"https://{host_cn}/{key}", key, region),
(f"http://{host_cn}/", "", region),
(f"s3://{host_cn}", "", region),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_path_style():
host = f"s3.{region}.amazonaws.com"
host_fips = f"s3-fips.{region}.amazonaws.com"
host_dual = f"s3.dualstack.{region}.amazonaws.com"
host_both = f"s3-fips.dualstack.{region}.amazonaws.com"
fixtures = [
# (url, path, region)
(f"https://{host}/{bucket}/{key}", key, region),
(f"http://{host}/{bucket}/", "", region),
(f"s3://{host}/{bucket}", "", region),
(f"s3://{host_fips}/{bucket}/{key}", key, region),
(f"https://{host_fips}/{bucket}/", "", region),
(f"http://{host_fips}/{bucket}", "", region),
(f"https://{host_dual}/{bucket}/{key}", key, region),
(f"s3://{host_dual}/{bucket}/", "", region),
(f"http://{host_dual}/{bucket}", "", region),
(f"https://{host_both}/{bucket}/{key}", key, region),
(f"https://{host_both}/{bucket}/", "", region),
(f"s3://{host_both}/{bucket}", "", region),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_path_style_no_match_no_bucket():
host = f"s3.{region}.amazonaws.com"
fixtures = [
# (url, path, region)
(f"http://{host}/", "", region),
(f"s3://{host}", "", region),
]
for (url, _, _) in fixtures:
got = parse(url)
assert got["match"] is False
def test_legacy_global_endpoint():
host = f"{bucket}.s3.amazonaws.com"
fixtures = [
# (url, path, region)
(f"https://{host}/{key}", key, ""),
(f"http://{host}/", "", ""),
(f"s3://{host}", "", ""),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_legacy_global_endpoint():
host = f"{bucket}.s3.amazonaws.com"
fixtures = [
# (url, path, region)
(f"https://{host}/{key}", key, ""),
(f"http://{host}/", "", ""),
(f"s3://{host}", "", ""),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_legacy_global_endpoint_us_east():
host = "s3.amazonaws.com"
fixtures = [
# (url, key, region)
(f"http://{host}/{bucket}/{key}", key, ""),
(f"s3://{host}/{bucket}", "", ""),
(f"https://{host}/{bucket}/", "", ""),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_legacy_s3_region():
host = f"{bucket}.s3-{region}.amazonaws.com"
fixtures = [
# (url, key, region)
(f"https://{host}/{key}", key, region),
(f"s3://{host}/", "", region),
(f"http://{host}", "", region),
]
for (url, want_key, want_region) in fixtures:
got = parse(url)
assert got["match"] is True
assert got["bucket"] == bucket
assert got["key"] == want_key
assert got["region"] == want_region
def test_protocol_parse():
endpoint = f"{bucket}.s3.{region}.amazonaws.com/{bucket}/{key}"
fixtures = [
# (url, proto)
(f"https://{endpoint}", "https"),
(f"http://{endpoint}", "http"),
(f"s3://{endpoint}", "s3"),
(f"my-s3-protocol://{endpoint}", "my-s3-protocol"),
(f"://{endpoint}", ""),
(endpoint, ""),
]
for (url, want_proto) in fixtures:
got = parse(url)
assert got["protocol"] == want_proto