Skip to content

About

UDP mocking for black-box testing network applications

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

udp-wiremock

Provides an UDP receiver to perform black-box testing on Rust applications that output UDP packets.

Inspired by wiremock-rs

Features

  • Automatic test results verification
  • Multi-message matching

Limitations

udp-wiremock will not be able to tell appart messages that have identical field types and sizes.

Workarounds require altering messages sent by users, but we are trying to avoid that.

See tests/api/limitations.rs for an example.

Usage

#[derive(serde::Serialize, serde::Deserialize)]
struct MyMessage {
    foo: u8,
    bar: u64,
}

async fn send_one_my_message(dest: &SocketAddr) {
    let message = MyMessage { foo: 8, bar: 3 };
    let socket = UdpSocket::bind("0.0.0.0:0").await.unwrap();
    let buf = postcard::to_allocvec(&message).unwrap();
    socket.send_to(&buf, dest).await.unwrap();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test(flavor = "multi_thread")]
    async fn example_test() {
        let server = MockServer::start().await;

        // The mock server will expect exactly one `MyMessage` to be sent to `server.address()`
        // before the mock is dropped.
        MockTest::matching::<MyMessage>()
            .named("test_my_message")
            .expect(1)
            .mount(&server)
            .await;

        // This is the code that will be tested.
        send_one_my_message(server.address()).await;

        // The test should pass because the code under test will send one `MyMessage`
        // and that will satisfy our expectations.
    }
}

Examples

See tests and examples directories

About

UDP mocking for black-box testing network applications

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages