From 3c32646ea4e784140e2f3338310594d0a31b2393 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 23 Dec 2015 23:07:56 -0500 Subject: [PATCH] Test OAuth2Error::from_response --- src/error.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/error.rs b/src/error.rs index 5a8dd16..ac867eb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -102,3 +102,62 @@ impl FromResponse for OAuth2Error { }) } } + +#[cfg(test)] +mod tests { + use rustc_serialize::json::Json; + + use client::response::{FromResponse, ParseError}; + use super::{OAuth2Error, OAuth2ErrorCode}; + + #[test] + fn from_response_empty() { + let json = Json::from_str("{}").unwrap(); + assert_eq!( + ParseError::ExpectedFieldType("error", "string"), + OAuth2Error::from_response(&json).unwrap_err() + ); + } + + #[test] + fn from_response() { + let json = Json::from_str(r#"{"error":"invalid_request"}"#).unwrap(); + assert_eq!( + OAuth2Error { + code: OAuth2ErrorCode::InvalidRequest, + description: None, + uri: None, + }, + OAuth2Error::from_response(&json).unwrap() + ); + } + + #[test] + fn from_response_with_description() { + let json = Json::from_str(r#"{"error":"invalid_request","error_description":"foo"}"#) + .unwrap(); + assert_eq!( + OAuth2Error { + code: OAuth2ErrorCode::InvalidRequest, + description: Some(String::from("foo")), + uri: None, + }, + OAuth2Error::from_response(&json).unwrap() + ); + } + + #[test] + fn from_response_with_uri() { + let json = Json::from_str( + r#"{"error":"invalid_request","error_uri":"http://example.com"}"# + ).unwrap(); + assert_eq!( + OAuth2Error { + code: OAuth2ErrorCode::InvalidRequest, + description: None, + uri: Some(String::from("http://example.com")), + }, + OAuth2Error::from_response(&json).unwrap() + ); + } +}