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
#![warn(clippy::pedantic)]
#![deny(warnings, missing_debug_implementations)]
use reqwest::RequestBuilder;
use serde::Serialize;
use serde_json::to_vec_pretty;
pub trait PrettyJson<T>: Sized
where
T: Serialize + ?Sized,
{
fn pretty_json(self, json: &T) -> Self;
}
impl<T> PrettyJson<T> for RequestBuilder
where
T: Serialize + ?Sized,
{
fn pretty_json(self, json: &T) -> Self {
let builder = self.json(json);
match to_vec_pretty(json) {
Ok(body) => builder.body(body),
Err(_) => builder,
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use reqwest::{Client, StatusCode};
use serde_json::{to_string, to_string_pretty, to_value, Value};
use super::*;
#[test]
fn add_pretty_json() {
let mut json_data = HashMap::new();
json_data.insert("foo", vec![1, 2, 3]);
let body_should_be = to_string_pretty(&json_data).unwrap();
let body_shouldnt_be = to_string(&json_data).unwrap();
let value = to_value(&json_data).unwrap();
let client = Client::new();
let mut response = client
.post("http://httpbin.org/post")
.pretty_json(&json_data)
.send()
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let result: Value = response.json().unwrap();
assert_eq!(result["data"], body_should_be);
assert_ne!(result["data"], body_shouldnt_be);
assert_eq!(result["headers"]["Content-Type"], "application/json");
assert_eq!(result["json"], value);
}
}