-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.rs
170 lines (130 loc) · 4.42 KB
/
test.rs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use reqwest::ClientBuilder;
use retroqwest::RetroqwestError;
use serde::{Deserialize, Serialize};
use wiremock::matchers::{body_json, query_param};
use wiremock::{
matchers::{method, path},
Mock, ResponseTemplate,
};
#[derive(Deserialize, Serialize, Clone)]
pub struct HttpBinResponse {
pub url: String,
}
#[retroqwest::retroqwest]
pub trait HttpBin {
#[http::get("/anything")]
async fn get_anything(&self) -> Result<HttpBinResponse, RetroqwestError>;
#[http::get("/anything/{name}")]
async fn get_by_name(&self, name: String) -> Result<HttpBinResponse, RetroqwestError>;
#[http::post("/anything/{name}")]
async fn post_to_name(
&self,
name: String,
#[query] q: bool,
#[json] body: &HttpBinResponse,
) -> Result<HttpBinResponse, RetroqwestError>;
#[http::get("/anything")]
async fn get_multiple_queries(
&self,
#[query] q: bool,
#[query] q2: i32,
) -> Result<HttpBinResponse, RetroqwestError>;
}
impl HttpBinClient {
pub fn new(base_uri: String) -> Result<Self, RetroqwestError> {
Self::from_builder(base_uri, ClientBuilder::default())
}
}
// This method allows for better code completion
// since `impl HttpBin` is better than the generated struct...
fn build_client(uri: String) -> Result<impl HttpBin, retroqwest::RetroqwestError> {
Ok(HttpBinClient::new(uri)?)
}
#[tokio::test]
async fn test_simple_gets() -> Result<(), Box<dyn std::error::Error>> {
let server = wiremock::MockServer::start().await;
Mock::given(method("GET"))
.and(path("/anything"))
.respond_with(ResponseTemplate::new(200).set_body_json(HttpBinResponse {
url: "test".to_string(),
}))
.mount(&server)
.await;
let client = build_client(server.uri())?;
let result: HttpBinResponse = client.get_anything().await?;
assert_eq!(result.url, "test".to_string());
Ok(())
}
#[tokio::test]
async fn test_gets_with_vars_in_path() -> Result<(), Box<dyn std::error::Error>> {
let server = wiremock::MockServer::start().await;
Mock::given(method("GET"))
.and(path("/anything/test"))
.respond_with(ResponseTemplate::new(200).set_body_json(HttpBinResponse {
url: "test".to_string(),
}))
.mount(&server)
.await;
let client = build_client(server.uri())?;
let result: HttpBinResponse = client.get_by_name("test".to_string()).await?;
assert_eq!(result.url, "test".to_string());
Ok(())
}
#[tokio::test]
async fn test_complex_post() -> Result<(), Box<dyn std::error::Error>> {
let server = wiremock::MockServer::start().await;
let body = HttpBinResponse {
url: "test".to_string(),
};
Mock::given(method("POST"))
.and(path("/anything/test"))
.and(query_param("q", "true"))
.and(body_json(body.clone()))
.respond_with(ResponseTemplate::new(200).set_body_json(HttpBinResponse {
url: "posted".to_string(),
}))
.mount(&server)
.await;
let client = build_client(server.uri())?;
let result: HttpBinResponse = client.post_to_name("test".to_string(), true, &body).await?;
assert_eq!(result.url, "posted".to_string());
Ok(())
}
#[tokio::test]
async fn test_multiple_query_params() -> Result<(), Box<dyn std::error::Error>> {
let server = wiremock::MockServer::start().await;
Mock::given(method("GET"))
.and(path("/anything"))
.and(query_param("q", "true"))
.and(query_param("q2", "3"))
.respond_with(ResponseTemplate::new(200).set_body_json(HttpBinResponse {
url: "multi-queries".to_string(),
}))
.mount(&server)
.await;
let client = build_client(server.uri())?;
let r = client.get_multiple_queries(true, 3).await;
if r.is_err() {
dbg!(server.received_requests().await);
}
let result: HttpBinResponse = r?;
assert_eq!(result.url, "multi-queries".to_string());
Ok(())
}
#[tokio::test]
async fn test_get_errors() -> Result<(), Box<dyn std::error::Error>> {
let server = wiremock::MockServer::start().await;
Mock::given(method("GET"))
.and(path("/anything/test"))
.respond_with(ResponseTemplate::new(400))
.mount(&server)
.await;
let client = build_client(server.uri())?;
let result = client
.get_by_name("test".to_string())
.await
.err()
.unwrap();
assert!(matches!(result, RetroqwestError::ResponseError {..}));
Ok(())
}