Debian Codegraph @

third_party/rust/warp/tests/path.rs

raw
1 #![deny(warnings)]
2 #[macro_use]
3 extern crate warp;
4
5 use futures_util::future;
6 use warp::Filter;
7
8 #[tokio::test]
9 async fn path() {
10 let _ = pretty_env_logger::try_init();
11
12 let foo = warp::path("foo");
13 let bar = warp::path(String::from("bar"));
14 let foo_bar = foo.and(bar.clone());
15
16 // /foo
17 let foo_req = || warp::test::request().path("/foo");
18
19 assert!(foo_req().matches(&foo).await);
20 assert!(!foo_req().matches(&bar).await);
21 assert!(!foo_req().matches(&foo_bar).await);
22
23 // /foo/bar
24 let foo_bar_req = || warp::test::request().path("/foo/bar");
25
26 assert!(foo_bar_req().matches(&foo).await);
27 assert!(!foo_bar_req().matches(&bar).await);
28 assert!(foo_bar_req().matches(&foo_bar).await);
29 }
30
31 #[tokio::test]
32 async fn param() {
33 let _ = pretty_env_logger::try_init();
34
35 let num = warp::path::param::<u32>();
36
37 let req = warp::test::request().path("/321");
38 assert_eq!(req.filter(&num).await.unwrap(), 321);
39
40 let s = warp::path::param::<String>();
41
42 let req = warp::test::request().path("/warp");
43 assert_eq!(req.filter(&s).await.unwrap(), "warp");
44
45 // u32 doesn't extract a non-int
46 let req = warp::test::request().path("/warp");
47 assert!(!req.matches(&num).await);
48
49 let combo = num.map(|n| n + 5).and(s);
50
51 let req = warp::test::request().path("/42/vroom");
52 assert_eq!(req.filter(&combo).await.unwrap(), (47, "vroom".to_string()));
53
54 // empty segments never match
55 let req = warp::test::request();
56 assert!(
57 !req.matches(&s).await,
58 "param should never match an empty segment"
59 );
60 }
61
62 #[tokio::test]
63 async fn end() {
64 let _ = pretty_env_logger::try_init();
65
66 let foo = warp::path("foo");
67 let end = warp::path::end();
68 let foo_end = foo.and(end);
69
70 assert!(
71 warp::test::request().path("/").matches(&end).await,
72 "end() matches /"
73 );
74
75 assert!(
76 warp::test::request()
77 .path("http://localhost:1234")
78 .matches(&end)
79 .await,
80 "end() matches /"
81 );
82
83 assert!(
84 warp::test::request()
85 .path("http://localhost:1234?q=2")
86 .matches(&end)
87 .await,
88 "end() matches empty path"
89 );
90
91 assert!(
92 warp::test::request()
93 .path("localhost:1234")
94 .matches(&end)
95 .await,
96 "end() matches authority-form"
97 );
98
99 assert!(
100 !warp::test::request().path("/foo").matches(&end).await,
101 "end() doesn't match /foo"
102 );
103
104 assert!(
105 warp::test::request().path("/foo").matches(&foo_end).await,
106 "path().and(end()) matches /foo"
107 );
108
109 assert!(
110 warp::test::request().path("/foo/").matches(&foo_end).await,
111 "path().and(end()) matches /foo/"
112 );
113 }
114
115 #[tokio::test]
116 async fn tail() {
117 let tail = warp::path::tail();
118
119 // matches full path
120 let ex = warp::test::request()
121 .path("/42/vroom")
122 .filter(&tail)
123 .await
124 .unwrap();
125 assert_eq!(ex.as_str(), "42/vroom");
126
127 // matches index
128 let ex = warp::test::request().path("/").filter(&tail).await.unwrap();
129 assert_eq!(ex.as_str(), "");
130
131 // doesn't include query
132 let ex = warp::test::request()
133 .path("/foo/bar?baz=quux")
134 .filter(&tail)
135 .await
136 .unwrap();
137 assert_eq!(ex.as_str(), "foo/bar");
138
139 // doesn't include previously matched prefix
140 let and = warp::path("foo").and(tail);
141 let ex = warp::test::request()
142 .path("/foo/bar")
143 .filter(&and)
144 .await
145 .unwrap();
146 assert_eq!(ex.as_str(), "bar");
147
148 // sets unmatched path index to end
149 let m = tail.and(warp::path("foo"));
150 assert!(!warp::test::request().path("/foo/bar").matches(&m).await);
151
152 let m = tail.and(warp::path::end());
153 assert!(warp::test::request().path("/foo/bar").matches(&m).await);
154
155 let ex = warp::test::request()
156 .path("localhost")
157 .filter(&tail)
158 .await
159 .unwrap();
160 assert_eq!(ex.as_str(), "/");
161 }
162
163 #[tokio::test]
164 async fn or() {
165 let _ = pretty_env_logger::try_init();
166
167 // /foo/bar OR /foo/baz
168 let foo = warp::path("foo");
169 let bar = warp::path("bar");
170 let baz = warp::path("baz");
171 let p = foo.and(bar.or(baz));
172
173 // /foo/bar
174 let req = warp::test::request().path("/foo/bar");
175
176 assert!(req.matches(&p).await);
177
178 // /foo/baz
179 let req = warp::test::request().path("/foo/baz");
180
181 assert!(req.matches(&p).await);
182
183 // deeper nested ORs
184 // /foo/bar/baz OR /foo/baz/bar OR /foo/bar/bar
185 let p = foo
186 .and(bar.and(baz).map(|| panic!("shouldn't match")))
187 .or(foo.and(baz.and(bar)).map(|| panic!("shouldn't match")))
188 .or(foo.and(bar.and(bar)));
189
190 // /foo/baz
191 let req = warp::test::request().path("/foo/baz/baz");
192 assert!(!req.matches(&p).await);
193
194 // /foo/bar/bar
195 let req = warp::test::request().path("/foo/bar/bar");
196 assert!(req.matches(&p).await);
197 }
198
199 #[tokio::test]
200 async fn or_else() {
201 let _ = pretty_env_logger::try_init();
202
203 let foo = warp::path("foo");
204 let bar = warp::path("bar");
205
206 let p = foo.and(bar.or_else(|_| future::ok::<_, std::convert::Infallible>(())));
207
208 // /foo/bar
209 let req = warp::test::request().path("/foo/nope");
210
211 assert!(req.matches(&p).await);
212 }
213
214 #[tokio::test]
215 async fn path_macro() {
216 let _ = pretty_env_logger::try_init();
217
218 let req = warp::test::request().path("/foo/bar");
219 let p = path!("foo" / "bar");
220 assert!(req.matches(&p).await);
221
222 let req = warp::test::request().path("/foo/bar");
223 let p = path!(String / "bar");
224 assert_eq!(req.filter(&p).await.unwrap(), "foo");
225
226 let req = warp::test::request().path("/foo/bar");
227 let p = path!("foo" / String);
228 assert_eq!(req.filter(&p).await.unwrap(), "bar");
229
230 // Requires path end
231
232 let req = warp::test::request().path("/foo/bar/baz");
233 let p = path!("foo" / "bar");
234 assert!(!req.matches(&p).await);
235
236 let req = warp::test::request().path("/foo/bar/baz");
237 let p = path!("foo" / "bar").and(warp::path("baz"));
238 assert!(!req.matches(&p).await);
239
240 // Prefix syntax
241
242 let req = warp::test::request().path("/foo/bar/baz");
243 let p = path!("foo" / "bar" / ..);
244 assert!(req.matches(&p).await);
245
246 let req = warp::test::request().path("/foo/bar/baz");
247 let p = path!("foo" / "bar" / ..).and(warp::path!("baz"));
248 assert!(req.matches(&p).await);
249
250 // Empty
251
252 let req = warp::test::request().path("/");
253 let p = path!();
254 assert!(req.matches(&p).await);
255
256 let req = warp::test::request().path("/foo");
257 let p = path!();
258 assert!(!req.matches(&p).await);
259 }
260
261 #[tokio::test]
262 async fn full_path() {
263 let full_path = warp::path::full();
264
265 let foo = warp::path("foo");
266 let bar = warp::path("bar");
267 let param = warp::path::param::<u32>();
268
269 // matches full request path
270 let ex = warp::test::request()
271 .path("/42/vroom")
272 .filter(&full_path)
273 .await
274 .unwrap();
275 assert_eq!(ex.as_str(), "/42/vroom");
276
277 // matches index
278 let ex = warp::test::request()
279 .path("/")
280 .filter(&full_path)
281 .await
282 .unwrap();
283 assert_eq!(ex.as_str(), "/");
284
285 // does not include query
286 let ex = warp::test::request()
287 .path("/foo/bar?baz=quux")
288 .filter(&full_path)
289 .await
290 .unwrap();
291 assert_eq!(ex.as_str(), "/foo/bar");
292
293 // includes previously matched prefix
294 let and = foo.and(full_path);
295 let ex = warp::test::request()
296 .path("/foo/bar")
297 .filter(&and)
298 .await
299 .unwrap();
300 assert_eq!(ex.as_str(), "/foo/bar");
301
302 // includes following matches
303 let and = full_path.and(foo);
304 let ex = warp::test::request()
305 .path("/foo/bar")
306 .filter(&and)
307 .await
308 .unwrap();
309 assert_eq!(ex.as_str(), "/foo/bar");
310
311 // includes previously matched param
312 let and = foo.and(param).and(full_path);
313 let (_, ex) = warp::test::request()
314 .path("/foo/123")
315 .filter(&and)
316 .await
317 .unwrap();
318 assert_eq!(ex.as_str(), "/foo/123");
319
320 // does not modify matching
321 let m = full_path.and(foo).and(bar);
322 assert!(warp::test::request().path("/foo/bar").matches(&m).await);
323
324 // doesn't panic on authority-form
325 let ex = warp::test::request()
326 .path("localhost:1234")
327 .filter(&full_path)
328 .await
329 .unwrap();
330 assert_eq!(ex.as_str(), "/");
331 }
332
333 #[tokio::test]
334 async fn peek() {
335 let peek = warp::path::peek();
336
337 let foo = warp::path("foo");
338 let bar = warp::path("bar");
339 let param = warp::path::param::<u32>();
340
341 // matches full request path
342 let ex = warp::test::request()
343 .path("/42/vroom")
344 .filter(&peek)
345 .await
346 .unwrap();
347 assert_eq!(ex.as_str(), "42/vroom");
348
349 // matches index
350 let ex = warp::test::request().path("/").filter(&peek).await.unwrap();
351 assert_eq!(ex.as_str(), "");
352
353 // does not include query
354 let ex = warp::test::request()
355 .path("/foo/bar?baz=quux")
356 .filter(&peek)
357 .await
358 .unwrap();
359 assert_eq!(ex.as_str(), "foo/bar");
360
361 // does not include previously matched prefix
362 let and = foo.and(peek);
363 let ex = warp::test::request()
364 .path("/foo/bar")
365 .filter(&and)
366 .await
367 .unwrap();
368 assert_eq!(ex.as_str(), "bar");
369
370 // includes following matches
371 let and = peek.and(foo);
372 let ex = warp::test::request()
373 .path("/foo/bar")
374 .filter(&and)
375 .await
376 .unwrap();
377 assert_eq!(ex.as_str(), "foo/bar");
378
379 // does not include previously matched param
380 let and = foo.and(param).and(peek);
381 let (_, ex) = warp::test::request()
382 .path("/foo/123")
383 .filter(&and)
384 .await
385 .unwrap();
386 assert_eq!(ex.as_str(), "");
387
388 // does not modify matching
389 let and = peek.and(foo).and(bar);
390 assert!(warp::test::request().path("/foo/bar").matches(&and).await);
391 }
392
393 #[tokio::test]
394 async fn peek_segments() {
395 let peek = warp::path::peek();
396
397 // matches full request path
398 let ex = warp::test::request()
399 .path("/42/vroom")
400 .filter(&peek)
401 .await
402 .unwrap();
403
404 assert_eq!(ex.segments().collect::<Vec<_>>(), &["42", "vroom"]);
405
406 // matches index
407 let ex = warp::test::request().path("/").filter(&peek).await.unwrap();
408
409 let segs = ex.segments().collect::<Vec<_>>();
410 assert_eq!(segs, Vec::<&str>::new());
411 }