Skip to content

Commit

Permalink
add tests for query_string
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-dd committed May 27, 2024
1 parent b6d9c1a commit 4a9f6e4
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/parsing/query_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) fn parse_query_string(bytes: &Bytes) -> Resolved {
query_string = &query_string[1..];
}
let mut result = BTreeMap::new();
let parsed = form_urlencoded::parse(query_string);
let parsed = form_urlencoded::parse_query_string(query_string);
for (k, value) in parsed {
let value = value.as_ref();
result
Expand All @@ -27,3 +27,84 @@ pub(crate) fn parse_query_string(bytes: &Bytes) -> Resolved {
}
Ok(result.into())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::btreemap;

#[test]
fn test_parses_complete() {
let result = parse_query_string(&"foo=%2B1&bar=2&xyz=&abc".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"foo" => "+1",
"bar" => "2",
"xyz" => "",
"abc" => "",
})
);
}

#[test]
fn test_parses_multiple_values() {
let result = parse_query_string(&"foo=bar&foo=xyz".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"foo" => vec!["bar", "xyz"],
})
);
}

#[test]
fn test_parses_ruby_on_rails_multiple_values() {
let result = parse_query_string(&"?foo%5b%5d=bar&foo%5b%5d=xyz".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"foo[]" => vec!["bar", "xyz"],
})
);
}

#[test]
fn test_parses_empty_key() {
let result = parse_query_string(&"=&=".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"" => vec!["", ""],
})
);
}

#[test]
fn test_parses_single_key() {
let result = parse_query_string(&"foo".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"foo" => "",
})
);
}

#[test]
fn test_parses_empty_string() {
let result = parse_query_string(&"".into()).unwrap();
assert_eq!(result, Value::from(btreemap! {}));
}

#[test]
fn test_parses_if_starts_with_question_mark() {
let result = parse_query_string(&"?foo=bar".into()).unwrap();
assert_eq!(
result,
Value::from(btreemap! {
"foo" => "bar",
})
);
}
}

0 comments on commit 4a9f6e4

Please sign in to comment.