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
Add the socket-server-mocker dependency to your Cargo.toml
for testing compilation:
[]
= "0.3.0"
Example
You can view all example test codes in tests directory. In particular, you there are examples of mocking the protocols PostgreSQL, HTTP, DNS and SMTP.
Here is a simple example in TCP:
use ServerMocker;
use *;
use TcpServerMocker;
use ;
use TcpStream;
use from_utf8;
// Mock a TCP server listening on port 35642. Note that the mock will only listen on the local interface.
let tcp_server_mocker = new_with_port.unwrap;
// Create the TCP client to test
let mut client = connect.unwrap;
// Mocked server behavior
tcp_server_mocker.add_mock_instructions;
// TCP client sends its first message
client.write_all.unwrap;
// Read a message sent by the mocked server
let mut buffer = ;
let received_size = client.read.unwrap;
// convert shrunk buffer to string
let received_message = from_utf8.unwrap;
// Check that the message received by the client is the one sent by the mocked server
assert_eq!;
// Check that the mocked server received the message sent by the client
assert_eq!;
// New instructions for the mocked server
tcp_server_mocker.add_mock_instructions;
// Tested client send a message to the mocked server
client.write_all.unwrap;
// Read a message sent by the mocked server
let mut buffer = ;
let received_size = client.read.unwrap;
// convert shrunk buffer to string
let received_message = from_utf8.unwrap;
assert_eq!;
assert_eq!;
// Check that no error has been raised by the mocked server
assert!;
Another example in UDP:
use ;
use ;
use UdpSocket;
use from_utf8;
// Mock a UDP server listening on port 35642. Note that the mock will only listen on the local interface.
let udp_server_mocker = new_with_port.unwrap;
// Create the UDP client to test
let client_socket = bind.unwrap;
client_socket.connect.unwrap;
// Mocked server behavior
udp_server_mocker.add_mock_instructions;
// UDP client sends its first message
client_socket.send.unwrap;
// Read a message sent by the mocked server
let mut buffer = ;
let received_size = client_socket.recv.unwrap;
// convert shrunk buffer to string
let received_message = from_utf8.unwrap;
// Check that the message received by the client is the one sent by the mocked server
assert_eq!;
// Check that the mocked server received the message sent by the client
assert_eq!;
let received_size = client_socket.recv.unwrap;
// convert shrunk buffer to string
let received_message = from_utf8.unwrap;
// Check that the message received by the client is the one sent by the mocked server
assert_eq!;
// Check that no error has been raised by the mocked server
assert!;
Development
- This project is easier to develop with just, a modern alternative to
make
. Install it withcargo install just
. - To get a list of available commands, run
just
. - To run tests, use
just test
.