axum_test/internals/
request_path_formatter.rs1use http::Method;
2use std::fmt;
3
4use crate::internals::QueryParamsStore;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct RequestPathFormatter<'a> {
8 method: &'a Method,
9
10 user_requested_path: &'a str,
12 query_params: Option<&'a QueryParamsStore>,
13}
14
15impl<'a> RequestPathFormatter<'a> {
16 pub fn new(
17 method: &'a Method,
18 user_requested_path: &'a str,
19 query_params: Option<&'a QueryParamsStore>,
20 ) -> Self {
21 Self {
22 method,
23 user_requested_path,
24 query_params,
25 }
26 }
27}
28
29impl fmt::Display for RequestPathFormatter<'_> {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 let method = &self.method;
32 let user_requested_path = &self.user_requested_path;
33
34 match self.query_params {
35 None => {
36 write!(f, "{method} {user_requested_path}")
37 }
38 Some(query_params) => {
39 if query_params.is_empty() {
40 write!(f, "{method} {user_requested_path}")
41 } else {
42 write!(f, "{method} {user_requested_path}?{query_params}")
43 }
44 }
45 }
46 }
47}
48
49#[cfg(test)]
50mod test_fmt {
51 use super::*;
52
53 #[test]
54 fn it_should_format_with_path_given() {
55 let query_params = QueryParamsStore::new();
56 let debug = RequestPathFormatter::new(&Method::GET, &"/donkeys", Some(&query_params));
57 let output = debug.to_string();
58
59 assert_eq!(output, "GET /donkeys");
60 }
61
62 #[test]
63 fn it_should_format_with_path_given_and_no_query_params() {
64 let debug = RequestPathFormatter::new(&Method::GET, &"/donkeys", None);
65 let output = debug.to_string();
66
67 assert_eq!(output, "GET /donkeys");
68 }
69
70 #[test]
71 fn it_should_format_with_path_given_and_query_params() {
72 let mut query_params = QueryParamsStore::new();
73 query_params.add_raw("value=123".to_string());
74 query_params.add_raw("another-value".to_string());
75
76 let debug = RequestPathFormatter::new(&Method::GET, &"/donkeys", Some(&query_params));
77 let output = debug.to_string();
78
79 assert_eq!(output, "GET /donkeys?value=123&another-value");
80 }
81}