1use reqwest::header::{HeaderMap, AUTHORIZATION, CONTENT_TYPE};
2
3use crate::http::HttpClient;
4
5pub struct Crux {
9 host: String,
10 port: String,
11 headers: HeaderMap,
12}
13
14impl Crux {
15 pub fn new(host: &str, port: &str) -> Self {
17 let mut headers = HeaderMap::new();
18 headers.insert(CONTENT_TYPE, "application/edn".parse().unwrap());
19
20 Self {
21 host: host.to_string(),
22 port: port.to_string(),
23 headers,
24 }
25 }
26
27 pub fn with_authorization(mut self, authorization: &str) -> Self {
29 self.headers
30 .insert(AUTHORIZATION, authorization.parse().unwrap());
31 self
32 }
33
34 #[cfg(not(test))]
35 fn uri(&self) -> String {
36 format!("http://{}:{}", self.host, self.port)
37 }
38
39 #[cfg(test)]
40 fn uri(&self) -> String {
41 use mockito::server_url;
42 server_url()
43 }
44
45 #[cfg(not(feature = "async"))]
47 pub fn http_client(&mut self) -> HttpClient {
48 HttpClient {
49 client: reqwest::blocking::Client::new(),
50 uri: self.uri().clone(),
51 headers: self.headers.clone(),
52 }
53 }
54
55 #[cfg(feature = "async")]
56 pub fn http_client(&mut self) -> HttpClient {
57 HttpClient {
58 client: reqwest::Client::new(),
59 uri: self.uri().clone(),
60 headers: self.headers.clone(),
61 }
62 }
63
64 #[cfg(feature = "mock")]
66 pub fn http_mock(&mut self) -> HttpClient {
67 use mockito::server_url;
68
69 self.headers
70 .insert(CONTENT_TYPE, "application/edn".parse().unwrap());
71 HttpClient {
72 client: reqwest::blocking::Client::new(),
73 uri: server_url(),
74 headers: self.headers.clone(),
75 }
76 }
77}
78
79#[cfg(test)]
80mod test {
81 use super::*;
82
83 #[test]
84 fn new() {
85 let actual = Crux::new("host", "port");
86 let mut headers = HeaderMap::new();
87 headers.insert(CONTENT_TYPE, "application/edn".parse().unwrap());
88
89 let expected = Crux {
90 host: String::from("host"),
91 port: String::from("port"),
92 headers,
93 };
94
95 assert_eq!(actual.host, expected.host);
96 assert_eq!(actual.port, expected.port);
97 assert_eq!(actual.headers, expected.headers);
98 }
99
100 #[test]
101 fn authorization() {
102 let crux = Crux::new("host", "port").with_authorization("auth");
103 let mut headers = HeaderMap::new();
104 headers.insert(AUTHORIZATION, "auth".parse().unwrap());
105 headers.insert(CONTENT_TYPE, "application/edn".parse().unwrap());
106
107 assert_eq!(crux.headers, headers);
108 }
109
110 #[test]
111 fn uri() {
112 let crux = Crux::new("localhost", "1234");
113
114 assert_eq!(crux.uri(), "http://127.0.0.1:1234")
115 }
116
117 #[test]
118 fn http_client() {
119 let mut headers = HeaderMap::new();
120 headers.insert(AUTHORIZATION, "auth".parse().unwrap());
121 headers.insert(CONTENT_TYPE, "application/edn".parse().unwrap());
122
123 let actual = Crux::new("127.0.0.1", "1234")
124 .with_authorization("auth")
125 .http_client();
126 let expected = HttpClient {
127 client: reqwest::blocking::Client::new(),
128 uri: "http://127.0.0.1:1234".to_string(),
129 headers: headers,
130 };
131
132 assert_eq!(actual.uri, expected.uri);
133 assert_eq!(actual.headers, expected.headers);
134 }
135}