pub trait ServerMocker {
// Required methods
fn socket_address(&self) -> SocketAddr;
fn add_mock_instructions(
&self,
instructions: Vec<Instruction>,
) -> Result<(), ServerMockerError>;
fn pop_received_message(&self) -> Option<Vec<u8>>;
fn pop_server_error(&self) -> Option<ServerMockerError>;
// Provided method
fn port(&self) -> u16 { ... }
}
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 later check that the messages sent by the tested application are the ones expected.
Required Methods§
sourcefn socket_address(&self) -> SocketAddr
fn socket_address(&self) -> SocketAddr
Returns the socket address on which the mock server is listening
sourcefn add_mock_instructions(
&self,
instructions: Vec<Instruction>,
) -> Result<(), ServerMockerError>
fn add_mock_instructions( &self, instructions: Vec<Instruction>, ) -> Result<(), ServerMockerError>
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 Instruction::StopExchange
instruction)
If you push a Instruction::SendMessage
instruction, you must ensure that there is a client connected to the server mocker
If you push a Instruction::ReceiveMessage
instruction, you must ensure that the client will send a message to the server mocker within the timeout defined in the options.
sourcefn pop_received_message(&self) -> Option<Vec<u8>>
fn pop_received_message(&self) -> Option<Vec<u8>>
Return first message received by the mock server on the messages queue
If no message is available, wait for net_timeout
and then return None
If a message is available, will return the message and remove it from the queue
sourcefn pop_server_error(&self) -> Option<ServerMockerError>
fn pop_server_error(&self) -> Option<ServerMockerError>
Return first error received by the mock server on the errors queue
If no error is available, wait for net_timeout
and then return None
Provided Methods§
Implementors§
impl ServerMocker for TcpServerMocker
TcpServerMocker
implementation
§Example
use std::io::Write;
use std::net::TcpStream;
use socket_server_mocker::ServerMocker;
use socket_server_mocker::Instruction::{self, ReceiveMessage, StopExchange};
use socket_server_mocker::TcpServerMocker;
let server = TcpServerMocker::new().unwrap();
let mut client = TcpStream::connect(server.socket_address()).unwrap();
server.add_mock_instructions(vec![
ReceiveMessage,
StopExchange,
]).unwrap();
client.write_all(&[1, 2, 3]).unwrap();
let mock_server_received_message = server.pop_received_message();
assert_eq!(Some(vec![1, 2, 3]), mock_server_received_message);
assert!(server.pop_server_error().is_none());
assert!(server.pop_server_error().is_none());
impl ServerMocker for UdpServerMocker
UdpServerMocker
implementation
§Example
use std::net::{SocketAddr, UdpSocket};
use socket_server_mocker::ServerMocker;
use socket_server_mocker::Instruction::{ReceiveMessage, SendMessage, StopExchange};
use socket_server_mocker::UdpServerMocker;
let server = UdpServerMocker::new().unwrap();
// 0 = random port
let mut client = UdpSocket::bind("127.0.0.1:0").unwrap();
server.add_mock_instructions(vec![
ReceiveMessage,
SendMessage(vec![4, 5, 6]),
StopExchange,
]).unwrap();
client.send_to(&[1, 2, 3], server.socket_address()).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]), server.pop_received_message());
assert!(server.pop_server_error().is_none());