test coverage with param change

This commit is contained in:
Adam Hopkins 2021-02-09 16:17:53 +02:00
parent 6b68c3702e
commit b850e49cb3
3 changed files with 41 additions and 42 deletions

View File

@ -405,46 +405,42 @@ class Sanic(BaseSanic):
# find all the parameters we will need to build in the URL # find all the parameters we will need to build in the URL
# matched_params = re.findall(self.router.parameter_pattern, uri) # matched_params = re.findall(self.router.parameter_pattern, uri)
route.finalize() route.finalize()
for params in route.params.values(): for param_info in route.params.values():
# name, _type, pattern = self.router.parse_parameter_string(match) # name, _type, pattern = self.router.parse_parameter_string(match)
# we only want to match against each individual parameter # we only want to match against each individual parameter
for idx, param_info in enumerate(params): try:
try: supplied_param = str(kwargs.pop(param_info.name))
supplied_param = str(kwargs.pop(param_info.name)) except KeyError:
except KeyError: raise URLBuildError(
raise URLBuildError( f"Required parameter `{param_info.name}` was not "
f"Required parameter `{param_info.name}` was not " "passed to url_for"
"passed to url_for" )
)
# determine if the parameter supplied by the caller # determine if the parameter supplied by the caller
# passes the test in the URL # passes the test in the URL
if param_info.pattern: if param_info.pattern:
passes_pattern = param_info.pattern.match(supplied_param) passes_pattern = param_info.pattern.match(supplied_param)
if not passes_pattern: if not passes_pattern:
if idx + 1 == len(params): if param_info.cast != str:
if param_info.cast != str: msg = (
msg = ( f'Value "{supplied_param}" '
f'Value "{supplied_param}" ' f"for parameter `{param_info.name}` does "
f"for parameter `{param_info.name}` does " "not match pattern for type "
"not match pattern for type " f"`{param_info.cast.__name__}`: "
f"`{param_info.cast.__name__}`: " f"{param_info.pattern.pattern}"
f"{param_info.pattern.pattern}" )
) else:
else: msg = (
msg = ( f'Value "{supplied_param}" for parameter '
f'Value "{supplied_param}" for parameter ' f"`{param_info.name}` does not satisfy "
f"`{param_info.name}` does not satisfy " f"pattern {param_info.pattern.pattern}"
f"pattern {param_info.pattern.pattern}" )
) raise URLBuildError(msg)
raise URLBuildError(msg)
else:
continue
# replace the parameter in the URL with the supplied value # replace the parameter in the URL with the supplied value
replacement_regex = f"(<{param_info.name}.*?>)" replacement_regex = f"(<{param_info.name}.*?>)"
out = re.sub(replacement_regex, supplied_param, out) out = re.sub(replacement_regex, supplied_param, out)
# parse the remainder of the keyword arguments into a querystring # parse the remainder of the keyword arguments into a querystring
query_string = urlencode(kwargs, doseq=True) if kwargs else "" query_string = urlencode(kwargs, doseq=True) if kwargs else ""

View File

@ -88,18 +88,18 @@ def test_bp_strict_slash(app):
app.blueprint(bp) app.blueprint(bp)
# request, response = app.test_client.get("/get") request, response = app.test_client.get("/get")
# assert response.text == "OK" assert response.text == "OK"
# assert response.json is None assert response.json is None
# request, response = app.test_client.get("/get/") request, response = app.test_client.get("/get/")
# assert response.status == 404 assert response.status == 404
request, response = app.test_client.post("/post/") request, response = app.test_client.post("/post/")
assert response.text == "OK" assert response.text == "OK"
# request, response = app.test_client.post("/post") request, response = app.test_client.post("/post")
# assert response.status == 404 assert response.status == 404
def test_bp_strict_slash_default_value(app): def test_bp_strict_slash_default_value(app):

View File

@ -538,6 +538,9 @@ def test_dynamic_route_regex(app):
async def handler(request, folder_id): async def handler(request, folder_id):
return text("OK") return text("OK")
app.router.finalize()
print(app.router.find_route_src)
request, response = app.test_client.get("/folder/test") request, response = app.test_client.get("/folder/test")
assert response.status == 200 assert response.status == 200