Skip to content

Commit 693385b

Browse files
committed
Add test for 404 error in mithril-client aggregator client
1 parent 2cdae3a commit 693385b

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

mithril-client/src/aggregator_client.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ impl AggregatorHTTPClient {
257257

258258
Err(self.handle_api_error(response.headers()).await)
259259
}
260-
StatusCode::NOT_FOUND => Err(AggregatorClientError::RemoteServerLogical(anyhow!(
261-
"Url='{url}' not found"
262-
))),
260+
StatusCode::NOT_FOUND => Err(Self::not_found_error(url)),
263261
status_code if status_code.is_client_error() => {
264262
Err(Self::remote_logical_error(response).await)
265263
}
@@ -301,9 +299,7 @@ impl AggregatorHTTPClient {
301299

302300
Err(self.handle_api_error(response.headers()).await)
303301
}
304-
StatusCode::NOT_FOUND => Err(AggregatorClientError::RemoteServerLogical(anyhow!(
305-
"Url='{url} not found"
306-
))),
302+
StatusCode::NOT_FOUND => Err(Self::not_found_error(url)),
307303
status_code if status_code.is_client_error() => {
308304
Err(Self::remote_logical_error(response).await)
309305
}
@@ -339,6 +335,10 @@ impl AggregatorHTTPClient {
339335
}
340336
}
341337

338+
fn not_found_error(url: Url) -> AggregatorClientError {
339+
AggregatorClientError::RemoteServerLogical(anyhow!("Url='{url}' not found"))
340+
}
341+
342342
async fn remote_logical_error(response: Response) -> AggregatorClientError {
343343
let status_code = response.status();
344344
let client_error = response
@@ -570,6 +570,38 @@ mod tests {
570570
assert_error_eq!(post_content_error, expected_error);
571571
}
572572

573+
#[tokio::test]
574+
async fn test_client_handle_404_not_found_error() {
575+
let client_error = ClientError::new("label", "message");
576+
577+
let (aggregator, client) = setup_server_and_client();
578+
aggregator.mock(|_when, then| {
579+
then.status(StatusCode::NOT_FOUND.as_u16())
580+
.json_body_obj(&client_error);
581+
});
582+
583+
let expected_error = AggregatorHTTPClient::not_found_error(
584+
Url::parse(&format!(
585+
"{}/{}",
586+
aggregator.base_url(),
587+
AggregatorRequest::ListCertificates.route()
588+
))
589+
.unwrap(),
590+
);
591+
592+
let get_content_error = client
593+
.get_content(AggregatorRequest::ListCertificates)
594+
.await
595+
.unwrap_err();
596+
assert_error_eq!(get_content_error, expected_error);
597+
598+
let post_content_error = client
599+
.post_content(AggregatorRequest::ListCertificates)
600+
.await
601+
.unwrap_err();
602+
assert_error_eq!(post_content_error, expected_error);
603+
}
604+
573605
#[tokio::test]
574606
async fn test_client_handle_5xx_errors() {
575607
let server_error = ServerError::new("message");

0 commit comments

Comments
 (0)