pub struct ServerMocker<T> { /* private fields */ }
Expand description
A socket server mocker, able to mock a TCP or UDP server to help test socket connections in a user app.
§TCP Example
use std::io::Write;
use std::net::TcpStream;
use socket_server_mocker::ServerMocker;
use socket_server_mocker::Instruction::{self, ReceiveMessage, StopExchange};
let server = ServerMocker::tcp().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());
§UDP Example
use std::net::{SocketAddr, UdpSocket};
use socket_server_mocker::ServerMocker;
use socket_server_mocker::Instruction::{ReceiveMessage, SendMessage, StopExchange};
let server = ServerMocker::udp().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());
Implementations§
Source§impl ServerMocker<TcpMocker>
impl ServerMocker<TcpMocker>
Sourcepub fn tcp() -> Result<Self, ServerMockerError>
pub fn tcp() -> Result<Self, ServerMockerError>
Create a new instance of the UDP server mocker on a random free port.
The port can be retrieved with the ServerMocker::port
method.
Sourcepub fn tcp_with_port(port: u16) -> Result<Self, ServerMockerError>
pub fn tcp_with_port(port: u16) -> Result<Self, ServerMockerError>
Create a new instance of the UDP server mocker on the given port. If the port is already in use, the method will return an error.
Source§impl ServerMocker<UdpMocker>
impl ServerMocker<UdpMocker>
Sourcepub fn udp() -> Result<Self, ServerMockerError>
pub fn udp() -> Result<Self, ServerMockerError>
Create a new instance of the UDP server mocker on a random free port.
The port can be retrieved with the ServerMocker::port
method.
Sourcepub fn udp_with_port(port: u16) -> Result<Self, ServerMockerError>
pub fn udp_with_port(port: u16) -> Result<Self, ServerMockerError>
Create a new instance of the UDP server mocker on the given port. If the port is already in use, the method will return an error.
Source§impl<T: MockerOptions> ServerMocker<T>
impl<T: MockerOptions> ServerMocker<T>
Sourcepub fn socket_address(&self) -> SocketAddr
pub fn socket_address(&self) -> SocketAddr
Get the socket address on which the server is listening
Sourcepub fn add_mock_instructions(
&self,
instructions: Vec<Instruction>,
) -> Result<(), ServerMockerError>
pub fn add_mock_instructions( &self, instructions: Vec<Instruction>, ) -> Result<(), ServerMockerError>
Add instructions to the server mocker
Sourcepub fn pop_received_message(&self) -> Option<Vec<u8>>
pub fn pop_received_message(&self) -> Option<Vec<u8>>
Pop the last received message from the server mocker
Sourcepub fn pop_server_error(&self) -> Option<ServerMockerError>
pub fn pop_server_error(&self) -> Option<ServerMockerError>
Pop the last server error from the server mocker
Sourcepub fn new_with_opts(options: T) -> Result<Self, ServerMockerError>
pub fn new_with_opts(options: T) -> Result<Self, ServerMockerError>
Create a new instance of the TCP server mocker with the given options.
§Panics
It is assumed that threads can use messages channels without panicking.