Skip to content

Commit

Permalink
test(error): expect api error (#222)
Browse files Browse the repository at this point in the history
* test(error): expect api error

* test(error): allow dead_code and track_caller

* build: allow dead code for tests
  • Loading branch information
EdJoPaTo authored Sep 19, 2024
1 parent 91ac379 commit bb6e227
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
14 changes: 5 additions & 9 deletions src/client_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,10 @@ mod async_tests {
.create_async()
.await;
let api = AsyncApi::new_url(server.url());

if let Err(Error::Api(error)) = dbg!(api.send_message(&params).await) {
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
} else {
panic!("API Error expected");
}
let error = api.send_message(&params).await.unwrap_err().unwrap_api();
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
}
}
16 changes: 6 additions & 10 deletions src/client_ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,11 @@ mod tests {
.with_body(response_string)
.create();
let api = Api::new_url(server.url());

if let Err(Error::Api(error)) = dbg!(api.send_message(&params)) {
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
} else {
panic!("API Error expected");
}
let error = api.send_message(&params).unwrap_err().unwrap_api();
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
}

#[test]
Expand Down Expand Up @@ -350,7 +346,7 @@ mod tests {
.create();
let api = Api::new_url(server.url());

let response = api.close().err();
let response = api.close().unwrap_err();

let json = serde_json::to_string(&response).unwrap();
assert_eq!(response_string, json);
Expand Down
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ pub enum Error {
#[error("Encode Error {0}")]
Encode(String),
}

impl Error {
#[cfg(test)]
#[track_caller]
pub(crate) fn unwrap_api(self) -> ErrorResponse {
if let Self::Api(api) = self {
api
} else {
panic!("API Error expected: {self}");
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(test, allow(dead_code))]

#[cfg(feature = "async-http-client")]
pub use reqwest;
Expand Down

0 comments on commit bb6e227

Please sign in to comment.