From 3cf2af86094ce4c1c69d0cb2d4feb77aeaf09f79 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 25 Jan 2016 23:06:44 -0500 Subject: [PATCH] Test serde de/serialization of tokens --- Cargo.toml | 1 + src/lib.rs | 3 +++ src/token/bearer.rs | 13 +++++++++++++ src/token/expiring.rs | 12 ++++++++++++ src/token/statik.rs | 9 +++++++++ 5 files changed, 38 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 5f6aea0..8451ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ serde = "0.6.1" url = "0.5.0" [dev-dependencies] +serde_json = "0.6.0" yup-hyper-mock = "1.3.2" diff --git a/src/lib.rs b/src/lib.rs index 3a2d01c..b87a54e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,3 +149,6 @@ pub mod token; pub mod provider; pub mod error; pub mod client; + +#[cfg(test)] +extern crate serde_json; diff --git a/src/token/bearer.rs b/src/token/bearer.rs index f65a498..94fd964 100644 --- a/src/token/bearer.rs +++ b/src/token/bearer.rs @@ -157,6 +157,7 @@ impl de::Visitor for FieldVisitor { mod tests { use chrono::{UTC, Duration}; use rustc_serialize::json::Json; + use serde_json; use client::response::{FromResponse, ParseError}; use token::{Static, Expiring}; @@ -258,4 +259,16 @@ mod tests { assert!(expiring.expires() > &UTC::now()); assert!(expiring.expires() <= &(UTC::now() + Duration::seconds(3600))); } + + #[test] + fn serialize_deserialize() { + let original = Bearer { + access_token: String::from("foo"), + scope: Some(String::from("bar")), + lifetime: Static, + }; + let serialized = serde_json::to_value(&original); + let deserialized = serde_json::from_value(serialized).unwrap(); + assert_eq!(original, deserialized); + } } diff --git a/src/token/expiring.rs b/src/token/expiring.rs index b56caad..5fb15c6 100644 --- a/src/token/expiring.rs +++ b/src/token/expiring.rs @@ -182,6 +182,7 @@ impl de::Visitor for FieldVisitor { mod tests { use chrono::{UTC, Duration, Timelike}; use rustc_serialize::json::{self, Json}; + use serde_json; use client::response::FromResponse; use super::Expiring; @@ -218,4 +219,15 @@ mod tests { let decoded = json::decode(&json).unwrap(); assert_eq!(expiring, decoded); } + + #[test] + fn serialize_deserialize() { + let original = Expiring { + refresh_token: String::from("foo"), + expires: UTC::now().with_nanosecond(0).unwrap(), + }; + let serialized = serde_json::to_value(&original); + let deserialized = serde_json::from_value(serialized).unwrap(); + assert_eq!(original, deserialized); + } } diff --git a/src/token/statik.rs b/src/token/statik.rs index 620ecdb..f95c751 100644 --- a/src/token/statik.rs +++ b/src/token/statik.rs @@ -39,6 +39,7 @@ impl Deserialize for Static { #[cfg(test)] mod tests { use rustc_serialize::json::Json; + use serde_json; use client::response::{FromResponse, ParseError}; use super::Static; @@ -57,4 +58,12 @@ mod tests { Static::from_response(&json).unwrap_err() ); } + + #[test] + fn serialize_deserialize() { + let original = Static; + let serialized = serde_json::to_value(&original); + let deserialized = serde_json::from_value(serialized).unwrap(); + assert_eq!(original, deserialized); + } }