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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::borrow::Cow;
#[derive(Clone, Debug, Default)]
pub struct CompanyDetails {
pub company_id: u64,
}
impl CompanyDetails {
pub fn new(company_id: u64) -> Self {
Self { company_id }
}
}
impl crate::prelude::Command for CompanyDetails {
type Output = super::Company;
fn path(&self) -> Cow<'static, str> {
Cow::Owned(format!("/company/{}", self.company_id))
}
fn params(&self) -> Vec<(&'static str, Cow<'_, str>)> {
Vec::new()
}
}
#[cfg(test)]
mod tests {
use super::CompanyDetails;
use crate::prelude::Command;
use crate::Client;
use mockito::{mock, Matcher};
#[tokio::test]
async fn it_works() {
let _m = mock("GET", "/company/1")
.match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/company-details.json"))
.create();
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let result = CompanyDetails::new(1).execute(&client).await.unwrap();
assert_eq!(result.inner.id, 1);
}
#[tokio::test]
async fn invalid_api_key() {
let _m = mock("GET", "/company/1")
.match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
.with_status(401)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/invalid-api-key.json"))
.create();
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let err = CompanyDetails::new(1).execute(&client).await.unwrap_err();
let server_err = err.as_server_error().unwrap();
assert_eq!(server_err.body.as_other_error().unwrap().status_code, 7);
}
#[tokio::test]
async fn resource_not_found() {
let _m = mock("GET", "/company/1")
.match_query(Matcher::UrlEncoded("api_key".into(), "secret".into()))
.with_status(404)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/resource-not-found.json"))
.create();
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let err = CompanyDetails::new(1).execute(&client).await.unwrap_err();
let server_err = err.as_server_error().unwrap();
assert_eq!(server_err.body.as_other_error().unwrap().status_code, 34);
}
}
#[cfg(all(test, feature = "integration"))]
mod integration_tests {
use super::CompanyDetails;
use crate::prelude::Command;
use crate::Client;
#[tokio::test]
async fn execute() {
let secret = std::env::var("TMDB_TOKEN_V3").unwrap();
let client = Client::new(secret);
let result = CompanyDetails::new(1).execute(&client).await.unwrap();
assert_eq!(result.inner.id, 1);
}
}