Struct ServerMocker

Source
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>

Source

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.

Source

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>

Source

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.

Source

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>

Source

pub fn options(&self) -> &T

Get the options used to create the server mocker

Source

pub fn socket_address(&self) -> SocketAddr

Get the socket address on which the server is listening

Source

pub fn port(&self) -> u16

Get the port on which the server is listening

Source

pub fn add_mock_instructions( &self, instructions: Vec<Instruction>, ) -> Result<(), ServerMockerError>

Add instructions to the server mocker

Source

pub fn pop_received_message(&self) -> Option<Vec<u8>>

Pop the last received message from the server mocker

Source

pub fn pop_server_error(&self) -> Option<ServerMockerError>

Pop the last server error from the server mocker

Source

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.

Auto Trait Implementations§

§

impl<T> Freeze for ServerMocker<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ServerMocker<T>
where T: RefUnwindSafe,

§

impl<T> Send for ServerMocker<T>
where T: Send,

§

impl<T> !Sync for ServerMocker<T>

§

impl<T> Unpin for ServerMocker<T>
where T: Unpin,

§

impl<T> UnwindSafe for ServerMocker<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.