diff options
author | Shaun Taheri <shaun@advancedtelematic.com> | 2016-05-30 18:36:35 +0200 |
---|---|---|
committer | Shaun Taheri <shaun@advancedtelematic.com> | 2016-06-08 12:05:21 +0200 |
commit | 047d80a47ae82e5357a40733ca3b011b5df8eb73 (patch) | |
tree | 2355c68ae2de11d4aa243d8cb167c66c86c9af55 /src/oauth2.rs | |
parent | 6ecbc3aaaeb2e96e8f058c41c82475e2d7c5dc51 (diff) | |
download | rvi_sota_client-047d80a47ae82e5357a40733ca3b011b5df8eb73.tar.gz |
Migrate HTTP Client to async form
Diffstat (limited to 'src/oauth2.rs')
-rw-r--r-- | src/oauth2.rs | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/src/oauth2.rs b/src/oauth2.rs index c1fb705..69d6084 100644 --- a/src/oauth2.rs +++ b/src/oauth2.rs @@ -1,72 +1,61 @@ use rustc_serialize::json; -use datatype::{AccessToken, AuthConfig, ClientId, ClientSecret, Error}; -use http_client::{Auth, HttpClient, HttpRequest}; +use datatype::{AccessToken, AuthConfig, Error, Method}; +use http_client::{HttpClient, HttpRequest}; -pub fn authenticate(config: &AuthConfig, client: &mut HttpClient) -> Result<AccessToken, Error> { - +pub fn authenticate(config: &AuthConfig, client: &HttpClient) -> Result<AccessToken, Error> { debug!("authenticate()"); - let req = HttpRequest::post::<_, _, String>( - config.server.join("/token").unwrap(), - Some(Auth::Credentials( - ClientId { get: config.client_id.clone() }, - ClientSecret { get: config.secret.clone() })), - None, - ); - - let resp = try!(client.send_request(&req)); + let resp_rx = client.send_request(HttpRequest{ + method: Method::Post, + url: config.server.join("/token").unwrap(), + body: None + }); - let body = try!(String::from_utf8(resp.body)); + let resp = try!(resp_rx.recv()); + let data = try!(resp); + let body = try!(String::from_utf8(data)); debug!("authenticate, body: `{}`", body); - Ok(try!(json::decode(&body))) - } #[cfg(test)] mod tests { - use super::*; + use datatype::{AccessToken, AuthConfig}; use http_client::TestHttpClient; - const TOKEN: &'static str = - r#"{"access_token": "token", - "token_type": "type", - "expires_in": 10, - "scope": ["scope"]} - "#; #[test] fn test_authenticate() { - assert_eq!(authenticate(&AuthConfig::default(), &mut TestHttpClient::from(vec![TOKEN])).unwrap(), - AccessToken { - access_token: "token".to_string(), - token_type: "type".to_string(), - expires_in: 10, - scope: vec!["scope".to_string()] - }) + let token = r#"{"access_token": "token", "token_type": "type", "expires_in": 10, "scope": ["scope"]}"#; + let client = TestHttpClient::from(vec![token.as_bytes().to_vec()]); + let expect = AccessToken { + access_token: "token".to_string(), + token_type: "type".to_string(), + expires_in: 10, + scope: vec!["scope".to_string()] + }; + assert_eq!(expect, authenticate(&AuthConfig::default(), &client).unwrap()); } #[test] fn test_authenticate_no_token() { - assert_eq!(format!("{}", authenticate(&AuthConfig::default(), - &mut TestHttpClient::from(vec![""])).unwrap_err()), - r#"Failed to decode JSON: ParseError(SyntaxError("EOF While parsing value", 1, 1))"#) - - // XXX: Old error message was arguebly a lot better... - // "Authentication error, didn't receive access token.") + let client = TestHttpClient::from(vec!["".as_bytes().to_vec()]); + // XXX: Old error message was arguably a lot better... + // "Authentication error, didn't receive access token.") + let expect = r#"Failed to decode JSON: ParseError(SyntaxError("EOF While parsing value", 1, 1))"#; + assert_eq!(expect, format!("{}", authenticate(&AuthConfig::default(), &client).unwrap_err())); } #[test] fn test_authenticate_bad_json() { - assert_eq!(format!("{}", authenticate(&AuthConfig::default(), - &mut TestHttpClient::from(vec![r#"{"apa": 1}"#])).unwrap_err()), - r#"Failed to decode JSON: MissingFieldError("access_token")"#) + let client = TestHttpClient::from(vec![r#"{"apa": 1}"#.as_bytes().to_vec()]); + let expect = r#"Failed to decode JSON: MissingFieldError("access_token")"#; + assert_eq!(expect, format!("{}", authenticate(&AuthConfig::default(), &client).unwrap_err())); } - } |