Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) expose urlencoded::parse for downstream unit testing #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a, 'b> plugin::Plugin<Request<'a, 'b>> for UrlEncodedQuery {

fn eval(req: &mut Request) -> QueryResult {
match req.url.query() {
Some(ref query) => create_param_hashmap(&query),
Some(ref query) => parse(&query),
None => Err(UrlDecodingError::EmptyQuery)
}
}
Expand All @@ -96,10 +96,14 @@ impl<'a, 'b> plugin::Plugin<Request<'a, 'b>> for UrlEncodedBody {
req.get::<bodyparser::Raw>()
.map(|x| x.unwrap_or("".to_string()))
.map_err(|e| UrlDecodingError::BodyError(e))
.and_then(|x| create_param_hashmap(&x))
.and_then(|x| parse(&x))
}
}

pub fn parse(data: &str) -> QueryResult {
create_param_hashmap(data)
}

/// Parse a urlencoded string into an optional HashMap.
fn create_param_hashmap(data: &str) -> QueryResult {
match data {
Expand Down
22 changes: 22 additions & 0 deletions tests/round_trip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate urlencoded;

use std::collections::HashMap;

#[test]
fn test_empty_query_round_trip() {
let data = "";
let answer = urlencoded::parse(data);
assert!(answer.is_err());
}

#[test]
fn test_query_round_trip() {
let data = "band=arctic%20monkeys&band=mumford%20%26%20sons&band=temper trap&color=green";
let answer = urlencoded::parse(data).unwrap();

let mut control = HashMap::new();
control.insert("band".to_string(),
vec!["arctic monkeys".to_string(), "mumford & sons".to_string(), "temper trap".to_string()]);
control.insert("color".to_string(), vec!["green".to_string()]);
assert_eq!(answer, control);
}