Skip to content

Commit 3384473

Browse files
committed
fixed coverage
1 parent 47a702e commit 3384473

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

packages/google-api-core/google/api_core/path_template.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,16 @@ def replacer(match):
143143
elif positional == "**":
144144
wildcard_types.append("**")
145145
return r"(.+)"
146-
elif name is not None:
147-
if not template or template == "*":
148-
wildcard_types.append("*")
149-
return r"([^/]+)"
150-
elif template == "**":
151-
wildcard_types.append("**")
152-
return r"(.+)"
153-
else:
154-
sub_pattern, sub_types = _build_capture_pattern(template)
155-
wildcard_types.extend(sub_types)
156-
return sub_pattern.pattern
157-
return match.group(0)
146+
elif not template or template == "*":
147+
wildcard_types.append("*")
148+
return r"([^/]+)"
149+
elif template == "**":
150+
wildcard_types.append("**")
151+
return r"(.+)"
152+
else:
153+
sub_pattern, sub_types = _build_capture_pattern(template)
154+
wildcard_types.extend(sub_types)
155+
return sub_pattern.pattern
158156

159157
parts = []
160158
last_idx = 0
@@ -232,7 +230,7 @@ def _extract_and_validate_wildcards(
232230
if captured_val in (".", ".."):
233231
name_str = property_name if property_name else "positional variable"
234232
raise ValueError("Invalid value {} for {}.".format(val, name_str))
235-
elif wildcard_type == "**":
233+
else:
236234
if not _validate_multi_segment_value(captured_val):
237235
name_str = property_name if property_name else "positional variable"
238236
raise ValueError("Invalid value {} for {}.".format(val, name_str))

packages/google-api-core/tests/unit/test_path_template.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ def test_path_traversal_dots_validation_double_star_valid(name_val, expected_pat
764764
],
765765
)
766766
def test_path_traversal_dots_validation_double_star_invalid(name_val):
767-
with pytest.raises(ValueError, match="Invalid value .* for name\\."):
767+
with pytest.raises(ValueError, match=r"Invalid value .* for name\."):
768768
path_template.expand(
769769
"/v3/{name=projects/*/monitoredResourceDescriptors/**}",
770770
name=name_val,
@@ -786,3 +786,29 @@ def test_percent_encoding_unreserved_characters(tmpl, kwargs, expected_result):
786786
# For single-segment with '/', validate should fail because '/' is preserved
787787
if "/" in kwargs.get("name", "") and tmpl == "/v1/{name}":
788788
assert not path_template.validate(tmpl, result)
789+
790+
791+
@pytest.mark.parametrize(
792+
"tmpl, args, kwargs, expected_err_match",
793+
[
794+
("/v1/**", [".."], {}, r"Invalid value .* for positional variable\."),
795+
("/v1/{name=**}", [], {"name": ".."}, r"Invalid value .* for name\."),
796+
],
797+
)
798+
def test_path_traversal_dots_validation_bare_double_star(tmpl, args, kwargs, expected_err_match):
799+
with pytest.raises(ValueError, match=expected_err_match):
800+
path_template.expand(tmpl, *args, **kwargs)
801+
802+
@pytest.mark.parametrize(
803+
"template_str, expected_pattern, expected_wildcards",
804+
[
805+
("projects/{project}/locations/{location}", "projects/([^/]+)/locations/([^/]+)", ("*", "*")),
806+
("projects/{project=**}", "projects/(.+)", ("**",)),
807+
("projects/{project=locations/*}", "projects/locations/([^/]+)", ("*",)),
808+
("projects/*/locations/**", "projects/([^/]+)/locations/(.+)", ("*", "**")),
809+
],
810+
)
811+
def test_build_capture_pattern(template_str, expected_pattern, expected_wildcards):
812+
pattern, wildcards = path_template._build_capture_pattern(template_str)
813+
assert pattern.pattern == expected_pattern
814+
assert wildcards == expected_wildcards

0 commit comments

Comments
 (0)