| 3 | |
| 4 | #[wstd::test] |
| 5 | async fn main() -> Result<(), Box<dyn Error>> { |
| 6 | let request = Request::post("https://postman-echo.com/post") |
| 7 | .header( |
| 8 | "content-type", |
| 9 | HeaderValue::from_str("application/json; charset=utf-8")?, |
| 10 | ) |
| 11 | .body("{\"test\": \"data\"}")?; |
| 12 | |
| 13 | let response = Client::new().send(request).await?; |
| 14 | |
| 15 | let content_type = response |
| 16 | .headers() |
| 17 | .get("Content-Type") |
| 18 | .ok_or("response expected to have Content-Type header")?; |
| 19 | assert_eq!(content_type, "application/json; charset=utf-8"); |
| 20 | |
| 21 | let mut body = response.into_body(); |
| 22 | let val: serde_json::Value = body.json().await?; |
| 23 | |
| 24 | let body_url = val |
| 25 | .get("url") |
| 26 | .ok_or("body json has url")? |
| 27 | .as_str() |
| 28 | .ok_or("body json url is str")?; |
| 29 | assert!( |
| 30 | body_url.contains("postman-echo.com/post"), |
| 31 | "expected body url to contain the authority and path, got: {body_url}" |
| 32 | ); |
| 33 | |
| 34 | let posted_json = val |
| 35 | .get("json") |
| 36 | .ok_or("body json has 'json' key")? |
| 37 | .as_object() |
| 38 | .ok_or_else(|| format!("body json 'json' is object. got {val:?}"))?; |
| 39 | |
| 40 | assert_eq!(posted_json.len(), 1); |
| 41 | assert_eq!( |
| 42 | posted_json |
| 43 | .get("test") |
| 44 | .ok_or("returned json has 'test' key")? |
| 45 | .as_str() |
| 46 | .ok_or("returned json 'test' key should be str value")?, |
| 47 | "data" |
| 48 | ); |
| 49 | |
| 50 | Ok(()) |
| 51 | } |