Test Bearer::from_response

This commit is contained in:
Curtis McEnroe 2015-12-24 00:44:09 -05:00
parent 552507478f
commit a8c1f7501e
1 changed files with 108 additions and 1 deletions

View File

@ -7,7 +7,7 @@ use client::response::{FromResponse, ParseError, JsonHelper};
/// The bearer token type.
///
/// See [RFC 6750](http://tools.ietf.org/html/rfc6750).
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct Bearer<L: Lifetime> {
access_token: String,
scope: Option<String>,
@ -57,3 +57,110 @@ impl<L: Lifetime> FromResponse for Bearer<L> {
Bearer::from_response_and_lifetime(json, lifetime)
}
}
#[cfg(test)]
mod tests {
use chrono::{UTC, Duration};
use rustc_serialize::json::Json;
use client::response::{FromResponse, ParseError};
use token::{Static, Expiring};
use super::Bearer;
#[test]
fn from_response_with_invalid_token_type() {
let json = Json::from_str(r#"{"token_type":"MAC","access_token":"aaaaaaaa"}"#).unwrap();
assert_eq!(
ParseError::ExpectedFieldValue("token_type", "Bearer"),
Bearer::<Static>::from_response(&json).unwrap_err()
);
}
#[test]
fn from_response_capital_b() {
let json = Json::from_str(r#"{"token_type":"Bearer","access_token":"aaaaaaaa"}"#).unwrap();
assert_eq!(
Bearer {
access_token: String::from("aaaaaaaa"),
scope: None,
lifetime: Static,
},
Bearer::<Static>::from_response(&json).unwrap()
);
}
#[test]
fn from_response_little_b() {
let json = Json::from_str(r#"{"token_type":"bearer","access_token":"aaaaaaaa"}"#).unwrap();
assert_eq!(
Bearer {
access_token: String::from("aaaaaaaa"),
scope: None,
lifetime: Static,
},
Bearer::<Static>::from_response(&json).unwrap()
);
}
#[test]
fn from_response_with_scope() {
let json = Json::from_str(
r#"{"token_type":"Bearer","access_token":"aaaaaaaa","scope":"foo"}"#
).unwrap();
assert_eq!(
Bearer {
access_token: String::from("aaaaaaaa"),
scope: Some(String::from("foo")),
lifetime: Static,
},
Bearer::<Static>::from_response(&json).unwrap()
);
}
#[test]
fn from_response_expiring() {
let json = Json::from_str(r#"
{
"token_type":"Bearer",
"access_token":"aaaaaaaa",
"expires_in":3600,
"refresh_token":"bbbbbbbb"
}
"#).unwrap();
let bearer = Bearer::<Expiring>::from_response(&json).unwrap();
assert_eq!("aaaaaaaa", bearer.access_token);
assert_eq!(None, bearer.scope);
let expiring = bearer.lifetime;
assert_eq!("bbbbbbbb", expiring.refresh_token());
assert!(expiring.expires() > &UTC::now());
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
}
#[test]
fn from_response_inherit_expiring() {
let json = Json::from_str(r#"
{
"token_type":"Bearer",
"access_token":"aaaaaaaa",
"expires_in":3600,
"refresh_token":"bbbbbbbb"
}
"#).unwrap();
let prev = Bearer::<Expiring>::from_response(&json).unwrap();
let json = Json::from_str(r#"
{
"token_type":"Bearer",
"access_token":"cccccccc",
"expires_in":3600
}
"#).unwrap();
let bearer = Bearer::<Expiring>::from_response_inherit(&json, &prev).unwrap();
assert_eq!("cccccccc", bearer.access_token);
assert_eq!(None, bearer.scope);
let expiring = bearer.lifetime;
assert_eq!("bbbbbbbb", expiring.refresh_token());
assert!(expiring.expires() > &UTC::now());
assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600)));
}
}