-
Notifications
You must be signed in to change notification settings - Fork 71
/
test_warp.rs
104 lines (88 loc) · 2.78 KB
/
test_warp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![cfg(feature = "warp")]
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_qs as qs;
extern crate warp_framework as warp;
use qs::Config as QsConfig;
use serde::de::Error;
use warp::{http::StatusCode, Filter};
fn from_str<'de, D, S>(deserializer: D) -> Result<S, D::Error>
where
D: serde::Deserializer<'de>,
S: std::str::FromStr,
{
let s = <&str as serde::Deserialize>::deserialize(deserializer)?;
S::from_str(s).map_err(|_| D::Error::custom("could not parse string"))
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct Query {
foo: u64,
bars: Vec<u64>,
#[serde(flatten)]
common: CommonParams,
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct CommonParams {
#[serde(deserialize_with = "from_str")]
limit: u64,
#[serde(deserialize_with = "from_str")]
offset: u64,
#[serde(deserialize_with = "from_str")]
remaining: bool,
}
#[test]
fn test_default_error_handler() {
futures::executor::block_on(async {
let filter = qs::warp::query::<Query>(QsConfig::default())
.map(|_| "")
.recover(qs::warp::recover_fn);
let resp = warp::test::request().path("/test").reply(&filter).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
})
}
#[test]
fn test_composite_querystring_extractor() {
futures::executor::block_on(async {
let filter = qs::warp::query::<Query>(QsConfig::default());
let s = warp::test::request()
.path("/test?foo=1&bars[]=0&bars[]=1&limit=100&offset=50&remaining=true")
.filter(&filter)
.await
.unwrap();
assert_eq!(s.foo, 1);
assert_eq!(s.bars, vec![0, 1]);
assert_eq!(s.common.limit, 100);
assert_eq!(s.common.offset, 50);
assert!(s.common.remaining);
})
}
#[test]
fn test_default_qs_config() {
futures::executor::block_on(async {
let filter = qs::warp::query::<Query>(QsConfig::default())
.map(|_| "")
.recover(qs::warp::recover_fn);
let resp = warp::test::request()
.path("/test?foo=1&bars%5B%5D=3&limit=100&offset=50&remaining=true")
.reply(&filter)
.await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
})
}
#[test]
fn test_custom_qs_config() {
futures::executor::block_on(async {
let filter = qs::warp::query::<Query>(QsConfig::new(5, false));
let s = warp::test::request()
.path("/test?foo=1&bars%5B%5D=3&limit=100&offset=50&remaining=true")
.filter(&filter)
.await
.unwrap();
assert_eq!(s.foo, 1);
assert_eq!(s.bars, vec![3]);
assert_eq!(s.common.limit, 100);
assert_eq!(s.common.offset, 50);
assert!(s.common.remaining);
})
}