1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::io::{Read, Write};
use std::net::TcpStream;
use std::str::from_utf8;
use std::thread::sleep;
use socket_server_mocker::Instruction::{
ReceiveMessage, ReceiveMessageWithMaxSize, SendMessage,
SendMessageDependingOnLastReceivedMessage, StopExchange,
};
use socket_server_mocker::{ServerMocker, ServerMockerError, TcpMocker};
#[test]
fn test_simple_tcp() {
// Mock a TCP server listening on a specific port. Note that the mock will only listen on the local interface.
let options = TcpMocker {
socket_addr: "127.0.0.1:35642".parse().unwrap(),
..TcpMocker::default()
};
let server = ServerMocker::new_with_opts(options).unwrap();
// Create the TCP client to test
let mut client = TcpStream::connect(server.socket_address()).unwrap();
// Mocked server behavior
server
.add_mock_instructions(vec![
ReceiveMessageWithMaxSize(16), // The mocked server will first wait for the client to send a message
SendMessage(b"hello from server".to_vec()), // Then it will send a message to the client
])
.unwrap();
// TCP client sends its first message
client.write_all(b"hello from client").unwrap();
// Read a message sent by the mocked server
let mut buffer = [0; 1024];
let received_size = client.read(&mut buffer).unwrap();
// convert shrunk buffer to string
let received_message = from_utf8(&buffer[..received_size]).unwrap();
// Check that the message received by the client is the one sent by the mocked server
assert_eq!("hello from server", received_message);
// Check that the mocked server received the message sent by the client
// The message is only 16 bytes, the letter 't' is dropped
assert_eq!(
"hello from clien",
from_utf8(&server.pop_received_message().unwrap()).unwrap()
);
// New instructions for the mocked server
let instructions = vec![
// Wait for another message from the tested client
ReceiveMessage,
// No message is sent to the server
SendMessageDependingOnLastReceivedMessage(|_| None),
// Send a message to the client depending on the last received message by the mocked server
SendMessageDependingOnLastReceivedMessage(|last_received_message| {
// "hello2 from client"
let mut received_message_string: String = from_utf8(&last_received_message.unwrap())
.unwrap()
.to_string();
// "hello2"
received_message_string.truncate(5);
Some(
format!("{received_message_string}2 from server")
.as_bytes()
.to_vec(),
)
}),
// Finally close the connection
StopExchange,
];
server.add_mock_instructions(instructions).unwrap();
// Tested client send a message to the mocked server
client.write_all(b"hello2 from client").unwrap();
// Read a message sent by the mocked server
let mut buffer = [0; 1024];
let received_size = client.read(&mut buffer).unwrap();
// convert shrunk buffer to string
let received_message = from_utf8(&buffer[..received_size]).unwrap();
assert_eq!("hello2 from server", received_message);
assert_eq!(
"hello2 from client",
from_utf8(&server.pop_received_message().unwrap()).unwrap()
);
// Check that no error has been raised by the mocked server
assert!(server.pop_server_error().is_none());
}
#[test]
fn test_try_listen_twice_on_same_port() {
// First server will listen on a random free port
let server = ServerMocker::tcp().unwrap();
// Second server will try to listen on the same port
let server2 = ServerMocker::tcp_with_port(server.port());
// The second server should fail to listen on the same port
assert!(matches!(
server2,
Err(ServerMockerError::UnableToBindListener(..))
));
}
#[test]
fn test_receive_timeout() {
// Mock a TCP server listening on a random free port
let server = ServerMocker::tcp().unwrap();
// Create the TCP client to test
let _client = TcpStream::connect(server.socket_address()).unwrap();
// Mocked server behavior
server
.add_mock_instructions(vec![
// Expect to receive a message from the client
ReceiveMessage,
])
.unwrap();
// Wait twice the timeout
sleep(2 * server.options().rx_timeout);
// Check that the mocked server has raised an error
let err = server.pop_server_error();
assert!(err.is_some());
let err = err.unwrap();
assert!(!err.is_fatal());
}