chore: consolidate repeated rmcp tests (#931)
This commit is contained in:
parent
4b9bea7e7d
commit
e1af378949
4 changed files with 306 additions and 332 deletions
|
|
@ -233,6 +233,8 @@ pub trait AsRequestContext {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rstest::rstest;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, JsonSchema)]
|
||||
|
|
@ -245,46 +247,44 @@ mod tests {
|
|||
value: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_type_handles_primitive() {
|
||||
let schema = schema_for_type::<i32>();
|
||||
#[rstest]
|
||||
#[case::primitive(schema_for_type::<i32>, "integer")]
|
||||
#[case::array(schema_for_type::<Vec<i32>>, "array")]
|
||||
#[case::struct_object(schema_for_type::<TestObject>, "object")]
|
||||
fn schema_for_type_sets_expected_root_type(
|
||||
#[case] schema_fn: fn() -> Arc<JsonObject>,
|
||||
#[case] expected_type: &str,
|
||||
) {
|
||||
let schema = schema_fn();
|
||||
|
||||
assert_eq!(schema.get("type"), Some(&serde_json::json!("integer")));
|
||||
assert_eq!(schema.get("type"), Some(&serde_json::json!(expected_type)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_type_handles_array() {
|
||||
fn schema_for_type_sets_array_item_type() {
|
||||
let schema = schema_for_type::<Vec<i32>>();
|
||||
let items = schema.get("items").and_then(|v| v.as_object()).unwrap();
|
||||
|
||||
assert_eq!(schema.get("type"), Some(&serde_json::json!("array")));
|
||||
let items = schema.get("items").and_then(|v| v.as_object());
|
||||
assert_eq!(
|
||||
items.unwrap().get("type"),
|
||||
Some(&serde_json::json!("integer"))
|
||||
);
|
||||
assert_eq!(items.get("type"), Some(&serde_json::json!("integer")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_type_handles_struct() {
|
||||
fn schema_for_type_sets_struct_properties() {
|
||||
let schema = schema_for_type::<TestObject>();
|
||||
let properties = schema
|
||||
.get("properties")
|
||||
.and_then(|v| v.as_object())
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(schema.get("type"), Some(&serde_json::json!("object")));
|
||||
let properties = schema.get("properties").and_then(|v| v.as_object());
|
||||
assert!(properties.unwrap().contains_key("value"));
|
||||
assert!(properties.contains_key("value"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_type_caches_primitive_types() {
|
||||
let schema1 = schema_for_type::<i32>();
|
||||
let schema2 = schema_for_type::<i32>();
|
||||
|
||||
assert!(Arc::ptr_eq(&schema1, &schema2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_type_caches_struct_types() {
|
||||
let schema1 = schema_for_type::<TestObject>();
|
||||
let schema2 = schema_for_type::<TestObject>();
|
||||
#[rstest]
|
||||
#[case::primitive(schema_for_type::<i32>)]
|
||||
#[case::struct_object(schema_for_type::<TestObject>)]
|
||||
fn test_schema_for_type_caches_schemas(#[case] schema_fn: fn() -> Arc<JsonObject>) {
|
||||
let schema1 = schema_fn();
|
||||
let schema2 = schema_fn();
|
||||
|
||||
assert!(Arc::ptr_eq(&schema1, &schema2));
|
||||
}
|
||||
|
|
@ -305,51 +305,36 @@ mod tests {
|
|||
assert!(Arc::ptr_eq(&schema, &cloned));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_output_rejects_primitive() {
|
||||
let result = schema_for_output::<i32>();
|
||||
assert!(result.is_err(),);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_output_accepts_object() {
|
||||
let result = schema_for_output::<TestObject>();
|
||||
assert!(result.is_ok(),);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_output_strips_top_level_title() {
|
||||
let schema = schema_for_output::<TestObject>().unwrap();
|
||||
assert!(!schema.contains_key("title"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_output_strips_top_level_description() {
|
||||
let schema = schema_for_output::<TestObject>().unwrap();
|
||||
assert!(!schema.contains_key("description"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_input_rejects_primitive() {
|
||||
let result = schema_for_input::<i32>();
|
||||
#[rstest]
|
||||
#[case::output(schema_for_output::<i32>)]
|
||||
#[case::input(schema_for_input::<i32>)]
|
||||
fn test_schema_for_object_wrappers_reject_primitives(
|
||||
#[case] schema_fn: fn() -> Result<Arc<JsonObject>, String>,
|
||||
) {
|
||||
let result = schema_fn();
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_input_accepts_object() {
|
||||
let result = schema_for_input::<TestObject>();
|
||||
#[rstest]
|
||||
#[case::output(schema_for_output::<TestObject>)]
|
||||
#[case::input(schema_for_input::<TestObject>)]
|
||||
fn test_schema_for_object_wrappers_accept_objects(
|
||||
#[case] schema_fn: fn() -> Result<Arc<JsonObject>, String>,
|
||||
) {
|
||||
let result = schema_fn();
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_input_strips_top_level_title() {
|
||||
let schema = schema_for_input::<TestObject>().unwrap();
|
||||
assert!(!schema.contains_key("title"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_schema_for_input_strips_top_level_description() {
|
||||
let schema = schema_for_input::<TestObject>().unwrap();
|
||||
assert!(!schema.contains_key("description"));
|
||||
#[rstest]
|
||||
#[case::output_title(schema_for_output::<TestObject>, "title")]
|
||||
#[case::output_description(schema_for_output::<TestObject>, "description")]
|
||||
#[case::input_title(schema_for_input::<TestObject>, "title")]
|
||||
#[case::input_description(schema_for_input::<TestObject>, "description")]
|
||||
fn test_schema_for_object_wrappers_strip_top_level_metadata(
|
||||
#[case] schema_fn: fn() -> Result<Arc<JsonObject>, String>,
|
||||
#[case] field: &str,
|
||||
) {
|
||||
let schema = schema_fn().unwrap();
|
||||
assert!(!schema.contains_key(field));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1701,49 +1701,70 @@ impl ElicitationSchemaBuilder {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use anyhow::anyhow;
|
||||
use rstest::rstest;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_string_schema_serialization() {
|
||||
let schema = StringSchema::email().description("Email address");
|
||||
let json = serde_json::to_value(&schema).unwrap();
|
||||
|
||||
assert_eq!(json["type"], "string");
|
||||
assert_eq!(json["format"], "email");
|
||||
assert_eq!(json["description"], "Email address");
|
||||
fn string_schema_json() -> serde_json::Value {
|
||||
serde_json::to_value(StringSchema::email().description("Email address")).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_number_schema_serialization() {
|
||||
let schema = NumberSchema::new()
|
||||
.range(0.0, 100.0)
|
||||
.description("Percentage");
|
||||
let json = serde_json::to_value(&schema).unwrap();
|
||||
|
||||
assert_eq!(json["type"], "number");
|
||||
assert_eq!(json["minimum"], 0.0);
|
||||
assert_eq!(json["maximum"], 100.0);
|
||||
fn number_schema_json() -> serde_json::Value {
|
||||
serde_json::to_value(
|
||||
NumberSchema::new()
|
||||
.range(0.0, 100.0)
|
||||
.description("Percentage"),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_integer_schema_serialization() {
|
||||
let schema = IntegerSchema::new().range(0, 150);
|
||||
let json = serde_json::to_value(&schema).unwrap();
|
||||
|
||||
assert_eq!(json["type"], "integer");
|
||||
assert_eq!(json["minimum"], 0);
|
||||
assert_eq!(json["maximum"], 150);
|
||||
fn integer_schema_json() -> serde_json::Value {
|
||||
serde_json::to_value(IntegerSchema::new().range(0, 150)).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boolean_schema_serialization() {
|
||||
let schema = BooleanSchema::new().with_default(true);
|
||||
let json = serde_json::to_value(&schema).unwrap();
|
||||
fn boolean_schema_json() -> serde_json::Value {
|
||||
serde_json::to_value(BooleanSchema::new().with_default(true)).unwrap()
|
||||
}
|
||||
|
||||
assert_eq!(json["type"], "boolean");
|
||||
assert_eq!(json["default"], true);
|
||||
#[rstest]
|
||||
#[case::string_schema(
|
||||
string_schema_json,
|
||||
json!({
|
||||
"type": "string",
|
||||
"format": "email",
|
||||
"description": "Email address",
|
||||
})
|
||||
)]
|
||||
#[case::number_schema(
|
||||
number_schema_json,
|
||||
json!({
|
||||
"type": "number",
|
||||
"description": "Percentage",
|
||||
"minimum": 0.0,
|
||||
"maximum": 100.0,
|
||||
})
|
||||
)]
|
||||
#[case::integer_schema(
|
||||
integer_schema_json,
|
||||
json!({
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"maximum": 150,
|
||||
})
|
||||
)]
|
||||
#[case::boolean_schema(
|
||||
boolean_schema_json,
|
||||
json!({
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
})
|
||||
)]
|
||||
fn primitive_schema_serializes_to_expected_json(
|
||||
#[case] schema_json: fn() -> serde_json::Value,
|
||||
#[case] expected: serde_json::Value,
|
||||
) {
|
||||
assert_eq!(schema_json(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -2975,6 +2975,7 @@ mod tests {
|
|||
};
|
||||
|
||||
use oauth2::{AuthType, CsrfToken, HttpResponse, PkceCodeVerifier};
|
||||
use rstest::rstest;
|
||||
use url::Url;
|
||||
|
||||
use super::{
|
||||
|
|
@ -3348,44 +3349,35 @@ mod tests {
|
|||
|
||||
// -- header value parsing --
|
||||
|
||||
#[test]
|
||||
fn parse_auth_param_value_handles_quoted_string() {
|
||||
let fragment = r#""example", realm="foo""#;
|
||||
let parsed = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
assert_eq!(parsed.0, "example");
|
||||
assert_eq!(parsed.1, 9);
|
||||
}
|
||||
#[rstest]
|
||||
#[case::quoted_string(r#""example", realm="foo""#, "example", r#""example""#)]
|
||||
#[case::escaped_quotes_and_whitespace(
|
||||
r#" "a\"b\\c" ,next=value"#,
|
||||
r#"a"b\c"#,
|
||||
r#" "a\"b\\c""#
|
||||
)]
|
||||
#[case::token_values(" token,next", "token", " token")]
|
||||
#[case::semicolon_separated_tokens(
|
||||
r#" https://example.com/meta; error="invalid_token""#,
|
||||
"https://example.com/meta",
|
||||
" https://example.com/meta"
|
||||
)]
|
||||
#[case::semicolon_after_quoted_value(
|
||||
r#" "https://example.com/meta"; error="invalid_token""#,
|
||||
"https://example.com/meta",
|
||||
r#" "https://example.com/meta""#
|
||||
)]
|
||||
fn parse_auth_param_value_handles_supported_values(
|
||||
#[case] fragment: &str,
|
||||
#[case] expected_value: &str,
|
||||
#[case] expected_consumed_prefix: &str,
|
||||
) {
|
||||
let (value, consumed) = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
|
||||
#[test]
|
||||
fn parse_auth_param_value_handles_escaped_quotes_and_whitespace() {
|
||||
let fragment = r#" "a\"b\\c" ,next=value"#;
|
||||
let parsed = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
assert_eq!(parsed.0, r#"a"b\c"#);
|
||||
assert_eq!(parsed.1, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_auth_param_value_handles_token_values() {
|
||||
let fragment = " token,next";
|
||||
let parsed = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
assert_eq!(parsed.0, "token");
|
||||
assert_eq!(parsed.1, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_auth_param_value_handles_semicolon_separated_tokens() {
|
||||
let fragment = r#" https://example.com/meta; error="invalid_token""#;
|
||||
let parsed = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
assert_eq!(parsed.0, "https://example.com/meta");
|
||||
assert_eq!(&fragment[..parsed.1], " https://example.com/meta");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_auth_param_value_handles_semicolon_after_quoted_value() {
|
||||
let fragment = r#" "https://example.com/meta"; error="invalid_token""#;
|
||||
let parsed = AuthorizationManager::parse_next_header_value(fragment).unwrap();
|
||||
assert_eq!(parsed.0, "https://example.com/meta");
|
||||
assert_eq!(&fragment[..parsed.1], r#" "https://example.com/meta""#);
|
||||
assert_eq!(
|
||||
(value.as_str(), &fragment[..consumed]),
|
||||
(expected_value, expected_consumed_prefix)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -4010,104 +4002,113 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_authorization_response_issuer_accepts_match_and_missing_issuer() {
|
||||
#[rstest]
|
||||
#[case::matching_issuer(
|
||||
Some("https://auth.example.com"),
|
||||
false,
|
||||
Some("https://auth.example.com")
|
||||
)]
|
||||
#[case::missing_issuer_when_not_required(Some("https://auth.example.com"), false, None)]
|
||||
fn validate_authorization_response_issuer_accepts_valid_cases(
|
||||
#[case] expected_issuer: Option<&str>,
|
||||
#[case] require_issuer: bool,
|
||||
#[case] received_issuer: Option<&str>,
|
||||
) {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(
|
||||
&pkce,
|
||||
&csrf,
|
||||
Some("https://auth.example.com".to_string()),
|
||||
false,
|
||||
expected_issuer.map(str::to_owned),
|
||||
require_issuer,
|
||||
);
|
||||
|
||||
assert!(
|
||||
AuthorizationManager::validate_authorization_response_issuer(
|
||||
&state,
|
||||
Some("https://auth.example.com")
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(AuthorizationManager::validate_authorization_response_issuer(&state, None).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_authorization_response_issuer_requires_issuer_when_advertised() {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(
|
||||
&pkce,
|
||||
&csrf,
|
||||
Some("https://auth.example.com".to_string()),
|
||||
true,
|
||||
);
|
||||
|
||||
let error =
|
||||
AuthorizationManager::validate_authorization_response_issuer(&state, None).unwrap_err();
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
AuthError::AuthorizationServerMissingIssuer { expected_issuer }
|
||||
if expected_issuer == "https://auth.example.com"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_authorization_response_issuer_rejects_present_issuer_without_expected_issuer() {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(&pkce, &csrf, None, false);
|
||||
|
||||
let error = AuthorizationManager::validate_authorization_response_issuer(
|
||||
&state,
|
||||
Some("https://auth.example.com"),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, AuthError::AuthorizationFailed(message) if message.contains("expected issuer was not recorded"))
|
||||
AuthorizationManager::validate_authorization_response_issuer(&state, received_issuer)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_authorization_response_issuer_rejects_required_issuer_without_expected_issuer() {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(&pkce, &csrf, None, true);
|
||||
|
||||
let error =
|
||||
AuthorizationManager::validate_authorization_response_issuer(&state, None).unwrap_err();
|
||||
|
||||
assert!(
|
||||
matches!(error, AuthError::AuthorizationFailed(message) if message.contains("expected issuer was not recorded"))
|
||||
);
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum ExpectedIssuerError {
|
||||
Missing {
|
||||
expected_issuer: &'static str,
|
||||
},
|
||||
NotRecorded,
|
||||
Mismatch {
|
||||
expected_issuer: &'static str,
|
||||
received_issuer: &'static str,
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_authorization_response_issuer_rejects_mismatch() {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(
|
||||
&pkce,
|
||||
&csrf,
|
||||
Some("https://auth.example.com".to_string()),
|
||||
false,
|
||||
);
|
||||
|
||||
let error = AuthorizationManager::validate_authorization_response_issuer(
|
||||
&state,
|
||||
Some("https://evil.example.com"),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
AuthError::AuthorizationServerMismatch {
|
||||
fn assert_expected_issuer_error(error: AuthError, expected: ExpectedIssuerError) {
|
||||
match expected {
|
||||
ExpectedIssuerError::Missing { expected_issuer } => assert!(matches!(
|
||||
error,
|
||||
AuthError::AuthorizationServerMissingIssuer { expected_issuer: actual }
|
||||
if actual == expected_issuer
|
||||
)),
|
||||
ExpectedIssuerError::NotRecorded => assert!(
|
||||
matches!(error, AuthError::AuthorizationFailed(message) if message.contains("expected issuer was not recorded"))
|
||||
),
|
||||
ExpectedIssuerError::Mismatch {
|
||||
expected_issuer,
|
||||
received_issuer
|
||||
} if expected_issuer == "https://auth.example.com"
|
||||
&& received_issuer == "https://evil.example.com"
|
||||
));
|
||||
received_issuer,
|
||||
} => assert!(matches!(
|
||||
error,
|
||||
AuthError::AuthorizationServerMismatch {
|
||||
expected_issuer: actual_expected,
|
||||
received_issuer: actual_received
|
||||
} if actual_expected == expected_issuer && actual_received == received_issuer
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::requires_advertised_issuer(
|
||||
Some("https://auth.example.com"),
|
||||
true,
|
||||
None,
|
||||
ExpectedIssuerError::Missing {
|
||||
expected_issuer: "https://auth.example.com",
|
||||
}
|
||||
)]
|
||||
#[case::present_issuer_without_expected(
|
||||
None,
|
||||
false,
|
||||
Some("https://auth.example.com"),
|
||||
ExpectedIssuerError::NotRecorded
|
||||
)]
|
||||
#[case::required_issuer_without_expected(None, true, None, ExpectedIssuerError::NotRecorded)]
|
||||
#[case::mismatched_issuer(
|
||||
Some("https://auth.example.com"),
|
||||
false,
|
||||
Some("https://evil.example.com"),
|
||||
ExpectedIssuerError::Mismatch {
|
||||
expected_issuer: "https://auth.example.com",
|
||||
received_issuer: "https://evil.example.com",
|
||||
}
|
||||
)]
|
||||
fn validate_authorization_response_issuer_rejects_invalid_cases(
|
||||
#[case] expected_issuer: Option<&str>,
|
||||
#[case] require_issuer: bool,
|
||||
#[case] received_issuer: Option<&str>,
|
||||
#[case] expected_error: ExpectedIssuerError,
|
||||
) {
|
||||
let pkce = PkceCodeVerifier::new("verifier".to_string());
|
||||
let csrf = CsrfToken::new("csrf".to_string());
|
||||
let state = StoredAuthorizationState::new_with_expected_issuer(
|
||||
&pkce,
|
||||
&csrf,
|
||||
expected_issuer.map(str::to_owned),
|
||||
require_issuer,
|
||||
);
|
||||
|
||||
let error =
|
||||
AuthorizationManager::validate_authorization_response_issuer(&state, received_issuer)
|
||||
.unwrap_err();
|
||||
|
||||
assert_expected_issuer_error(error, expected_error);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -4642,62 +4643,40 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn validate_client_credentials_metadata_accepts_supported_method() {
|
||||
fn client_secret_credentials_config() -> super::ClientCredentialsConfig {
|
||||
super::ClientCredentialsConfig::ClientSecret {
|
||||
client_id: "id".to_string(),
|
||||
client_secret: "secret".to_string(),
|
||||
scopes: vec![],
|
||||
resource: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn metadata_with_auth_methods(methods: serde_json::Value) -> AuthorizationMetadata {
|
||||
let mut additional_fields = HashMap::new();
|
||||
additional_fields.insert(
|
||||
"token_endpoint_auth_methods_supported".to_string(),
|
||||
serde_json::json!(["client_secret_post", "client_secret_basic"]),
|
||||
);
|
||||
let meta = AuthorizationMetadata {
|
||||
additional_fields.insert("token_endpoint_auth_methods_supported".to_string(), methods);
|
||||
AuthorizationMetadata {
|
||||
authorization_endpoint: "http://localhost/authorize".to_string(),
|
||||
token_endpoint: "http://localhost/token".to_string(),
|
||||
additional_fields,
|
||||
..Default::default()
|
||||
};
|
||||
let mgr = manager_with_metadata(Some(meta)).await;
|
||||
let config = super::ClientCredentialsConfig::ClientSecret {
|
||||
client_id: "id".to_string(),
|
||||
client_secret: "secret".to_string(),
|
||||
scopes: vec![],
|
||||
resource: None,
|
||||
};
|
||||
mgr.validate_client_credentials_metadata(&config).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::supported_methods(Some(serde_json::json!([
|
||||
"client_secret_post",
|
||||
"client_secret_basic"
|
||||
])))]
|
||||
#[case::field_absent(None)]
|
||||
#[case::client_secret_basic_only(Some(serde_json::json!(["client_secret_basic"])))]
|
||||
#[tokio::test]
|
||||
async fn validate_client_credentials_metadata_permits_when_field_absent() {
|
||||
let mgr = manager_with_metadata(None).await;
|
||||
let config = super::ClientCredentialsConfig::ClientSecret {
|
||||
client_id: "id".to_string(),
|
||||
client_secret: "secret".to_string(),
|
||||
scopes: vec![],
|
||||
resource: None,
|
||||
};
|
||||
mgr.validate_client_credentials_metadata(&config).unwrap();
|
||||
}
|
||||
async fn validate_client_credentials_metadata_accepts_supported_configurations(
|
||||
#[case] auth_methods: Option<serde_json::Value>,
|
||||
) {
|
||||
let mgr = manager_with_metadata(auth_methods.map(metadata_with_auth_methods)).await;
|
||||
let config = client_secret_credentials_config();
|
||||
|
||||
#[tokio::test]
|
||||
async fn validate_client_credentials_metadata_accepts_client_secret_basic_only() {
|
||||
let mut additional_fields = HashMap::new();
|
||||
additional_fields.insert(
|
||||
"token_endpoint_auth_methods_supported".to_string(),
|
||||
serde_json::json!(["client_secret_basic"]),
|
||||
);
|
||||
let meta = AuthorizationMetadata {
|
||||
authorization_endpoint: "http://localhost/authorize".to_string(),
|
||||
token_endpoint: "http://localhost/token".to_string(),
|
||||
additional_fields,
|
||||
..Default::default()
|
||||
};
|
||||
let mgr = manager_with_metadata(Some(meta)).await;
|
||||
let config = super::ClientCredentialsConfig::ClientSecret {
|
||||
client_id: "id".to_string(),
|
||||
client_secret: "secret".to_string(),
|
||||
scopes: vec![],
|
||||
resource: None,
|
||||
};
|
||||
// A server advertising only client_secret_basic must be accepted.
|
||||
mgr.validate_client_credentials_metadata(&config).unwrap();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -309,6 +309,8 @@ impl StreamableHttpClientTransport<reqwest::Client> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rstest::rstest;
|
||||
|
||||
use super::parse_json_rpc_error;
|
||||
use crate::{
|
||||
model::JsonRpcMessage,
|
||||
|
|
@ -346,25 +348,15 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_json_rpc_error_rejects_non_error_request() {
|
||||
// A valid JSON-RPC request (method + id) must not be accepted as an error.
|
||||
let body = r#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#;
|
||||
#[rstest]
|
||||
#[case::non_error_request(r#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#)]
|
||||
#[case::notification(
|
||||
r#"{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":1}}"#
|
||||
)]
|
||||
#[case::plain_text("not json at all")]
|
||||
#[case::empty("")]
|
||||
#[case::truncated_json(r#"{"broken":"#)]
|
||||
fn parse_json_rpc_error_rejects_non_error_bodies(#[case] body: &str) {
|
||||
assert!(parse_json_rpc_error(body).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_json_rpc_error_rejects_notification() {
|
||||
// A notification (method, no id) must not be accepted as an error.
|
||||
let body =
|
||||
r#"{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":1}}"#;
|
||||
assert!(parse_json_rpc_error(body).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_json_rpc_error_rejects_malformed_json() {
|
||||
assert!(parse_json_rpc_error("not json at all").is_none());
|
||||
assert!(parse_json_rpc_error("").is_none());
|
||||
assert!(parse_json_rpc_error(r#"{"broken":"#).is_none());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue