Skip to content

Commit 1405841

Browse files
authored
tests: add testcases for extract/payload/* (#630)
1 parent 7042b7a commit 1405841

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

crates/oapi/src/extract/payload/form.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,111 @@ where
106106
operation.request_body = Some(request_body);
107107
}
108108
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use std::collections::BTreeMap;
113+
114+
use assert_json_diff::assert_json_eq;
115+
use salvo_core::test::TestClient;
116+
use serde_json::json;
117+
118+
use super::*;
119+
120+
#[test]
121+
fn test_form_body_into_inner() {
122+
let form = FormBody::<String>("form_body".to_string());
123+
assert_eq!(form.into_inner(), "form_body".to_string());
124+
}
125+
126+
#[test]
127+
fn test_form_body_deref() {
128+
let form = FormBody::<String>("form_body".to_string());
129+
assert_eq!(form.deref(), &"form_body".to_string());
130+
}
131+
132+
#[test]
133+
fn test_form_body_deref_mut() {
134+
let mut form = FormBody::<String>("form_body".to_string());
135+
assert_eq!(form.deref_mut(), &mut "form_body".to_string());
136+
}
137+
138+
#[test]
139+
fn test_form_body_to_request_body() {
140+
let mut components = Components::default();
141+
let request_body = FormBody::<String>::to_request_body(&mut components);
142+
assert_json_eq!(
143+
request_body,
144+
json!({
145+
"description": "Extract form format data from request.",
146+
"content": {
147+
"application/x-www-form-urlencoded": {
148+
"schema": {
149+
"type": "string"
150+
}
151+
},
152+
"multipart/*": {
153+
"schema": {
154+
"type": "string"
155+
}
156+
}
157+
}
158+
})
159+
);
160+
}
161+
162+
#[test]
163+
fn test_form_body_debug() {
164+
let form = FormBody::<String>("form_body".to_string());
165+
assert_eq!(format!("{:?}", form), r#""form_body""#);
166+
}
167+
168+
#[test]
169+
fn test_form_body_display() {
170+
let form = FormBody::<String>("form_body".to_string());
171+
assert_eq!(format!("{}", form), "form_body");
172+
}
173+
174+
#[test]
175+
fn test_form_body_metadata() {
176+
let metadata = FormBody::<String>::metadata();
177+
assert_eq!("", metadata.name);
178+
}
179+
180+
#[tokio::test]
181+
async fn test_form_body_extract_with_arg() {
182+
let map = BTreeMap::from_iter([("key", "value")]);
183+
let mut req = TestClient::post("http://127.0.0.1:5800/").form(&map).build();
184+
let result = FormBody::<BTreeMap<&str, &str>>::extract_with_arg(&mut req, "key").await;
185+
assert_eq!("value", result.unwrap().0["key"]);
186+
}
187+
188+
#[test]
189+
fn test_form_body_register() {
190+
let mut components = Components::new();
191+
let mut operation = Operation::new();
192+
FormBody::<String>::register(&mut components, &mut operation, "arg");
193+
194+
assert_json_eq!(
195+
operation,
196+
json!({
197+
"requestBody": {
198+
"content": {
199+
"application/x-www-form-urlencoded": {
200+
"schema": {
201+
"type": "string"
202+
}
203+
},
204+
"multipart/*": {
205+
"schema": {
206+
"type": "string"
207+
}
208+
}
209+
},
210+
"description": "Extract form format data from request."
211+
},
212+
"responses": {}
213+
})
214+
);
215+
}
216+
}

crates/oapi/src/extract/payload/json.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,101 @@ where
101101
operation.request_body = Some(request_body);
102102
}
103103
}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use std::collections::BTreeMap;
108+
109+
use assert_json_diff::assert_json_eq;
110+
use salvo_core::test::TestClient;
111+
use serde_json::json;
112+
113+
use super::*;
114+
115+
#[test]
116+
fn test_json_body_into_inner() {
117+
let form = JsonBody::<String>("json_body".to_string());
118+
assert_eq!(form.into_inner(), "json_body".to_string());
119+
}
120+
121+
#[test]
122+
fn test_json_body_deref() {
123+
let form = JsonBody::<String>("json_body".to_string());
124+
assert_eq!(form.deref(), &"json_body".to_string());
125+
}
126+
127+
#[test]
128+
fn test_json_body_deref_mut() {
129+
let mut form = JsonBody::<String>("json_body".to_string());
130+
assert_eq!(form.deref_mut(), &mut "json_body".to_string());
131+
}
132+
133+
#[test]
134+
fn test_json_body_to_request_body() {
135+
let mut components = Components::default();
136+
let request_body = JsonBody::<String>::to_request_body(&mut components);
137+
assert_json_eq!(
138+
request_body,
139+
json!({
140+
"description": "Extract json format data from request.",
141+
"content": {
142+
"application/json": {
143+
"schema": {
144+
"type": "string"
145+
}
146+
}
147+
}
148+
})
149+
);
150+
}
151+
152+
#[test]
153+
fn test_json_body_debug() {
154+
let form = JsonBody::<String>("json_body".to_string());
155+
assert_eq!(format!("{:?}", form), r#""json_body""#);
156+
}
157+
158+
#[test]
159+
fn test_json_body_display() {
160+
let form = JsonBody::<String>("json_body".to_string());
161+
assert_eq!(format!("{}", form), "json_body");
162+
}
163+
164+
#[test]
165+
fn test_json_body_metadata() {
166+
let metadata = JsonBody::<String>::metadata();
167+
assert_eq!("", metadata.name);
168+
}
169+
170+
#[tokio::test]
171+
async fn test_json_body_extract_with_arg() {
172+
let map = BTreeMap::from_iter([("key", "value")]);
173+
let mut req = TestClient::post("http://127.0.0.1:5800/").json(&map).build();
174+
let result = JsonBody::<BTreeMap<&str, &str>>::extract_with_arg(&mut req, "key").await;
175+
assert_eq!("value", result.unwrap().0["key"]);
176+
}
177+
178+
#[test]
179+
fn test_json_body_register() {
180+
let mut components = Components::new();
181+
let mut operation = Operation::new();
182+
JsonBody::<String>::register(&mut components, &mut operation, "arg");
183+
184+
assert_json_eq!(
185+
operation,
186+
json!({
187+
"requestBody": {
188+
"content": {
189+
"application/json": {
190+
"schema": {
191+
"type": "string"
192+
}
193+
}
194+
},
195+
"description": "Extract json format data from request."
196+
},
197+
"responses": {}
198+
})
199+
);
200+
}
201+
}

0 commit comments

Comments
 (0)