Add new test based on token auth
This commit is contained in:
@ -91,6 +91,38 @@ impl DummyTCPServer {
|
||||
self.next_conn_square_operations().await
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform complex exchange: receive numbers from client and respond with their value + a given number
|
||||
pub async fn next_conn_add_operations(&self, inc: i32) {
|
||||
let (mut conn, _addr) = self
|
||||
.0
|
||||
.accept()
|
||||
.await
|
||||
.expect("Could not open next connection!");
|
||||
|
||||
let mut buff: [u8; 100] = [0; 100];
|
||||
loop {
|
||||
let size = conn.read(&mut buff).await.unwrap();
|
||||
if size == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
let content = String::from_utf8_lossy(&buff[0..size])
|
||||
.to_string()
|
||||
.parse::<i64>()
|
||||
.unwrap();
|
||||
|
||||
conn.write_all((content + inc as i64).to_string().as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn loop_next_conn_add_operations(&self, inc: i32) {
|
||||
loop {
|
||||
self.next_conn_add_operations(inc).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn dummy_tcp_client_read_conn(port: u16) -> Vec<u8> {
|
||||
@ -152,6 +184,33 @@ pub async fn dummy_tcp_client_square_root_requests(port: u16, num_exchanges: usi
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn dummy_tcp_client_additions_requests(port: u16, inc: i32, num_exchanges: usize) {
|
||||
let mut socket = TcpStream::connect(format!("127.0.0.1:{}", port))
|
||||
.await
|
||||
.expect("Failed to connect to dummy TCP server!");
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut buff: [u8; 100] = [0; 100];
|
||||
|
||||
for _ in 0..num_exchanges {
|
||||
let num = rng.gen::<i32>() % 100;
|
||||
socket.write_all(num.to_string().as_bytes()).await.unwrap();
|
||||
|
||||
let size = socket.read(&mut buff).await.unwrap();
|
||||
|
||||
if size == 0 {
|
||||
panic!("Got empty response!");
|
||||
}
|
||||
|
||||
let got = String::from_utf8_lossy(&buff[0..size])
|
||||
.to_string()
|
||||
.parse::<i64>()
|
||||
.unwrap();
|
||||
println!("{} + {} = {} (based on server response)", num, inc, got);
|
||||
assert_eq!((num + inc) as i64, got);
|
||||
}
|
||||
}
|
||||
|
||||
/// Check whether a given port is open or not
|
||||
pub async fn is_port_open(port: u16) -> bool {
|
||||
match TcpStream::connect(("127.0.0.1", port)).await {
|
||||
@ -175,8 +234,9 @@ pub async fn wait_for_port(port: u16) {
|
||||
|
||||
mod test {
|
||||
use crate::test::dummy_tcp_sockets::{
|
||||
dummy_tcp_client_read_conn, dummy_tcp_client_square_root_requests,
|
||||
dummy_tcp_client_write_conn, dummy_tcp_client_write_then_read_conn, DummyTCPServer,
|
||||
dummy_tcp_client_additions_requests, dummy_tcp_client_read_conn,
|
||||
dummy_tcp_client_square_root_requests, dummy_tcp_client_write_conn,
|
||||
dummy_tcp_client_write_then_read_conn, DummyTCPServer,
|
||||
};
|
||||
use crate::test::{get_port_number, PortsAllocation};
|
||||
|
||||
@ -253,4 +313,27 @@ mod test {
|
||||
|
||||
handle.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn socket_dummy_addition_calculator() {
|
||||
let listener = DummyTCPServer::start(port(5)).await;
|
||||
let handle = tokio::spawn(async move {
|
||||
listener.next_conn_add_operations(4).await;
|
||||
});
|
||||
let data = dummy_tcp_client_write_then_read_conn(port(5), "5".as_bytes()).await;
|
||||
assert_eq!(data, "9".as_bytes());
|
||||
|
||||
handle.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn socket_dummy_addition_calculator_multiple() {
|
||||
let listener = DummyTCPServer::start(port(6)).await;
|
||||
let handle = tokio::spawn(async move {
|
||||
listener.next_conn_add_operations(7).await;
|
||||
});
|
||||
dummy_tcp_client_additions_requests(port(6), 7, 10).await;
|
||||
|
||||
handle.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user