Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannesd3 committed Apr 9, 2021
1 parent 63540ef commit 5c17f7d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
23 changes: 23 additions & 0 deletions core/src/apresolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
48 changes: 16 additions & 32 deletions core/tests/connect.rs
Original file line number Diff line number Diff line change
@@ -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"),
};
}

0 comments on commit 5c17f7d

Please sign in to comment.