#[cfg(test)]
mod tests {
use std::convert::Infallible;
use hyper::Body;
use oauth_credentials::Token;
use tower::ServiceExt;
use crate::request::RawRequest;
use super::*;
#[tokio::test]
async fn parse_errors() {
#[derive(oauth::Request)]
struct Foo {
param: u32,
}
impl RawRequest for Foo {
fn method(&self) -> &http::Method {
&http::Method::GET
}
fn uri(&self) -> &'static str {
"https://api.twitter.com/1.1/test/foo.json"
}
}
let token = Token::from_parts("", "", "", "");
let (http, mut handle) = tower_test::mock::pair::<http::Request<Vec<u8>>, _>();
let mut http = http.map_err(|e| -> Infallible { panic!("{:?}", e) });
let res_fut =
tokio::spawn(Foo { param: 42 }.send_raw(&token, http.ready_and().await.unwrap()));
let (_req, tx) = handle.next_request().await.unwrap();
let payload = br#"{"errors":[{"code":104,"message":"You aren't allowed to add members to this list."}]}"#;
let res = http::Response::builder()
.status(http::StatusCode::FORBIDDEN)
.body(Body::from(&payload[..]))
.unwrap();
tx.send_response(res);
match res_fut.await.unwrap().unwrap_err() {
Error::Twitter(crate::error::TwitterErrors {
status: http::StatusCode::FORBIDDEN,
errors,
rate_limit: None,
}) => {
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].code, 104);
assert_eq!(
errors[0].message,
"You aren't allowed to add members to this list.",
);
}
e => panic!("{:?}", e),
};
}
}