pub trait ServerMocker {
    const DEFAULT_NET_TIMEOUT_MS: u64 = 100u64;
    const DEFAULT_THREAD_RECEIVER_TIMEOUT_MS: u64 = 100u64;

    fn new(port: u16) -> Result<Self, ServerMockerError>
    where
        Self: Sized
; fn listening_port(&self) -> u16; fn add_mock_instructions_list(
        &self,
        instructions_list: ServerMockerInstructionsList
    ) -> Result<(), ServerMockerError>; fn pop_received_message(&self) -> Option<BinaryMessage>; fn pop_server_error(&self) -> Option<ServerMockerError>; fn add_mock_instructions(
        &self,
        instructions: &[ServerMockerInstruction]
    ) -> Result<(), ServerMockerError> { ... } }
Expand description

Trait that define the behavior of a network server mocker over an IP layer.

The mocker is able to receive and send messages to the application that is tested.

The mocker can be configured to send messages to the tested application depending on the messages received.

You can afterwards check that the messages sent by the tested application are the ones expected.

Provided Associated Constants

Default timeout in milliseconds for the server to wait for a message from the client.

Timeout if no more instruction is available and ServerMockerInstruction::StopExchange hasn’t been sent

Required Methods

Creates a new server mocker

Arguments

port - the port to listen on, should be the same as the port the application you want to test uses to connect to the server

Will listen on the local interface, port should not be used by another listening application

Note that only 1 client will be able to connect to the server in case you use TCP, and the messages that the server send back to the client will be sent to the last client that sent to the server.

If port is set to 0, the OS will choose a free port. Then you can get the port with listening_port

Panics

Will panic in case of error with thread channel

Returns the port on which the mock server is listening

Listen only on local interface

Port should not be used by another listening process

Adds a list of instructions to the server mocker

The server mocker will execute the instructions in the order they are added

This function could be called as many times as you want, until the connection is closed (event by the client or the server if received a ServerMockerInstruction::StopExchange instruction)

If you push a ServerMockerInstruction::SendMessage instruction, you must ensure that there is a client connected to the server mocker

If you push a ServerMockerInstruction::ReceiveMessage instruction, you must ensure that the client will send a message to the server mocker within the timeout defined in ServerMocker::DEFAULT_NET_TIMEOUT_MS

Return first message received by the mock server on the messages queue

If no message is available, wait during ServerMocker::DEFAULT_NET_TIMEOUT_MS and then return None

If a message is available, will return the message and remove it from the queue

Return first error received by the mock server on the errors queue

If no error is available, wait during ServerMocker::DEFAULT_NET_TIMEOUT_MS and then return None

Provided Methods

Adds a slice of instructions to the server mocker

The server mocker will execute the instructions in the order they are added

This function could be called as many times as you want, until the connection is closed (event by the client or the server if received a ServerMockerInstruction::StopExchange instruction)

If you push a ServerMockerInstruction::SendMessage instruction, you must ensure that there is a client connected to the server mocker

If you push a ServerMockerInstruction::ReceiveMessage instruction, you must ensure that the client will send a message to the server mocker within the timeout defined in ServerMocker::DEFAULT_NET_TIMEOUT_MS

Implementors

TcpServerMocker implementation

Example

use std::io::Write;
use std::net::TcpStream;
use socket_server_mocker::server_mocker::ServerMocker;
use socket_server_mocker::server_mocker_instruction::{ServerMockerInstructionsList, ServerMockerInstruction};
use socket_server_mocker::tcp_server_mocker::TcpServerMocker;

let tcp_server_mocker = TcpServerMocker::new(1234).unwrap();
let mut client = TcpStream::connect("127.0.0.1:1234").unwrap();

tcp_server_mocker.add_mock_instructions_list(ServerMockerInstructionsList::new_with_instructions([
    ServerMockerInstruction::ReceiveMessage,
    ServerMockerInstruction::StopExchange,
].as_slice()));
client.write_all(&[1, 2, 3]).unwrap();

let mock_server_received_message = tcp_server_mocker.pop_received_message();
assert_eq!(Some(vec![1, 2, 3]), mock_server_received_message);
assert!(tcp_server_mocker.pop_server_error().is_none());
assert!(tcp_server_mocker.pop_server_error().is_none());

UdpServerMocker implementation

Example

use std::net::{SocketAddr, UdpSocket};
use socket_server_mocker::server_mocker::ServerMocker;
use socket_server_mocker::server_mocker_instruction::{ServerMockerInstructionsList, ServerMockerInstruction};
use socket_server_mocker::udp_server_mocker::UdpServerMocker;

// 0 = random port
let udp_server_mocker = UdpServerMocker::new(0).unwrap();
let mut client = UdpSocket::bind("127.0.0.1:0").unwrap();
let server_addr = SocketAddr::from(([127, 0, 0, 1], udp_server_mocker.listening_port()));

udp_server_mocker.add_mock_instructions_list(ServerMockerInstructionsList::new_with_instructions([
   ServerMockerInstruction::ReceiveMessage,
   ServerMockerInstruction::SendMessage(vec![4, 5, 6]),
   ServerMockerInstruction::StopExchange,
].as_slice()));
client.send_to(&[1, 2, 3], server_addr).unwrap();
let mut buffer = [0; 3];
client.recv_from(&mut buffer).unwrap();
assert_eq!([4, 5, 6], buffer);
assert_eq!(Some(vec![1, 2, 3]), udp_server_mocker.pop_received_message());
assert!(udp_server_mocker.pop_server_error().is_none());