requestMatchesPathOrTemplate function
A function to build a CharlatanRequestMatcher that matches if the request
path matches the provided pathOrTemplate
.
Implementation
CharlatanRequestMatcher requestMatchesPathOrTemplate(String pathOrTemplate) =>
(request) {
final uri = Uri.parse(request.path);
final template = UriTemplate(pathOrTemplate);
final parser = UriParser(template);
final match = parser.matches(uri);
if (!match) {
return false;
}
// by reversing the parse we're confirming that we match the right pattern
// e.g. /goals/{id} will also be a match for /goals/{id}/foo
// but we want to use a /goals/{id}/foo pattern if one exists
final vars = parser.parse(uri);
final reverseMatch = template.expand(vars) == uri.toString();
if (!reverseMatch) {
return false;
}
return true;
};