|
1 | 1 | use super::{DEFAULT_MAX_PAGES, DEFAULT_MAX_RESPONSE_BYTES, ServerConfig}; |
2 | 2 |
|
| 3 | +// --- apply_session_token --- |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_session_token_default_none() { |
| 7 | + let config = ServerConfig::default(); |
| 8 | + assert!(config.auth_token_setting.is_none()); |
| 9 | +} |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn test_session_token_prefix_default_empty() { |
| 13 | + let config = ServerConfig::default(); |
| 14 | + // Struct Default gives an empty prefix; configure_auth applies the real |
| 15 | + // "Bearer" default when the server option is absent. |
| 16 | + assert_eq!(config.auth_token_prefix, ""); |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_apply_session_token_adds_bearer_header() { |
| 21 | + let mut headers = vec![]; |
| 22 | + ServerConfig::apply_session_token(&mut headers, "tok_abc123", "Bearer"); |
| 23 | + assert_eq!(headers.len(), 1); |
| 24 | + assert_eq!(headers[0].0, "authorization"); |
| 25 | + assert_eq!(headers[0].1, "Bearer tok_abc123"); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_apply_session_token_custom_prefix() { |
| 30 | + let mut headers = vec![]; |
| 31 | + ServerConfig::apply_session_token(&mut headers, "tok_xyz", "Token"); |
| 32 | + assert_eq!(headers[0].1, "Token tok_xyz"); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn test_apply_session_token_empty_prefix_no_prefix() { |
| 37 | + // Empty prefix → raw token value, no leading space |
| 38 | + let mut headers = vec![]; |
| 39 | + ServerConfig::apply_session_token(&mut headers, "rawtoken", ""); |
| 40 | + assert_eq!(headers[0].1, "rawtoken"); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_apply_session_token_replaces_existing_authorization() { |
| 45 | + let mut headers = vec![("authorization".to_owned(), "Bearer old-token".to_owned())]; |
| 46 | + ServerConfig::apply_session_token(&mut headers, "new-token", "Bearer"); |
| 47 | + // Should replace, not append |
| 48 | + assert_eq!(headers.len(), 1); |
| 49 | + assert_eq!(headers[0].1, "Bearer new-token"); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn test_apply_session_token_replaces_existing_authorization_case_insensitive() { |
| 54 | + // Existing header uses capital "Authorization" (e.g. from the headers option). |
| 55 | + // Header names are case-insensitive, so it should be replaced, not duplicated. |
| 56 | + let mut headers = vec![("Authorization".to_owned(), "Bearer old-token".to_owned())]; |
| 57 | + ServerConfig::apply_session_token(&mut headers, "new-token", "Bearer"); |
| 58 | + assert_eq!(headers.len(), 1); |
| 59 | + assert_eq!(headers[0].1, "Bearer new-token"); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn test_apply_session_token_replaces_leaves_other_headers_intact() { |
| 64 | + let mut headers = vec![ |
| 65 | + ("content-type".to_owned(), "application/json".to_owned()), |
| 66 | + ("authorization".to_owned(), "Bearer old".to_owned()), |
| 67 | + ("x-request-id".to_owned(), "req-123".to_owned()), |
| 68 | + ]; |
| 69 | + ServerConfig::apply_session_token(&mut headers, "new", "Bearer"); |
| 70 | + assert_eq!(headers.len(), 3); |
| 71 | + assert_eq!(headers[0].0, "content-type"); |
| 72 | + assert_eq!(headers[1].1, "Bearer new"); |
| 73 | + assert_eq!(headers[2].0, "x-request-id"); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn test_apply_session_token_appends_when_no_existing_authorization() { |
| 78 | + let mut headers = vec![ |
| 79 | + ("content-type".to_owned(), "application/json".to_owned()), |
| 80 | + ("user-agent".to_owned(), "Wrappers/1.0".to_owned()), |
| 81 | + ]; |
| 82 | + ServerConfig::apply_session_token(&mut headers, "tok", "Bearer"); |
| 83 | + assert_eq!(headers.len(), 3); |
| 84 | + assert_eq!(headers[2].0, "authorization"); |
| 85 | + assert_eq!(headers[2].1, "Bearer tok"); |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn test_apply_session_token_empty_token_noop() { |
| 90 | + let mut headers = vec![("authorization".to_owned(), "Bearer original".to_owned())]; |
| 91 | + ServerConfig::apply_session_token(&mut headers, "", "Bearer"); |
| 92 | + // Empty token → no change |
| 93 | + assert_eq!(headers[0].1, "Bearer original"); |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn test_apply_session_token_whitespace_token_noop() { |
| 98 | + let mut headers = vec![("authorization".to_owned(), "Bearer original".to_owned())]; |
| 99 | + ServerConfig::apply_session_token(&mut headers, " ", "Bearer"); |
| 100 | + assert_eq!(headers[0].1, "Bearer original"); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn test_apply_session_token_empty_token_does_not_append() { |
| 105 | + let mut headers = vec![]; |
| 106 | + ServerConfig::apply_session_token(&mut headers, "", "Bearer"); |
| 107 | + assert!(headers.is_empty()); |
| 108 | +} |
| 109 | + |
| 110 | +#[test] |
| 111 | +fn test_apply_session_token_overrides_static_bearer() { |
| 112 | + // Simulate: static bearer_token set at init, session token injected at request time |
| 113 | + let mut headers = vec![ |
| 114 | + ("content-type".to_owned(), "application/json".to_owned()), |
| 115 | + ("authorization".to_owned(), "Bearer static-token".to_owned()), |
| 116 | + ]; |
| 117 | + ServerConfig::apply_session_token(&mut headers, "per-user-token", "Bearer"); |
| 118 | + let auth = headers.iter().find(|h| h.0 == "authorization").unwrap(); |
| 119 | + assert_eq!(auth.1, "Bearer per-user-token"); |
| 120 | + // Static token not leaked |
| 121 | + assert!(!auth.1.contains("static-token")); |
| 122 | +} |
| 123 | + |
| 124 | +#[test] |
| 125 | +fn test_apply_session_token_second_call_replaces_first() { |
| 126 | + let mut headers = vec![]; |
| 127 | + ServerConfig::apply_session_token(&mut headers, "first", "Bearer"); |
| 128 | + ServerConfig::apply_session_token(&mut headers, "second", "Bearer"); |
| 129 | + assert_eq!(headers.len(), 1); |
| 130 | + assert_eq!(headers[0].1, "Bearer second"); |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn test_debug_shows_auth_token_setting_name() { |
| 135 | + let config = ServerConfig { |
| 136 | + auth_token_setting: Some("app.user_token".to_string()), |
| 137 | + ..Default::default() |
| 138 | + }; |
| 139 | + let debug_output = format!("{config:?}"); |
| 140 | + // Setting name is not sensitive — it should appear in debug output |
| 141 | + assert!(debug_output.contains("app.user_token")); |
| 142 | +} |
| 143 | + |
| 144 | +#[test] |
| 145 | +fn test_debug_no_auth_token_setting_shows_none() { |
| 146 | + let config = ServerConfig::default(); |
| 147 | + let debug_output = format!("{config:?}"); |
| 148 | + assert!(debug_output.contains("auth_token_setting: None")); |
| 149 | +} |
| 150 | + |
3 | 151 | // --- Default values --- |
4 | 152 |
|
5 | 153 | #[test] |
|
0 commit comments