Skip to content

Commit

Permalink
test: Add unit tests for GatewayError handling in InferenceGatewayClient
Browse files Browse the repository at this point in the history
Signed-off-by: Eden Reich <[email protected]>
  • Loading branch information
edenreich committed Jan 28, 2025
1 parent fe85dcb commit 6b7e6d4
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,56 @@ mod tests {
use super::*;
use mockito::{Matcher, Server};

#[test]
fn test_gateway_errors() {
let mut server = Server::new();

// Test unauthorized error
let unauthorized_mock = server
.mock("GET", "/llms")
.with_status(401)
.with_header("content-type", "application/json")
.with_body(r#"{"error":"Invalid token"}"#)
.create();

let client = InferenceGatewayClient::new(&server.url());
match client.list_models() {
Err(GatewayError::Unauthorized(msg)) => assert_eq!(msg, "Invalid token"),
_ => panic!("Expected Unauthorized error"),
}
unauthorized_mock.assert();

// Test bad request error
let bad_request_mock = server
.mock("GET", "/llms")
.with_status(400)
.with_header("content-type", "application/json")
.with_body(r#"{"error":"Invalid provider"}"#)
.create();

match client.list_models() {
Err(GatewayError::BadRequest(msg)) => assert_eq!(msg, "Invalid provider"),
_ => panic!("Expected BadRequest error"),
}
bad_request_mock.assert();

// Test internal server error
let internal_error_mock = server
.mock("GET", "/llms")
.with_status(500)
.with_header("content-type", "application/json")
.with_body(r#"{"error":"Internal server error occurred"}"#)
.create();

match client.list_models() {
Err(GatewayError::InternalError(msg)) => {
assert_eq!(msg, "Internal server error occurred")
}
_ => panic!("Expected InternalError error"),
}
internal_error_mock.assert();
}

#[test]
fn test_provider_serialization() {
let providers = vec![
Expand Down

0 comments on commit 6b7e6d4

Please sign in to comment.