-
Notifications
You must be signed in to change notification settings - Fork 124
/
mod.rs
90 lines (79 loc) · 2.74 KB
/
mod.rs
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
pub mod base;
pub mod mutex;
pub mod oauth;
pub mod pagination;
pub use base::BaseClient;
pub use oauth::OAuthClient;
use crate::ClientResult;
use serde::Deserialize;
/// Converts a JSON response from Spotify into its model.
pub(in crate) fn convert_result<'a, T: Deserialize<'a>>(input: &'a str) -> ClientResult<T> {
serde_json::from_str::<T>(input).map_err(Into::into)
}
/// Append device ID to an API path.
pub(in crate) fn append_device_id(path: &str, device_id: Option<&str>) -> String {
let mut new_path = path.to_string();
if let Some(_device_id) = device_id {
if path.contains('?') {
new_path.push_str(&format!("&device_id={}", _device_id));
} else {
new_path.push_str(&format!("?device_id={}", _device_id));
}
}
new_path
}
#[cfg(test)]
mod test {
use super::*;
use crate::{model::Token, scopes, ClientCredsSpotify};
use chrono::{prelude::*, Duration};
#[test]
fn test_append_device_id_without_question_mark() {
let path = "me/player/play";
let device_id = Some("fdafdsadfa");
let new_path = append_device_id(path, device_id);
assert_eq!(new_path, "me/player/play?device_id=fdafdsadfa");
}
#[test]
fn test_append_device_id_with_question_mark() {
let path = "me/player/shuffle?state=true";
let device_id = Some("fdafdsadfa");
let new_path = append_device_id(path, device_id);
assert_eq!(
new_path,
"me/player/shuffle?state=true&device_id=fdafdsadfa"
);
}
#[test]
fn test_endpoint_url() {
let spotify = ClientCredsSpotify::default();
assert_eq!(
spotify.endpoint_url("me/player/play"),
"https://api.spotify.com/v1/me/player/play"
);
assert_eq!(
spotify.endpoint_url("http://api.spotify.com/v1/me/player/play"),
"http://api.spotify.com/v1/me/player/play"
);
assert_eq!(
spotify.endpoint_url("https://api.spotify.com/v1/me/player/play"),
"https://api.spotify.com/v1/me/player/play"
);
}
#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
async fn test_auth_headers() {
let tok = Token {
access_token: "test-access_token".to_string(),
expires_in: Duration::seconds(1),
expires_at: Some(Utc::now()),
scopes: scopes!("playlist-read-private"),
refresh_token: Some("...".to_string()),
};
let spotify = ClientCredsSpotify::from_token(tok);
let headers = spotify.auth_headers().await;
assert_eq!(
headers.get("authorization"),
Some(&"Bearer test-access_token".to_owned())
);
}
}