♻️ refactor: extract basic client tests
							parent
							
								
									4838a99288
								
							
						
					
					
						commit
						1e42110ae1
					
				
							
								
								
									
										103
									
								
								src/tests.rs
								
								
								
								
							
							
						
						
									
										103
									
								
								src/tests.rs
								
								
								
								
							| 
						 | 
					@ -18,8 +18,6 @@ use tokio::{
 | 
				
			||||||
use reqwest::Client;
 | 
					use reqwest::Client;
 | 
				
			||||||
use tracing::{debug, info};
 | 
					use tracing::{debug, info};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use ptth_relay::load_templates;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Poll for a few seconds till the server is ready
 | 
					// Poll for a few seconds till the server is ready
 | 
				
			||||||
// If this takes more than 5 seconds-ish, it's bad, the test should
 | 
					// If this takes more than 5 seconds-ish, it's bad, the test should
 | 
				
			||||||
// fail
 | 
					// 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 {
 | 
					struct TestingConfig {
 | 
				
			||||||
	server_name: &'static str,
 | 
						server_name: &'static str,
 | 
				
			||||||
	api_key: &'static str,
 | 
						api_key: &'static str,
 | 
				
			||||||
| 
						 | 
					@ -172,38 +208,9 @@ fn end_to_end () {
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		let client = Client::builder ()
 | 
							let client = Client::builder ()
 | 
				
			||||||
		.timeout (Duration::from_secs (2))
 | 
					 | 
				
			||||||
		.build ().expect ("Couldn't build HTTP client");
 | 
							.build ().expect ("Couldn't build HTTP client");
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url))
 | 
							testing_client_checks (&testing_config, &client).await;
 | 
				
			||||||
		.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);
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		info! ("Shutting down end-to-end test");
 | 
							info! ("Shutting down end-to-end test");
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
| 
						 | 
					@ -232,7 +239,6 @@ fn debug_proxy () {
 | 
				
			||||||
			proxy_port,
 | 
								proxy_port,
 | 
				
			||||||
			relay_port,
 | 
								relay_port,
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
		let relay_url = testing_config.relay_url ();
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		let testing_relay = TestingRelay::new (&testing_config).await;
 | 
							let testing_relay = TestingRelay::new (&testing_config).await;
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
| 
						 | 
					@ -254,38 +260,9 @@ fn debug_proxy () {
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		let client = Client::builder ()
 | 
							let client = Client::builder ()
 | 
				
			||||||
		.timeout (Duration::from_secs (2))
 | 
					 | 
				
			||||||
		.build ().expect ("Couldn't build HTTP client");
 | 
							.build ().expect ("Couldn't build HTTP client");
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url))
 | 
							testing_client_checks (&testing_config, &client).await;
 | 
				
			||||||
		.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);
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		info! ("Shutting down end-to-end test");
 | 
							info! ("Shutting down end-to-end test");
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue