From 65eb87e1d49753a827b1bd759454ea76edd70c11 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 30 Dec 2015 01:09:17 -0500 Subject: [PATCH] Test mocked invalid request error --- tests/mock.rs | 41 +++++++++++++++++++++++++++++ tests/response/invalid_request.http | 10 +++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/response/invalid_request.http diff --git a/tests/mock.rs b/tests/mock.rs index 47607e7..59c371c 100644 --- a/tests/mock.rs +++ b/tests/mock.rs @@ -6,6 +6,7 @@ extern crate yup_hyper_mock; use chrono::{UTC, Duration}; use inth_oauth2::{Client, ClientError, Token, Lifetime}; +use inth_oauth2::error::OAuth2ErrorCode; mod provider { use inth_oauth2::token::{Bearer, Static, Expiring}; @@ -44,6 +45,15 @@ mod connector { include_str!("response/request_token_bearer_expiring.http") include_str!("response/refresh_token_bearer_partial.http") }); + + mock_connector_in_order!(InvalidRequest { + include_str!("response/invalid_request.http") + }); + + mock_connector_in_order!(RefreshInvalidRequest { + include_str!("response/request_token_bearer_expiring.http") + include_str!("response/invalid_request.http") + }); } macro_rules! mock_client { @@ -117,3 +127,34 @@ fn request_token_bearer_expiring_wrong_lifetime() { let err = client.request_token("code").unwrap_err(); assert!(match err { ClientError::Parse(..) => true, _ => false }); } + +#[test] +fn request_token_invalid_request() { + let client = mock_client!(provider::BearerStatic, connector::InvalidRequest); + let err = client.request_token("code").unwrap_err(); + assert!(match err { + ClientError::OAuth2(err) => { + assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code); + assert_eq!("example", err.description.unwrap()); + assert_eq!("https://example.com/error", err.uri.unwrap()); + true + }, + _ => false, + }); +} + +#[test] +fn refresh_token_invalid_request() { + let client = mock_client!(provider::BearerExpiring, connector::RefreshInvalidRequest); + let token = client.request_token("code").unwrap(); + let err = client.refresh_token(token, None).unwrap_err(); + assert!(match err { + ClientError::OAuth2(err) => { + assert_eq!(OAuth2ErrorCode::InvalidRequest, err.code); + assert_eq!("example", err.description.unwrap()); + assert_eq!("https://example.com/error", err.uri.unwrap()); + true + }, + _ => false, + }); +} diff --git a/tests/response/invalid_request.http b/tests/response/invalid_request.http new file mode 100644 index 0000000..e0b79d8 --- /dev/null +++ b/tests/response/invalid_request.http @@ -0,0 +1,10 @@ +HTTP/1.1 400 Bad Request +Content-Type: application/json;charset=UTF-8 +Cache-Control: no-store +Pragma: no-cache + +{ + "error":"invalid_request", + "error_description":"example", + "error_uri":"https://example.com/error" +}