Add simple tests to librespot-core

The first test checks whether apresolve works. A second test tries
to create a Spotify sessions with fake credentials and asserts that
an error is returned.
This commit is contained in:
johannesd3 2021-04-02 14:44:42 +02:00
parent 5435ab3270
commit 690e0d2e10
2 changed files with 39 additions and 32 deletions

View file

@ -66,3 +66,26 @@ pub async fn apresolve(proxy: Option<&Url>, ap_port: Option<u16>) -> String {
AP_FALLBACK.into()
})
}
#[cfg(test)]
mod test {
use std::net::ToSocketAddrs;
use super::try_apresolve;
#[tokio::test]
async fn test_apresolve() {
let ap = try_apresolve(None, None).await.unwrap();
// Assert that the result contains a valid host and port
ap.to_socket_addrs().unwrap().next().unwrap();
}
#[tokio::test]
async fn test_apresolve_port_443() {
let ap = try_apresolve(None, Some(443)).await.unwrap();
let port = ap.to_socket_addrs().unwrap().next().unwrap().port();
assert_eq!(port, 443);
}
}

View file

@ -1,34 +1,18 @@
use librespot_core::*;
use librespot_core::authentication::Credentials;
use librespot_core::config::SessionConfig;
use librespot_core::session::Session;
// TODO: test is broken
// #[cfg(test)]
// mod tests {
// use super::*;
// // Test AP Resolve
// use apresolve::apresolve_or_fallback;
// #[tokio::test]
// async fn test_ap_resolve() {
// env_logger::init();
// let ap = apresolve_or_fallback(&None, &None).await;
// println!("AP: {:?}", ap);
// }
#[tokio::test]
async fn test_connection() {
let result = Session::connect(
SessionConfig::default(),
Credentials::with_password("test", "test"),
None,
)
.await;
// // Test connect
// use authentication::Credentials;
// use config::SessionConfig;
// #[tokio::test]
// async fn test_connection() -> Result<(), Box<dyn std::error::Error>> {
// println!("Running connection test");
// let ap = apresolve_or_fallback(&None, &None).await;
// let credentials = Credentials::with_password(String::from("test"), String::from("test"));
// let session_config = SessionConfig::default();
// let proxy = None;
// println!("Connecting to AP \"{}\"", ap);
// let mut connection = connection::connect(ap, &proxy).await?;
// let rc = connection::authenticate(&mut connection, credentials, &session_config.device_id)
// .await?;
// println!("Authenticated as \"{}\"", rc.username);
// Ok(())
// }
// }
match result {
Ok(_) => panic!("Authentication succeeded despite of bad credentials."),
Err(e) => assert_eq!(e.to_string(), "Login failed with reason: Bad credentials"),
};
}