Skip to content

Commit

Permalink
Remove warp::ext::set function
Browse files Browse the repository at this point in the history
Closes #222
  • Loading branch information
seanmonstar committed Dec 18, 2019
1 parent b6cbcca commit 12a9b2f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
17 changes: 1 addition & 16 deletions src/filters/ext.rs
Expand Up @@ -21,20 +21,6 @@ pub fn get<T: Clone + Send + Sync + 'static>(
})
}

/// Set an arbitrary value in the current route extensions.
///
/// After setting the value, it can be retrieved in another filter by
/// use `get` with the same type.
///
/// # Panics
///
/// This function panics if not called within the context of a `Filter`.
pub fn set<T: Send + Sync + 'static>(val: T) {
crate::route::with(move |route| {
route.extensions_mut().insert(val);
});
}

/// An error used to reject if `get` cannot find the extension.
#[derive(Debug)]
pub struct MissingExtension {
Expand All @@ -47,5 +33,4 @@ impl ::std::fmt::Display for MissingExtension {
}
}

impl StdError for MissingExtension {
}
impl StdError for MissingExtension {}
9 changes: 9 additions & 0 deletions src/test.rs
Expand Up @@ -228,6 +228,15 @@ impl RequestBuilder {
self
}

/// Add a type to the request's `http::Extensions`.
pub fn extension<T>(mut self, ext: T) -> Self
where
T: Send + Sync + 'static,
{
self.req.extensions_mut().insert(ext);
self
}

/// Set the bytes of this request body.
///
/// Default is an empty body.
Expand Down
15 changes: 2 additions & 13 deletions tests/ext.rs
Expand Up @@ -6,14 +6,9 @@ struct Ext1(i32);

#[tokio::test]
async fn set_and_get() {
let ext = warp::any()
.map(|| {
warp::ext::set(Ext1(55));
})
.untuple_one()
.and(warp::ext::get::<Ext1>());
let ext = warp::ext::get::<Ext1>();

let extracted = warp::test::request().filter(&ext).await.unwrap();
let extracted = warp::test::request().extension(Ext1(55)).filter(&ext).await.unwrap();

assert_eq!(extracted, Ext1(55));
}
Expand All @@ -27,9 +22,3 @@ async fn get_missing() {
assert_eq!(res.status(), 500);
assert_eq!(res.body(), "Missing request extension");
}

#[test]
#[should_panic]
fn set_outside_of_filter_should_panic() {
warp::ext::set(Ext1(55));
}

0 comments on commit 12a9b2f

Please sign in to comment.