Skip to content

Commit

Permalink
adjust tests & fail parsing on incomplete consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Nov 22, 2024
1 parent 89f65f7 commit 585c9ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,14 @@ impl FromStr for AllPathPatterns {

fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_path_pattern(s)
.map(|(_, result)| result)
.map_err(|err| err.to_string())
.and_then(|(leftover, result)| {
if !leftover.is_empty() {
Err("Failed to parse path".to_string())
} else {
Ok(result)
}
})
}
}

Expand Down Expand Up @@ -519,7 +525,7 @@ mod tests {

#[test]
fn split_path_works_with_single_value() {
let path_pattern = "foo";
let path_pattern = "/foo";
let result = AllPathPatterns::parse(path_pattern);

let expected = AllPathPatterns {
Expand All @@ -532,7 +538,7 @@ mod tests {

#[test]
fn split_path_works_with_multiple_values() {
let path_pattern = "foo/bar";
let path_pattern = "/foo/bar";
let result = AllPathPatterns::parse(path_pattern);

let expected = AllPathPatterns {
Expand All @@ -545,7 +551,7 @@ mod tests {

#[test]
fn split_path_works_with_variables() {
let path_pattern = "foo/bar/{var}";
let path_pattern = "/foo/bar/{var}";
let result = AllPathPatterns::parse(path_pattern);

let expected = AllPathPatterns {
Expand All @@ -562,7 +568,7 @@ mod tests {

#[test]
fn split_path_works_with_variables_and_queries() {
let path_pattern = "foo/bar/{var}?{userid1}&{userid2}";
let path_pattern = "/foo/bar/{var}?{userid1}&{userid2}";
let result = AllPathPatterns::parse(path_pattern);

let expected = AllPathPatterns {
Expand Down Expand Up @@ -741,22 +747,22 @@ mod tests {
assert_eq!(core_http_definition, decoded);
}
test_encode_decode(
"foo/{user-id}",
"/foo/{user-id}",
"let x: str = request.path.user-id; \"shopping-cart-${if x>100 then 0 else 1}\"",
"${ let result = golem:it/api.{do-something}(request.body); {status: if result.user == \"admin\" then 401 else 200 } }",
);
test_encode_decode(
"foo/{user-id}",
"/foo/{user-id}",
"let x: str = request.path.user-id; \"shopping-cart-${if x>100 then 0 else 1}\"",
"${ let result = golem:it/api.{do-something}(request.body.foo); {status: if result.user == \"admin\" then 401 else 200 } }",
);
test_encode_decode(
"foo/{user-id}",
"/foo/{user-id}",
"let x: str = request.path.user-id; \"shopping-cart-${if x>100 then 0 else 1}\"",
"${ let result = golem:it/api.{do-something}(request.path.user-id); {status: if result.user == \"admin\" then 401 else 200 } }",
);
test_encode_decode(
"foo",
"/foo",
"let x: str = request.body.user-id; \"shopping-cart-${if x>100 then 0 else 1}\"",
"${ let result = golem:it/api.{do-something}(\"foo\"); {status: if result.user == \"admin\" then 401 else 200 } }",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,6 @@ mod tests {
);

// {+var} is not allowed in the middle of the path
assert!(parse_path_pattern("/api/{foo}/{+others}/{bar}").is_err());
assert!(AllPathPatterns::parse("/api/{foo}/{+others}/{bar}").is_err());
}
}

0 comments on commit 585c9ca

Please sign in to comment.