http_auth.rs (1644B)
1 #![forbid(unsafe_code)] 2 3 use crate::envelope::kind::KIND_HTTP_AUTH as KIND_HTTP_AUTH_EVENT; 4 5 #[cfg(not(feature = "std"))] 6 use alloc::string::String; 7 8 pub const KIND_HTTP_AUTH: u32 = KIND_HTTP_AUTH_EVENT; 9 10 #[cfg_attr( 11 any(feature = "serde", test), 12 derive(serde::Serialize, serde::Deserialize) 13 )] 14 #[derive(Clone, Debug, PartialEq, Eq)] 15 pub struct HttpAuth { 16 pub url: String, 17 pub method: String, 18 pub payload_sha256: Option<String>, 19 } 20 21 #[cfg(all(test, feature = "serde"))] 22 #[cfg_attr(coverage_nightly, coverage(off))] 23 mod tests { 24 use super::*; 25 26 #[test] 27 fn http_auth_kind_matches_nip98() { 28 assert_eq!(KIND_HTTP_AUTH, 27235); 29 } 30 31 #[test] 32 fn http_auth_serializes_optional_payload_hash() { 33 let value = serde_json::to_value(HttpAuth { 34 url: "https://media.example.invalid/upload".to_string(), 35 method: "POST".to_string(), 36 payload_sha256: Some( 37 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_string(), 38 ), 39 }) 40 .unwrap(); 41 42 assert_eq!(value["url"], "https://media.example.invalid/upload"); 43 assert_eq!(value["method"], "POST"); 44 assert_eq!( 45 value["payload_sha256"], 46 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 47 ); 48 } 49 50 #[test] 51 fn http_auth_allows_absent_payload_hash() { 52 let auth = HttpAuth { 53 url: "https://media.example.invalid/download".to_string(), 54 method: "GET".to_string(), 55 payload_sha256: None, 56 }; 57 58 assert_eq!(auth.payload_sha256, None); 59 } 60 }