4 个版本
0.0.4 | 2022 年 10 月 16 日 |
---|---|
0.0.3 | 2022 年 10 月 8 日 |
0.0.2 | 2022 年 10 月 2 日 |
0.0.1 | 2022 年 10 月 1 日 |
#686 在 测试
272 每月下载
用于 2 crates
58KB
777 行
socket-server-mocker
在 Rust 中模拟套接字服务器,用于测试各种网络客户端。
我正在开发一个需要连接到外部服务器的应用程序,并且我正在寻找一种方法来测试应用程序发送给服务器的消息,直接使用 cargo test
。所以我寻找了一种方法来直接在 Rust 中模拟网络服务器,而无需每次启动测试时都集成实际的 Docker 服务器。
使用这个包,可以直接测试应用程序发送的消息,这些消息通常连接到服务器。
使用方法
将 socket-server-mocker 依赖项添加到您的 Cargo.toml
以进行测试编译
[dev-dependencies]
socket-server-mocker = "0.0.4"
示例
您可以在 tests 目录中查看所有示例测试代码。特别是,那里有模拟协议 PostgreSQL、HTTP、DNS 和 SMTP 的示例。
以下是 TCP 中的一个简单示例
use socket_server_mocker::server_mocker_instruction::{
ServerMockerInstruction, ServerMockerInstructionsList,
};
use socket_server_mocker::tcp_server_mocker::TcpServerMocker;
use std::io::{Read, Write};
use std::net::TcpStream;
use socket_server_mocker::server_mocker::ServerMocker;
#[test]
fn test_simple_tcp() {
// Mock a TCP server listening on port 35642. Note that the mock will only listen on the local interface.
let tcp_server_mocker = TcpServerMocker::new(35642).unwrap();
// Create the TCP client to test
let mut client = TcpStream::connect("127.0.0.1:35642").unwrap();
// Mocked server behavior
tcp_server_mocker.add_mock_instructions_list(
ServerMockerInstructionsList::new_with_instructions(
[
ServerMockerInstruction::ReceiveMessageWithMaxSize(16), // The mocked server will first wait for the client to send a message
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()), // Then it will send a message to the client
]
.as_slice(),
),
);
// TCP client sends its first message
client.write_all("hello from client".as_bytes()).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 = std::str::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
assert_eq!(
"hello from clien",
std::str::from_utf8(&*tcp_server_mocker.pop_received_message().unwrap()).unwrap()
);
// New instructions for the mocked server
let mut instructions = ServerMockerInstructionsList::new().with_added_receive_message(); // Wait for another message from the tested client
instructions.add_send_message_depending_on_last_received_message(|_| {
None
}); // No message is sent to the server
instructions.add_send_message_depending_on_last_received_message(|last_received_message| {
// "hello2 from client"
let mut received_message_string : String = std::str::from_utf8(&last_received_message.unwrap()).unwrap().to_string();
// "hello2"
received_message_string.truncate(5);
Some(format!("{}2 from server", received_message_string).as_bytes().to_vec())
}); // Send a message to the client depending on the last received message by the mocked server
instructions.add_stop_exchange(); // Finally close the connection
tcp_server_mocker.add_mock_instructions_list(instructions);
// Tested client send a message to the mocked server
client.write_all("hello2 from client".as_bytes()).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 = std::str::from_utf8(&buffer[..received_size]).unwrap();
assert_eq!("hello2 from server", received_message);
assert_eq!(
"hello2 from client",
std::str::from_utf8(&*tcp_server_mocker.pop_received_message().unwrap()).unwrap()
);
// Check that no error has been raised by the mocked server
assert!(tcp_server_mocker.pop_server_error().is_none());
}
以下是 UDP 中的一个示例
use std::net::UdpSocket;
use socket_server_mocker::server_mocker::ServerMocker;
use socket_server_mocker::server_mocker_instruction::{ServerMockerInstruction, ServerMockerInstructionsList};
use socket_server_mocker::udp_server_mocker;
#[test]
fn test_simple_udp() {
// Mock a UDP server listening on port 35642. Note that the mock will only listen on the local interface.
let udp_server_mocker = udp_server_mocker::UdpServerMocker::new(35642).unwrap();
// Create the UDP client to test
let client_socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
client_socket.connect("127.0.0.1:35642").unwrap();
// Mocked server behavior
udp_server_mocker.add_mock_instructions_list(
ServerMockerInstructionsList::new_with_instructions(
&[
// The mocked server will first wait for the client to send a message, with max size = 32 bytes
ServerMockerInstruction::ReceiveMessageWithMaxSize(32),
// Then it will send a message to the client
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()),
// Send nothing
ServerMockerInstruction::SendMessageDependingOnLastReceivedMessage(|_| {
None
}),
// Send a message to the client depending on the last received message by the mocked server
ServerMockerInstruction::SendMessageDependingOnLastReceivedMessage(|last_received_message| {
// "hello2 from client"
let mut received_message_string: String = std::str::from_utf8(&last_received_message.unwrap()).unwrap().to_string();
// "hello2"
received_message_string.truncate(5);
Some(format!("{}2 from server", received_message_string).as_bytes().to_vec())
}),
]
),
);
// UDP client sends its first message
client_socket.send("hello from client".as_bytes()).unwrap();
// Read a message sent by the mocked server
let mut buffer = [0; 32];
let received_size = client_socket.recv(&mut buffer).unwrap();
// convert shrunk buffer to string
let received_message = std::str::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
assert_eq!(
"hello from client",
std::str::from_utf8(&*udp_server_mocker.pop_received_message().unwrap()).unwrap()
);
let received_size = client_socket.recv(&mut buffer).unwrap();
// convert shrunk buffer to string
let received_message = std::str::from_utf8(&buffer[..received_size]).unwrap();
// Check that the message received by the client is the one sent by the mocked server
assert_eq!("hello2 from server", received_message);
// Check that no error has been raised by the mocked server
assert!(udp_server_mocker.pop_server_error().is_none());
}