inth_oauth2_async/token/
statik.rs

1use serde_json::Value;
2
3use crate::client::response::{FromResponse, ParseError};
4use crate::token::Lifetime;
5
6/// A static, non-expiring token.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub struct Static;
9
10impl Lifetime for Static {
11    fn expired(&self) -> bool { false }
12}
13
14impl FromResponse for Static {
15    fn from_response(json: &Value) -> Result<Self, ParseError> {
16        let obj = json.as_object().ok_or(ParseError::ExpectedType("object"))?;
17        if obj.contains_key("expires_in") {
18            return Err(ParseError::UnexpectedField("expires_in"));
19        }
20        Ok(Static)
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn from_response() {
30        let json = "{}".parse().unwrap();
31        assert_eq!(Static, Static::from_response(&json).unwrap());
32    }
33
34    #[test]
35    fn from_response_with_expires_in() {
36        let json = r#"{"expires_in":3600}"#.parse().unwrap();
37        assert_eq!(
38            ParseError::UnexpectedField("expires_in"),
39            Static::from_response(&json).unwrap_err()
40        );
41    }
42}