socket-server-mocker
Mock socket server in Rust, for testing various network clients.
I was developing an application that needed to connect to an external server, and I was looking for a way to test the messages sent by the application to the server, directly with cargo test
. So I looked for a way to directly mock a network server in Rust, without having to integrate a real server in docker each time the tests were launched.
With this crate, it is possible to directly test the messages sent by your application which normally connects to a server.
Usage
To be filled
Example
use std::io::{Read, Write};
use std::net::TcpStream;
use socket_server_mocker::server_mocker_instruction::{ServerMockerInstruction, ServerMockerInstructionsList};
use socket_server_mocker::tcp_server_mocker::TcpServerMocker;
#[test]
fn test_simple_tcp() {
let tcp_server_mocker = TcpServerMocker::new(35642);
let mut client = TcpStream::connect("127.0.0.1:35642").unwrap();
tcp_server_mocker.add_mock_instructions_list(ServerMockerInstructionsList::new_with_instructions([
ServerMockerInstruction::ReceiveMessage, ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()), ].as_slice()
));
client.write_all("hello from client".as_bytes()).unwrap();
let mut buffer = [0; 1024];
let received_size = client.read(&mut buffer).unwrap();
let received_message = std::str::from_utf8(&buffer[..received_size]).unwrap();
assert_eq!("hello from server", received_message);
assert_eq!("hello from client", std::str::from_utf8(&*tcp_server_mocker.pop_received_message().unwrap()).unwrap());
let mut instructions = ServerMockerInstructionsList::new().with_added_receive_message(); instructions.add_send_message("hello2 from server".as_bytes().to_vec()); instructions.add_stop_exchange();
tcp_server_mocker.add_mock_instructions_list(instructions);
client.write_all("hello2 from client".as_bytes()).unwrap();
let mut buffer = [0; 1024];
let received_size = client.read(&mut buffer).unwrap();
let received_message = std::str::from_utf8(&buffer[..received_size]).unwrap();
assert_eq!("hello2 from server", received_message);
assert_eq!("hello2 from client", std::str::from_utf8(&*tcp_server_mocker.pop_received_message().unwrap()).unwrap());
}