summaryrefslogtreecommitdiff
path: root/trakt-rs/src/test.rs
blob: 344449950165972ab7e2a2662756f49b58bf321c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use trakt_core::{
    error::{FromHttpError, IntoHttpError},
    Context, Request, Response,
};

pub fn assert_request<R, T>(ctx: Context, req: R, exp_url: &str, exp_body: &T)
where
    R: Request,
    T: ToString + ?Sized,
{
    let http_req = req.try_into_http_request::<Vec<u8>>(ctx).unwrap();

    assert_eq!(http_req.method(), R::METADATA.method);
    assert_eq!(http_req.uri(), exp_url);
    assert_eq!(
        http_req.headers().get("Content-Type").unwrap(),
        "application/json"
    );
    assert_eq!(http_req.headers().get("trakt-api-version").unwrap(), "2");
    assert_eq!(
        http_req.headers().get("trakt-api-key").unwrap(),
        ctx.client_id
    );
    if let Some(token) = ctx.oauth_token {
        assert_eq!(
            *http_req.headers().get("Authorization").unwrap(),
            format!("Bearer {token}")
        );
    }

    assert_eq!(
        String::from_utf8_lossy(http_req.body()),
        exp_body.to_string()
    );
}

pub fn execute<R: Request>(ctx: Context, req: R) -> Result<R::Response, Error> {
    let request: http::Request<Vec<u8>> = req.try_into_http_request(ctx)?;
    let (parts, body) = request.into_parts();
    let request = ureq::Request::from(parts);

    let response = request.send_bytes(&body)?;
    let http_res: http::Response<Vec<u8>> = http::Response::from(response);

    Ok(Response::try_from_http_response(http_res)?)
}

#[derive(Debug)]
pub enum Error {
    Reqwest(Box<ureq::Error>),
    IntoHttp(IntoHttpError),
    FromHttp(FromHttpError),
}

impl From<ureq::Error> for Error {
    fn from(e: ureq::Error) -> Self {
        Self::Reqwest(Box::new(e))
    }
}

impl From<IntoHttpError> for Error {
    fn from(e: IntoHttpError) -> Self {
        Self::IntoHttp(e)
    }
}

impl From<FromHttpError> for Error {
    fn from(e: FromHttpError) -> Self {
        Self::FromHttp(e)
    }
}