From 1e42110ae16c5796347a12136c770f1b1a2ad269 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 6 Mar 2021 19:36:37 +0000 Subject: [PATCH] :recycle: refactor: extract basic client tests --- src/tests.rs | 103 ++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 63 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 92caa84..66aa02d 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -18,8 +18,6 @@ use tokio::{ use reqwest::Client; use tracing::{debug, info}; -use ptth_relay::load_templates; - // Poll for a few seconds till the server is ready // If this takes more than 5 seconds-ish, it's bad, the test should // fail @@ -33,6 +31,44 @@ async fn wait_for_any_server (relay_state: &ptth_relay::RelayState) { } } +async fn testing_client_checks ( + testing_config: &TestingConfig, + client: &reqwest::Client +) { + let relay_url = testing_config.relay_url (); + let server_name = testing_config.server_name; + + let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url)) + .send ().await.expect ("Couldn't check if relay is up").bytes ().await.expect ("Couldn't check if relay is up"); + + assert_eq! (resp, "Relay is up\n"); + + let resp = client.get (&format! ("{}/frontend/servers/{}/files/COPYING", relay_url, server_name)) + .send ().await.expect ("Couldn't find license").bytes ().await.expect ("Couldn't find license"); + + if blake3::hash (&resp) != blake3::Hash::from ([ + 0xca, 0x02, 0x92, 0x78, + 0x9c, 0x0a, 0x0e, 0xcb, + 0xa7, 0x06, 0xf4, 0xb3, + 0xf3, 0x49, 0x30, 0x07, + + 0xa9, 0x95, 0x17, 0x31, + 0xc1, 0xd4, 0x32, 0xc5, + 0x2c, 0x4a, 0xac, 0x1f, + 0x1a, 0xbb, 0xa8, 0xef, + ]) { + panic! ("{}", String::from_utf8 (resp.to_vec ()).expect ("???")); + } + + // Requesting a file from a server that isn't registered + // will error out + + let resp = client.get (&format! ("{}/frontend/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url)) + .send ().await.expect ("Couldn't send request to bogus server"); + + assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND); +} + struct TestingConfig { server_name: &'static str, api_key: &'static str, @@ -172,38 +208,9 @@ fn end_to_end () { ]); let client = Client::builder () - .timeout (Duration::from_secs (2)) .build ().expect ("Couldn't build HTTP client"); - let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url)) - .send ().await.expect ("Couldn't check if relay is up").bytes ().await.expect ("Couldn't check if relay is up"); - - assert_eq! (resp, "Relay is up\n"); - - let resp = client.get (&format! ("{}/frontend/servers/{}/files/COPYING", relay_url, server_name)) - .send ().await.expect ("Couldn't find license").bytes ().await.expect ("Couldn't find license"); - - if blake3::hash (&resp) != blake3::Hash::from ([ - 0xca, 0x02, 0x92, 0x78, - 0x9c, 0x0a, 0x0e, 0xcb, - 0xa7, 0x06, 0xf4, 0xb3, - 0xf3, 0x49, 0x30, 0x07, - - 0xa9, 0x95, 0x17, 0x31, - 0xc1, 0xd4, 0x32, 0xc5, - 0x2c, 0x4a, 0xac, 0x1f, - 0x1a, 0xbb, 0xa8, 0xef, - ]) { - panic! ("{}", String::from_utf8 (resp.to_vec ()).expect ("???")); - } - - // Requesting a file from a server that isn't registered - // will error out - - let resp = client.get (&format! ("{}/frontend/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url)) - .send ().await.expect ("Couldn't send request to bogus server"); - - assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND); + testing_client_checks (&testing_config, &client).await; info! ("Shutting down end-to-end test"); @@ -232,7 +239,6 @@ fn debug_proxy () { proxy_port, relay_port, }; - let relay_url = testing_config.relay_url (); let testing_relay = TestingRelay::new (&testing_config).await; @@ -254,38 +260,9 @@ fn debug_proxy () { ]); let client = Client::builder () - .timeout (Duration::from_secs (2)) .build ().expect ("Couldn't build HTTP client"); - let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url)) - .send ().await.expect ("Couldn't check if relay is up").bytes ().await.expect ("Couldn't check if relay is up"); - - assert_eq! (resp, "Relay is up\n"); - - let resp = client.get (&format! ("{}/frontend/servers/{}/files/COPYING", relay_url, server_name)) - .send ().await.expect ("Couldn't find license").bytes ().await.expect ("Couldn't find license"); - - if blake3::hash (&resp) != blake3::Hash::from ([ - 0xca, 0x02, 0x92, 0x78, - 0x9c, 0x0a, 0x0e, 0xcb, - 0xa7, 0x06, 0xf4, 0xb3, - 0xf3, 0x49, 0x30, 0x07, - - 0xa9, 0x95, 0x17, 0x31, - 0xc1, 0xd4, 0x32, 0xc5, - 0x2c, 0x4a, 0xac, 0x1f, - 0x1a, 0xbb, 0xa8, 0xef, - ]) { - panic! ("{}", String::from_utf8 (resp.to_vec ()).expect ("???")); - } - - // Requesting a file from a server that isn't registered - // will error out - - let resp = client.get (&format! ("{}/frontend/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url)) - .send ().await.expect ("Couldn't send request to bogus server"); - - assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND); + testing_client_checks (&testing_config, &client).await; info! ("Shutting down end-to-end test");