Skip to content

Commit

Permalink
feature: filter for raw query strings (#36)
Browse files Browse the repository at this point in the history
add `warp::query::raw()` filter

A new filter `raw()` creates a `Filter` that returns the raw query
string as type String.
  • Loading branch information
hwchen authored and seanmonstar committed Aug 8, 2018
1 parent c848f06 commit 44e66de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/filters/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ pub fn query<T: DeserializeOwned + Send>() -> impl Filter<Extract=One<T>, Error=
.unwrap_or_else(|| Err(reject::bad_request()))
})
}

/// Creates a `Filter` that returns the raw query string as type String.
pub fn raw() -> impl Filter<Extract=One<String>, Error=Rejection> + Copy {
filter_fn_one(|route| {
route
.query()
.map(|q| {
q.to_owned()
})
.map(Ok)
.unwrap_or_else(|| Err(reject::bad_request()))
})
}
11 changes: 11 additions & 0 deletions tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ fn query() {
assert_eq!(extracted["foo"], "bar");
assert_eq!(extracted["baz"], "quux");
}

#[test]
fn raw_query() {
let as_raw = warp::query::raw();

let req = warp::test::request()
.path("/?foo=bar&baz=quux");

let extracted = req.filter(&as_raw).unwrap();
assert_eq!(extracted, "foo=bar&baz=quux".to_owned());
}

0 comments on commit 44e66de

Please sign in to comment.