|
| 1 | +use std::{any::TypeId, error::Error}; |
| 2 | + |
| 3 | +use rmcp::{ |
| 4 | + service::ClientInitializeError, |
| 5 | + transport::{ |
| 6 | + AuthError, DynamicTransportError, |
| 7 | + streamable_http_client::{AuthRequiredError, InsufficientScopeError, StreamableHttpError}, |
| 8 | + }, |
| 9 | +}; |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +type TestHttpError = StreamableHttpError<std::io::Error>; |
| 13 | + |
| 14 | +#[derive(Debug, Error)] |
| 15 | +#[error("outer transport wrapper")] |
| 16 | +struct OuterError(#[source] TestHttpError); |
| 17 | + |
| 18 | +fn initialization_error(error: impl Error + Send + Sync + 'static) -> ClientInitializeError { |
| 19 | + ClientInitializeError::TransportError { |
| 20 | + error: DynamicTransportError::from_parts( |
| 21 | + "test transport", |
| 22 | + TypeId::of::<()>(), |
| 23 | + Box::new(error), |
| 24 | + ), |
| 25 | + context: "initialize".into(), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn classifies_local_authorization_required() { |
| 31 | + let error = TestHttpError::Auth(AuthError::AuthorizationRequired); |
| 32 | + |
| 33 | + assert!(initialization_error(error).is_authorization_required()); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn classifies_http_authorization_challenge() { |
| 38 | + let error = |
| 39 | + TestHttpError::AuthRequired(AuthRequiredError::new("Bearer realm=\"mcp\"".to_owned())); |
| 40 | + |
| 41 | + assert!(initialization_error(error).is_authorization_required()); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn classifies_authorization_required_through_multiple_sources() { |
| 46 | + let error = OuterError(TestHttpError::Auth(AuthError::AuthorizationRequired)); |
| 47 | + |
| 48 | + assert!(initialization_error(error).is_authorization_required()); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn does_not_classify_unrelated_transport_errors() { |
| 53 | + let closed = TestHttpError::TransportChannelClosed; |
| 54 | + let refresh = TestHttpError::Auth(AuthError::TokenRefreshFailed("timeout".to_owned())); |
| 55 | + let scope = TestHttpError::InsufficientScope(InsufficientScopeError::new( |
| 56 | + "Bearer error=\"insufficient_scope\"".to_owned(), |
| 57 | + Some("admin".to_owned()), |
| 58 | + )); |
| 59 | + |
| 60 | + assert!(!initialization_error(closed).is_authorization_required()); |
| 61 | + assert!(!initialization_error(refresh).is_authorization_required()); |
| 62 | + assert!(!initialization_error(scope).is_authorization_required()); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn does_not_classify_non_transport_initialization_errors() { |
| 67 | + assert!(!ClientInitializeError::Cancelled.is_authorization_required()); |
| 68 | + assert!( |
| 69 | + !ClientInitializeError::ConnectionClosed("server closed the connection".to_owned()) |
| 70 | + .is_authorization_required() |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn http_challenge_remains_available_as_an_error_source() { |
| 76 | + let error = |
| 77 | + TestHttpError::AuthRequired(AuthRequiredError::new("Bearer realm=\"mcp\"".to_owned())); |
| 78 | + |
| 79 | + let source = error.source().expect("auth challenge should be a source"); |
| 80 | + let challenge = source |
| 81 | + .downcast_ref::<AuthRequiredError>() |
| 82 | + .expect("source should retain the challenge type"); |
| 83 | + |
| 84 | + assert_eq!(challenge.www_authenticate_header, "Bearer realm=\"mcp\""); |
| 85 | +} |
0 commit comments