Add basic mocked client integration tests

This commit is contained in:
Curtis McEnroe 2015-12-29 22:45:33 -05:00
parent f6ceabec1c
commit 95a27a601b
4 changed files with 98 additions and 0 deletions

View File

@ -17,3 +17,6 @@ chrono = "0.2.17"
hyper = "0.7.0" hyper = "0.7.0"
rustc-serialize = "0.3.16" rustc-serialize = "0.3.16"
url = "0.5.0" url = "0.5.0"
[dev-dependencies]
yup-hyper-mock = "1.3.2"

73
tests/mock.rs Normal file
View File

@ -0,0 +1,73 @@
extern crate chrono;
extern crate hyper;
extern crate inth_oauth2;
#[macro_use]
extern crate yup_hyper_mock;
use chrono::{UTC, Duration};
use inth_oauth2::{Client, Token, Lifetime};
mod provider {
use inth_oauth2::token::{Bearer, Static, Expiring};
use inth_oauth2::provider::Provider;
pub struct BearerStatic;
impl Provider for BearerStatic {
type Lifetime = Static;
type Token = Bearer<Static>;
fn auth_uri() -> &'static str { "https://example.com/oauth/auth" }
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
}
pub struct BearerExpiring;
impl Provider for BearerExpiring {
type Lifetime = Expiring;
type Token = Bearer<Expiring>;
fn auth_uri() -> &'static str { "https://example.com/oauth/auth" }
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
}
}
mod connector {
use hyper;
mock_connector_in_order!(BearerStaticSuccess {
include_str!("response/request_token_bearer_static_success.http")
});
mock_connector_in_order!(BearerExpiringSuccess {
include_str!("response/request_token_bearer_expiring_success.http")
});
}
macro_rules! mock_client {
($p:ty, $c:ty) => {
Client::<$p>::new(
hyper::Client::with_connector(<$c>::default()),
"client_id",
"client_secret",
None
)
}
}
#[test]
fn request_token_bearer_static_success() {
let client = mock_client!(provider::BearerStatic, connector::BearerStaticSuccess);
let token = client.request_token("code").unwrap();
assert_eq!("aaaaaaaa", token.access_token());
assert_eq!(Some("example"), token.scope());
}
#[test]
fn request_token_bearer_expiring_success() {
let client = mock_client!(provider::BearerExpiring, connector::BearerExpiringSuccess);
let token = client.request_token("code").unwrap();
assert_eq!("aaaaaaaa", token.access_token());
assert_eq!(Some("example"), token.scope());
assert_eq!("bbbbbbbb", token.lifetime().refresh_token());
assert_eq!(false, token.lifetime().expired());
assert!(token.lifetime().expires() > &UTC::now());
assert!(token.lifetime().expires() <= &(UTC::now() + Duration::seconds(3600)));
}

View File

@ -0,0 +1,12 @@
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"aaaaaaaa",
"token_type":"bearer",
"expires_in":3600,
"refresh_token":"bbbbbbbb",
"scope":"example"
}

View File

@ -0,0 +1,10 @@
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"aaaaaaaa",
"token_type":"bearer",
"scope":"example"
}