Skip to content

Commit c891308

Browse files
committed
test(aggregator-client): add a minimal post test
1 parent 7ad948b commit c891308

File tree

1 file changed

+126
-47
lines changed
  • internal/mithril-aggregator-client/src

1 file changed

+126
-47
lines changed

internal/mithril-aggregator-client/src/client.rs

Lines changed: 126 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,65 +72,144 @@ mod tests {
7272

7373
use super::*;
7474

75-
#[derive(Debug, Eq, PartialEq, serde::Deserialize)]
76-
struct TestResponse {
77-
foo: String,
78-
bar: i32,
79-
}
75+
fn setup_server_and_client() -> (MockServer, AggregatorClient) {
76+
let server = MockServer::start();
77+
let client = AggregatorClient::builder(server.base_url())
78+
.with_logger(TestLogger::stdout())
79+
.build()
80+
.unwrap();
8081

81-
struct TestGetQuery;
82+
(server, client)
83+
}
8284

83-
#[async_trait::async_trait]
84-
impl AggregatorQuery for TestGetQuery {
85-
type Response = TestResponse;
86-
type Body = ();
85+
mod get {
86+
use super::*;
8787

88-
fn method() -> QueryMethod {
89-
QueryMethod::Get
88+
#[derive(Debug, Eq, PartialEq, serde::Deserialize)]
89+
struct TestResponse {
90+
foo: String,
91+
bar: i32,
9092
}
9193

92-
fn route(&self) -> String {
93-
"/dummy-route".to_string()
94-
}
94+
struct TestGetQuery;
9595

96-
async fn handle_response(
97-
&self,
98-
context: QueryContext,
99-
) -> AggregatorClientResult<Self::Response> {
100-
match context.response.status() {
101-
StatusCode::OK => context
102-
.response
103-
.json::<TestResponse>()
104-
.await
105-
.map_err(|err| AggregatorClientError::JsonParseFailed(anyhow!(err))),
106-
_ => Err(context.unhandled_status_code().await),
96+
#[async_trait::async_trait]
97+
impl AggregatorQuery for TestGetQuery {
98+
type Response = TestResponse;
99+
type Body = ();
100+
101+
fn method() -> QueryMethod {
102+
QueryMethod::Get
103+
}
104+
105+
fn route(&self) -> String {
106+
"/dummy-get-route".to_string()
107+
}
108+
109+
async fn handle_response(
110+
&self,
111+
context: QueryContext,
112+
) -> AggregatorClientResult<Self::Response> {
113+
match context.response.status() {
114+
StatusCode::OK => context
115+
.response
116+
.json::<TestResponse>()
117+
.await
118+
.map_err(|err| AggregatorClientError::JsonParseFailed(anyhow!(err))),
119+
_ => Err(context.unhandled_status_code().await),
120+
}
107121
}
108122
}
123+
124+
#[tokio::test]
125+
async fn test_minimal_get_query() {
126+
let (server, client) = setup_server_and_client();
127+
server.mock(|when, then| {
128+
when.method(httpmock::Method::GET).path("/dummy-get-route");
129+
then.status(200).body(r#"{"foo": "bar", "bar": 123}"#);
130+
});
131+
132+
let response = client.send(TestGetQuery).await.unwrap();
133+
134+
assert_eq!(
135+
response,
136+
TestResponse {
137+
foo: "bar".to_string(),
138+
bar: 123,
139+
}
140+
)
141+
}
109142
}
110143

111-
#[tokio::test]
112-
async fn test_minimal_query() {
113-
let server = MockServer::start();
114-
server.mock(|when, then| {
115-
when.method(httpmock::Method::GET).path(TestGetQuery.route());
116-
then.status(200).body(r#"{"foo": "bar", "bar": 123}"#);
117-
});
118-
119-
let aggregator_endpoint = Url::parse(&server.url("/")).unwrap();
120-
let client = AggregatorClient {
121-
aggregator_endpoint,
122-
client: reqwest::Client::new(),
123-
logger: TestLogger::stdout(),
124-
};
144+
mod post {
145+
use super::*;
125146

126-
let response = client.send(TestGetQuery).await.unwrap();
147+
#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize)]
148+
struct TestBody {
149+
pika: String,
150+
chu: u8,
151+
}
152+
153+
struct TestPostQuery {
154+
body: TestBody,
155+
}
156+
157+
#[async_trait::async_trait]
158+
impl AggregatorQuery for TestPostQuery {
159+
type Response = ();
160+
type Body = TestBody;
161+
162+
fn method() -> QueryMethod {
163+
QueryMethod::Post
164+
}
165+
166+
fn route(&self) -> String {
167+
"/dummy-post-route".to_string()
168+
}
169+
170+
fn body(&self) -> Option<Self::Body> {
171+
Some(self.body.clone())
172+
}
127173

128-
assert_eq!(
129-
response,
130-
TestResponse {
131-
foo: "bar".to_string(),
132-
bar: 123,
174+
async fn handle_response(
175+
&self,
176+
context: QueryContext,
177+
) -> AggregatorClientResult<Self::Response> {
178+
match context.response.status() {
179+
StatusCode::CREATED => Ok(()),
180+
_ => Err(context.unhandled_status_code().await),
181+
}
133182
}
134-
)
183+
}
184+
185+
#[tokio::test]
186+
async fn test_minimal_post_query() {
187+
let (server, client) = setup_server_and_client();
188+
server.mock(|when, then| {
189+
when.method(httpmock::Method::POST)
190+
.path("/dummy-post-route")
191+
.header("content-type", "application/json")
192+
.body(
193+
serde_json::to_string(&TestBody {
194+
pika: "miaouss".to_string(),
195+
chu: 5,
196+
})
197+
.unwrap(),
198+
);
199+
then.status(201);
200+
});
201+
202+
let response = client
203+
.send(TestPostQuery {
204+
body: TestBody {
205+
pika: "miaouss".to_string(),
206+
chu: 5,
207+
},
208+
})
209+
.await
210+
.unwrap();
211+
212+
assert_eq!(response, ())
213+
}
135214
}
136215
}

0 commit comments

Comments
 (0)