{"id":33936813,"url":"https://github.com/thomasarmel/socket-server-mocker","last_synced_at":"2026-04-07T15:31:13.733Z","repository":{"id":60580070,"uuid":"544017381","full_name":"thomasarmel/socket-server-mocker","owner":"thomasarmel","description":"Mock socket server in Rust, for testing various network clients.","archived":false,"fork":false,"pushed_at":"2024-10-13T16:20:17.000Z","size":104,"stargazers_count":2,"open_issues_count":5,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-12-14T02:38:20.893Z","etag":null,"topics":["mock","network","rust","socket","tcp","test","test-automation","testing"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/socket-server-mocker","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomasarmel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-01T12:22:02.000Z","updated_at":"2025-05-05T16:14:26.000Z","dependencies_parsed_at":"2022-10-01T17:10:18.731Z","dependency_job_id":null,"html_url":"https://github.com/thomasarmel/socket-server-mocker","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/thomasarmel/socket-server-mocker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasarmel%2Fsocket-server-mocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasarmel%2Fsocket-server-mocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasarmel%2Fsocket-server-mocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasarmel%2Fsocket-server-mocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomasarmel","download_url":"https://codeload.github.com/thomasarmel/socket-server-mocker/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasarmel%2Fsocket-server-mocker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31518398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T03:10:19.677Z","status":"ssl_error","status_checked_at":"2026-04-07T03:10:13.982Z","response_time":105,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","network","rust","socket","tcp","test","test-automation","testing"],"created_at":"2025-12-12T14:35:09.845Z","updated_at":"2026-04-07T15:31:13.722Z","avatar_url":"https://github.com/thomasarmel.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socket-server-mocker\n\n[![GitHub](https://img.shields.io/badge/github-thomasarmel/socket--server--mocker-8da0cb?logo=github)](https://github.com/thomasarmel/socket-server-mocker)\n[![crates.io version](https://img.shields.io/crates/v/socket-server-mocker.svg)](https://crates.io/crates/socket-server-mocker)\n[![docs.rs docs](https://docs.rs/socket-server-mocker/badge.svg)](https://docs.rs/socket-server-mocker)\n[![crates.io version](https://img.shields.io/crates/l/socket-server-mocker.svg)](https://github.com/thomasarmel/socket-server-mocker/blob/main/LICENSE)\n[![CI build](https://github.com/thomasarmel/socket-server-mocker/actions/workflows/rust.yml/badge.svg)](https://github.com/thomasarmel/socket-server-mocker/actions)\n\n_Mock socket server in Rust, for testing various network clients._\n\n***\n\nI 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.\n\nWith this crate, it is possible to directly test the messages sent by your application which normally connects to a server.\n\n\n## Usage\n\nAdd the **socket-server-mocker** dependency to your `Cargo.toml` for testing compilation:\n\n```toml\n[dev-dependencies]\nsocket-server-mocker = \"0.5\"\n```\n\n## Example\n\nYou can view all example test codes in **[tests](./tests)** directory.\nIn particular, you there are examples of mocking the protocols [PostgreSQL](tests/postgres_mock.rs), [HTTP](tests/http_reqwest_api_mock.rs), [DNS](./tests/dns_mock.rs) and [SMTP](./tests/smtp_mock.rs).\n\nHere is a simple example in TCP:\n\n```rust\nuse socket_server_mocker::ServerMocker;\nuse socket_server_mocker::Instruction::*;\nuse std::io::{Read, Write};\nuse std::net::TcpStream;\nuse std::str::from_utf8;\n\n// Mock a TCP server listening on port 35642. Note that the mock will only listen on the local interface.\nlet server = ServerMocker::tcp_with_port(35642).unwrap();\n\n// Create the TCP client to test\nlet mut client = TcpStream::connect(server.socket_address()).unwrap();\n\n// Mocked server behavior\nserver.add_mock_instructions(vec![\n    ReceiveMessageWithMaxSize(16), // The mocked server will first wait for the client to send a message\n    SendMessage(b\"hello from server\".to_vec()), // Then it will send a message to the client\n]);\n\n// TCP client sends its first message\nclient.write_all(b\"hello from client\").unwrap();\n\n// Read a message sent by the mocked server\nlet mut buffer = [0; 1024];\nlet received_size = client.read(\u0026mut buffer).unwrap();\n\n// convert shrunk buffer to string\nlet received_message = from_utf8(\u0026buffer[..received_size]).unwrap();\n\n// Check that the message received by the client is the one sent by the mocked server\nassert_eq!(\"hello from server\", received_message);\n\n// Check that the mocked server received the message sent by the client\nassert_eq!(\n    \"hello from clien\", // Max 16 bytes, the word \"client\" is truncated\n    from_utf8(server.pop_received_message().unwrap().as_ref()).unwrap()\n);\n\n// New instructions for the mocked server\nserver.add_mock_instructions(vec![\n    ReceiveMessage, // Wait for another message from the tested client\n    SendMessageDependingOnLastReceivedMessage(|_| {\n        None\n    }), // No message is sent to the server\n    SendMessageDependingOnLastReceivedMessage(|last_received_message| {\n        // \"hello2 from client\"\n        let mut received_message_string: String = from_utf8(\u0026last_received_message.unwrap()).unwrap().to_string();\n        // \"hello2\"\n        received_message_string.truncate(5);\n        Some(format!(\"{}2 from server\", received_message_string).as_bytes().to_vec())\n    }), // Send a message to the client depending on the last received message by the mocked server\n    StopExchange, // Finally close the connection\n]);\n\n// Tested client send a message to the mocked server\nclient.write_all(b\"hello2 from client\").unwrap();\n\n// Read a message sent by the mocked server\nlet mut buffer = [0; 1024];\nlet received_size = client.read(\u0026mut buffer).unwrap();\n\n// convert shrunk buffer to string\nlet received_message = from_utf8(\u0026buffer[..received_size]).unwrap();\n\nassert_eq!(\"hello2 from server\", received_message);\n\nassert_eq!(\n    \"hello2 from client\",\n    from_utf8(\u0026*server.pop_received_message().unwrap()).unwrap()\n);\n\n// Check that no error has been raised by the mocked server\nassert!(server.pop_server_error().is_none());\n```\n\nAnother example in UDP:\n\n```rust\nuse socket_server_mocker::ServerMocker;\nuse socket_server_mocker::Instruction::{SendMessage, SendMessageDependingOnLastReceivedMessage, ReceiveMessageWithMaxSize};\nuse std::net::UdpSocket;\nuse std::str::from_utf8;\n\n// Mock a UDP server listening on port 35642. Note that the mock will only listen on the local interface.\nlet server = ServerMocker::udp_with_port(35642).unwrap();\n\n// Create the UDP client to test at a random port\nlet client_socket = UdpSocket::bind(\"127.0.0.1:0\").unwrap();\nclient_socket.connect(server.socket_address()).unwrap();\n\n// Mocked server behavior\nserver.add_mock_instructions(vec![\n    // The mocked server will first wait for the client to send a message, with max size = 32 bytes\n    ReceiveMessageWithMaxSize(32),\n    // Then it will send a message to the client\n    SendMessage(b\"hello from server\".to_vec()),\n    // Send nothing\n    SendMessageDependingOnLastReceivedMessage(|_| {\n        None\n    }),\n    // Send a message to the client depending on the last received message by the mocked server\n    SendMessageDependingOnLastReceivedMessage(|last_received_message| {\n        // \"hello2 from client\"\n        let mut received_message_string: String = from_utf8(\u0026last_received_message.unwrap()).unwrap().to_string();\n        // \"hello2\"\n        received_message_string.truncate(5);\n        Some(format!(\"{}2 from server\", received_message_string).as_bytes().to_vec())\n    }),\n]\n);\n\n// UDP client sends its first message\nclient_socket.send(b\"hello from client\").unwrap();\n\n// Read a message sent by the mocked server\nlet mut buffer = [0; 32];\nlet received_size = client_socket.recv(\u0026mut buffer).unwrap();\n\n// convert shrunk buffer to string\nlet received_message = from_utf8(\u0026buffer[..received_size]).unwrap();\n\n// Check that the message received by the client is the one sent by the mocked server\nassert_eq!(\"hello from server\", received_message);\n\n// Check that the mocked server received the message sent by the client\nassert_eq!(\n    \"hello from client\",\n    from_utf8(\u0026*server.pop_received_message().unwrap()).unwrap()\n);\n\nlet received_size = client_socket.recv(\u0026mut buffer).unwrap();\n// convert shrunk buffer to string\nlet received_message = from_utf8(\u0026buffer[..received_size]).unwrap();\n\n// Check that the message received by the client is the one sent by the mocked server\nassert_eq!(\"hello2 from server\", received_message);\n\n// Check that no error has been raised by the mocked server\nassert!(server.pop_server_error().is_none());\n```\n\n## Development\n\n* This project is easier to develop with [just](https://github.com/casey/just#readme), a modern alternative to `make`.\n  Install it with `cargo install just`.\n* To get a list of available commands, run `just`.\n* To run tests, use `just test`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasarmel%2Fsocket-server-mocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasarmel%2Fsocket-server-mocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasarmel%2Fsocket-server-mocker/lists"}